GD Thumbnailing Probs
Posted: Wed Sep 03, 2003 7:23 pm
Hi,
I am writing an eCommerce site for a client and when showing products i wish to show a thumbnail which is generated on the fly from a picture. It works, but not totally correctly. This is my code (I borrowed it from somewhere and tweaked it slightly)
The code is called like this:
And this is the result:
This is what it should look like:
Any ideas where i might be going wrong?
Thanks for the help
-Pointybeard
I am writing an eCommerce site for a client and when showing products i wish to show a thumbnail which is generated on the fly from a picture. It works, but not totally correctly. This is my code (I borrowed it from somewhere and tweaked it slightly)
Code: Select all
<?php
$error = False;
$system = explode(".", $image);
// check if the 'image' parameter has been given
if (!isset($image)) // if not generate an error
$error = True;
// check if the 'img_size' parameter has been given
if (!isset($img_size) && ($resize != 'no')) // if not generate an error
$error = True;
if($resize != 'no') {
// ensure that $img_size is an integer
$img_size = (integer) $img_size;
// check that the size asked for is sensible
if ($img_size < 20)// if not generate an error
$error = True;
}
if(!$bigimage=@imagecreatefromjpeg($image))
$error = true;
if($error){ // generate an error image
$img = ImageCreate(123, 100);
$bg = ImageColorAllocate($img, 255, 255, 255);
$white = ImageColorAllocate($img, 255, 0, 0);
ImageString($img, 5, 30, 20, "Picture", $white);
ImageString($img, 5, 45, 40, "Not", $white);
ImageString($img, 5, 25, 60, "Available", $white);
ImagePNG($img);
ImageDestroy($img);
exit();
}
$x=imageSX($bigimage);
$y=imageSY($bigimage);
if($resize == 'no' || (($x > $y) && ($x < $img_size)) || (($y > $x) && ($y < $img_size))){
imagejpeg($bigimage);
exit();
}
// find the larger dimension
if ($x>$y) { // if it is the width then
$dx = 0; // the left side of the new image
$w = $img_size; // the width of the new image
$h = ($y / $x) * $img_size; // the height of the new image
$dy = 0; // the top of the new image
} else { // if the height is larger then
$dy = 0; // the top of the new image
$h = $img_size; // the height of the new image
$w = ($x / $y) * $img_size; // the width of the new image
$dx = 0; // the left edgeof the new image
}
$tnimage = ImageCreate($w, $h);
ImageCreateTrueColor($w, $h);
ImageCopyResampled($tnimage, $bigimage, $dx, $dy, 0, 0, $w, $h, $x, $y);
// copy the resized version into the thumbnal image
//ImageCopyResized($tnimage, $bigimage, $dx, $dy, 0, 0, $w, $h, $x, $y);
imagejpeg($tnimage);
// release the memory used by the thumbnail image
ImageDestroy($tnimage);
// release the memory used by the original big image
ImageDestroy($bigimage);
?>The code is called like this:
Code: Select all
<img border=0 src="_scripts/image.php?image=../_images/products/$recїID].jpg&img_size=123" />And this is the result:
This is what it should look like:
Any ideas where i might be going wrong?
Thanks for the help
-Pointybeard