GD Library to Make Thumbnail Out of Part of Image

I want to make a thumbnail that is 256x156. The poster I’m making a thumbnail out of is about 590x911.

I want to size the poster down to 256xH in proper proportions, and then cut out a chunk that is 256x156.

I tried


$imagesource = '/image.jpg';

$thumbwidth = 256;
$thumbheight = 156;

$image = @imagecreatefromjpeg($imagesource);

$imagewidth = imagesx($image);
$imageheight = imagesy($image);

$newheight = ($imageheight/$imagewidth) * $thumbwidth; //get proportion height

$thumb = @imagecreatetruecolor($thumbwidth,$thumbheight);

//now resize source image to 256x$newheight while placing in 256x156 window
imagecopyresampled($thumb, $image, 0, 0,0, 0, $thumbwidth, $thumbheight,  $thumbwidth, $newheight);

imagejpeg($thumb);
imagedestroy($image);
imagedestroy($thumb);


That doesn’t seem to be doing the trick. What am I overlooking?

All help appreciated!
Ryan

you’re trying to do too much in one command for what you described.

Load the image.
Figure out sizes.
Resize it to 256xH.
Crop it to 256x156.

You’ve tried to do the last 2 steps at once.

You are right. Figured it out:


$imagesource = '/image.jpg';

$thumbwidth = 256;
$thumbheight = 156;

$image = @imagecreatefromjpeg($imagesource);
$imagewidth = imagesx($image); 
$imageheight = imagesy($image);

//new dimensions
$new_width = $thumbwidth;
$new_height = round(($imageheight/$imagewidth) * $thumbwidth, 0, PHP_ROUND_HALF_DOWN);

$image_p = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $imagewidth, $imageheight);

$thumb = @imagecreatetruecolor($thumbwidth,$thumbheight);

$checkheight = $new_height - $thumbheight;
if($checkheight > 66) {
imagecopyresampled($thumb, $image_p, 0, 0, 0, 60, $thumbwidth, $thumbheight, $new_width, $thumbheight);
} else {
imagecopyresampled($thumb, $image_p, 0, 0, 0, 0, $thumbwidth, $thumbheight, $new_width, $thumbheight);
}

//now resize source image to 256x$newheight while placing in 256x156 window
imagecopyresampled($thumb, $image, 0, 0,0, 0, $thumbwidth, $thumbheight,  $thumbwidth, $newheight);

imagejpeg($thumb);
imagedestroy($image_p);
imagedestroy($image);
imagedestroy($thumb);

That did the trick.

Cheers!
Ryan