Hey I was wondering if you guys know of any good tutorials for php/mysql where you can encode the image straight into the database. I saw spoonos tutorial on it but I was wondering if there were others. Isn't there a way to do it with the "blob" type? I don't really know I am just wondering what different ways you can do it.
Image Databasing
Started by Nerdstyle, Mar 14 2006 02:20 AM
4 replies to this topic
#1
Posted 14 March 2006 - 02:20 AM
#2
Posted 14 March 2006 - 09:07 AM
i dont think there is a way to store the actual image into the database but the most comon thing to do is to store the location to the image in the column in the database e.g. images/matt.jpg.
#3
Posted 17 March 2006 - 12:13 PM
Maybe you can use this:
<?php
/******************************************\
* *
* CREATE TABLE pictures ( *
* pid int(11) NOT NULL auto_increment, *
* title text, *
* imgdata blob, *
* PRIMARY KEY (pid) *
* ) ENGINE=MyISAM; *
* *
\******************************************/
require("./../connect.php");
function resize( $filename, $newfilename, $maxw, $maxh )
{
$result = false;
$srcim = imagecreatefromjpeg( $filename );
$ow = imagesx( $srcim );
$oh = imagesy( $srcim );
$wscale = $maxw / $ow;
$hscale = $maxh / $oh;
$scale = ( $hscale < $wscale ? $hscale : $wscale );
$nw = round( $ow * $scale, 0 );
$nh = round( $oh * $scale, 0 );
$dstim = imagecreatetruecolor( $nw, $nh );
imagecopyresampled( $dstim, $srcim, 0, 0, 0, 0, $nw, $nh, $ow, $oh );
$result = imagejpeg( $dstim, $newfilename, 85 );
imagedestroy( $dstim );
imagedestroy( $srcim );
return $result;
}
$allowPics = array("jpg","jpeg","jpe");
if ($_POST['completed'] == 1)
{
// Need to add - check for large upload. Otherwise the code
// will just duplicate old file;-)
// ALSO - note that latest.img must be public write and in a
// live appliaction should be in another (safe!) directory.
if($_FILES['imagefile']['size'] > 1024000)
{
$errmsg = "Too large!";
}
else
{
$extension = strtolower(end(explode(".",$_FILES['imagefile']['name'])));
if(!in_array($extension,$allowPics))
{
$errmsg = "Wrong filetype!";
}
else
{
resize($_FILES['imagefile']['tmp_name'],"latest.img",300,300);
$instr = fopen("latest.img","rb");
$image = addslashes(fread($instr,filesize("latest.img")));
mysql_query ("insert into pictures (title, imgdata) values (\"".$_POST['whatsit']."\", \"".$image."\")") or die(mysql_error());
}
}
}
// Find out about latest image
$gotten = mysql_query("select * from pictures order by pid desc limit 1") or die(mysql_error());
if(mysql_num_rows($gotten)>0)
{
$row = mysql_fetch_assoc($gotten);
$title = htmlspecialchars($row['title']);
$bytes = $row['imgdata'];
}
else
{
$errmsg = "There is no image in the database yet";
$title = "no database image available";
// Put up a picture of our training centre
$instr = fopen("./images.jpg","rb");
$bytes = fread($instr,filesize("./images.jpg"));
}
// If this is the image request, send out the image
if ($_GET['gim'] == 1)
{
header("Content-type: image/jpeg");
print $bytes;
exit ();
}
?>
<html>
<head>
<title>Upload an image to a database</title>
<body bgcolor=white><h2>Here's the latest picture</h2>
<font color=red><?= $errmsg ?></font>
<center><img src=?gim=1><br>
<b><?= $title ?></center>
<hr>
<h2>Please upload a new picture and title</h2>
<form enctype="multipart/form-data" method="post">
<input type="hidden" name="completed" value="1">
Please choose an image to upload: <input type="file" name="imagefile"><br>
Please enter the title of that picture: <input name="whatsit"><br>
then: <input type="submit"></form><br>
</body>
</html>
taken from http://www.eksperten.dk/spm/695439 (danish site)
#4
Posted 22 March 2006 - 04:03 AM
This uses up valuable server resources and is the worst possible idea to do unless you and only a few others plans to use it, but if someone found the location.. they could just refresh over and over until the server blows up
#5
Posted 23 March 2006 - 09:28 AM
HaloprO, on Mar 22 2006, 10:02 AM, said:
This uses up valuable server resources and is the worst possible idea to do unless you and only a few others plans to use it, but if someone found the location.. they could just refresh over and over until the server blows up 
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users
