PHP / Image Manipulation / thumbnailing on the fly
Snippet details
ID: 512
Viewed: 1653
Added: 2003-09-30
Version: 1
Sorry no demo
here is a short little script that will take an image and create a thumbnail view of the same image. this will not just resize it but it creates a new image and makes it the correct size so it actually leaves the default image alone.
Browsers: all
GD Library: 2.0+
Browsers: all
GD Library: 2.0+
General Details
Snippet uploaded by: snippet
Email : webmaster@snippetlibrary.com
Snippet By: snippet
<!---Head--->
none
<!---Body--->
<?php
$error = False;
$img_size = 20;
$image = "someimage.jpg";
// check if the 'image' parameter has been given
if (!isset($image)){ // 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
}
$thumbimage =ImageCreateTrueColor($w, $h);
ImageCopyResampled($thumbimage, $bigimage, $dx, $dy, 0, 0, $w, $h, $x, $y);
imagejpeg($tnimage);
// release the memory used by the thumbnail image
ImageDestroy($thumbimage);
// release the memory used by the original big image
ImageDestroy($bigimage);
?>
No Reviews to show
