NYCPHP Meetup

NYPHP.org

[nycphp-talk] Creatings Thumbnails with GD

alex at pilgrimstudio.com alex at pilgrimstudio.com
Wed Nov 23 14:01:52 EST 2005


> Wow, that's weird - that looks almost exactly like some code we just wrote
> for a project.
>
>
> function ConvertImageToThumbnail($inPath, $outPath) {
>     // Convert the image at the path specified by $inPath
>     // to a thumbnail image at the path specified by $outPath.
>
> 	// Get the width and height of the actual image.
>     list($width, $height) = getimagesize($inPath);
>
> 	// How big should the thumbnail be?
>     if( (119.0/$width) > (119.0/$height) )
>         $scale = 119.0/$height;
>     else
>         $scale = 119.0/$width;
>
>     $newwidth = $width * $scale;
>     $newheight = $height * $scale;
>
> 	// Create a new thumbnail image of the correct size.
>     $thumb = imagecreatetruecolor($newwidth, $newheight);
>
> 	// Load the original image.
>     $source = imagecreatefromjpeg($inPath);
>
>
> imagecopyresampled($thumb,$source,0,0,0,0,$newwidth,$newheight,$width,$heigh
> t);
>
> 	// Write out the thumbnail as a JPEG.
>     imagejpeg($thumb, $outPath);
> }  // ConvertImageToThumbnail
>
>

it looks like we all using almost the same code.


header("Content-type: image/jpeg");
$image = imagecreatefromjpeg($filename);
if(isset($height) && !isset($width))
{
	$width=imagesx($image)/imagesy($image)*$height;
}
else if(isset($width) && !isset($height))
{
	$height=imagesy($image)/imagesx($image)*$width;
}

$im_thumb=imagecreatetruecolor($width,$height);
imagecopyresampled($im_thumb,
$image,0,0,0,0,$width,$height,imagesx($image),imagesy($image));
imagejpeg($im_thumb);
imagedestroy($im_thumb);




More information about the talk mailing list