Question is:
Can one make thumbnails using gd on images stored in MySql?
And if it can be done where can i get such information. It's been 2 days and i've searched from google to yahoo and nothing comes up on the matter.
This is the code im using but it doenst work I know im not a pro but if someone could help me pleaseeeee!!!!!!!!!!!
But it comes out nasty as if i were opening the image with a text editor 🙁
<?
require('passport/conn.inc');
$query = "select image,filetype from images where imageid='$id'";
$result = mysql_query($query) or do_err('Query failed');
$row = mysql_fetch_array($result);
$type = $row["filetype"];
$data = $row["image"];
$img = "$data.$type";
$w = "75";
$h = "75";
$mode = "0";
SetType($mode, 'integer');
SetType($w, 'integer');
SetType($h, 'integer');
SetType($img, 'string' );
function percent($p, $w)
{
return (real)(100 * ($p / $w));
}
function unpercent($percent, $whole)
{
return (real)(($percent * $whole) / 100);
}
// If the user defined a type to use.
if (!isset($type))
{
$ext = explode('.', $img);
$ext = $ext[count($ext)-1];
switch(strtolower($ext))
{
case 'jpeg' :
$type = 'jpg';
break;
default :
$type = $ext;
break;
}
}
// Create the image...
switch (strtolower($type))
{
case 'jpg':
$tmp = imagecreatefromjpeg($img);
break;
case 'gif':
$tmp = @imagecreatefromgif($img);
break;
case 'png':
$tmp = @imagecreatefrompng($img);
break;
default:
echo 'Error: Unrecognized image format.';
exit();
break;
}
if ($tmp)
{
// Resize it
$ow = imagesx ($tmp); // Original image width
$oh = imagesy ($tmp); // Original image height
if ($mode)
{
// Just smash it up to fit the dimensions
$nw = $w;
$nh = $h;
}
else
{
// Make it proportional.
if ($ow > $oh)
{
$nw = $w;
$nh = unpercent(percent($nw, $ow), $oh);
}
else if ($oh > $ow)
{
$nh = $h;
$nw = unpercent(percent($nh, $oh), $ow);
}
else
{
$nh = $h;
$oh = $w;
}
}
$out = imagecreate($nw, $nh);
imagecopyresized($out, $tmp, 0, 0, 0, 0, $nw, $nh, $ow, $oh);
imagedestroy($tmp);
}
else
{
echo 'Could not create image resource.';
exit;
}
if ($out)
{
switch (strtolower($type))
{
case 'jpg':
header('Content-type: image/jpeg');
imagejpeg($out);
break;
case 'gif':
header('Content-type: image/gif');
imagegif($out);
break;
case 'png':
header('Content-type: image/png');
imagepng($out);
break;
}
imagedestroy($out);
}
else
{
echo 'ERROR: Could not create resized image.';
}
?>