skyer2000 Posted March 14, 2007 Share Posted March 14, 2007 I have the following code to convert pictures into thumbnails. It creates the thumbnail to the size 200x150 perfectly, but the image is stretched horizontally. Anyone have ideas on how to fix this code so it will not stretch everything? $max_width=200; $max_height=200; $add="../images/temp/$filename.jpg"; $tsrc="$dir/th$filename.jpg"; $im=ImageCreateFromJPEG($add); $width=ImageSx($im); $height=ImageSy($im); if ($width > $height) { $src_w = $src_h = $width; $src_x = 0; $src_y = round(($height - $width) / 2); } else { $src_w = $src_h = $width; $src_x = 0; $src_y = round(($height - $width) / 2); } $newimage=imagecreatetruecolor(200,150); imageCopyResized($newimage,$im,0,0,$src_x,$src_y,200,150,$src_w, $src_h); ImageJpeg($newimage,$tsrc, 70); Link to comment https://forums.phpfreaks.com/topic/42731-solved-cropping-pictures-with-gd-stretching-problem/ Share on other sites More sharing options...
Barand Posted March 14, 2007 Share Posted March 14, 2007 If you want to avoid the stretched look you need to scale the image keeping the original proportions of width to height. try <?php $max_width=200; $max_height=200; $add="../images/temp/$filename.jpg"; $tsrc="$dir/th$filename.jpg"; $im=ImageCreateFromJPEG($add); $width=ImageSx($im); $height=ImageSy($im); /** * get the larger dimension and calc required scale factor * to fit in a 200x200 square */ $size = max($width, $height); $scale_factor = $max_width/$size; $new_w = $width * $scale_factor; $new_h = $height * $scale_factor; $newimage=imagecreatetruecolor($new_w, $new_h); imageCopyResampled($newimage,$im, 0, 0, 0, 0, $new_w, $new_h, $width, $height); ImageJpeg($newimage,$tsrc, 70); ?> Link to comment https://forums.phpfreaks.com/topic/42731-solved-cropping-pictures-with-gd-stretching-problem/#findComment-207366 Share on other sites More sharing options...
skyer2000 Posted March 14, 2007 Author Share Posted March 14, 2007 Is there no way around that though? I need all of the thumbnails to be 200x150, no matter what the proportions. Link to comment https://forums.phpfreaks.com/topic/42731-solved-cropping-pictures-with-gd-stretching-problem/#findComment-207379 Share on other sites More sharing options...
artacus Posted March 14, 2007 Share Posted March 14, 2007 Sure, you're just going to have to figure out if you need to crop from top and bottom or from left and right. $lg_x = 200; $lg_y = 150; $src_img = imagecreatefromjpeg($ph_link); $s_x = imagesx($src_img); $s_y = imagesy($src_img); $temp = imagecreatetruecolor($lg_x, $lg_y); if($s_x / $s_y > $lg_x / $lg_y) { //trim x $x = round(($s_x - ($lg_x * $s_y / $lg_y)) / 2); $y = 0; } else if ($s_x / $s_y > $lg_x / $lg_y) { //trim y $x = 0; $y = round(($s_y - ($lg_y * $s_x / $lg_x)) / 2); } else { $x = 0; $y = 0; } //create large image imagecopyresampled($temp, $src_img, 0, 0, $x, $y, $lg_x, $lg_y, $s_x, $s_y); imagejpeg($temp, $filename); Link to comment https://forums.phpfreaks.com/topic/42731-solved-cropping-pictures-with-gd-stretching-problem/#findComment-207414 Share on other sites More sharing options...
Barand Posted March 14, 2007 Share Posted March 14, 2007 You then have to work it the other way round and grab as big a part of the source image as you can while retaining a 200x150 size ratio <?php $t_width=200; $t_height=150; $add="../images/temp/$filename.jpg"; $tsrc="$dir/th$filename.jpg"; $im=ImageCreateFromJPEG($add); $width=ImageSx($im); $height=ImageSy($im); /** * calc required scale factors */ $scale_h= $height/$t_height; $scale_w= $width/$t_width; /** * get MIN scale factor */ $scale_factor = min($scale_w, $scale_h); $src_w = $t_width * $scale_factor; $src_h = $t_height * $scale_factor; $newimage=imagecreatetruecolor($new_w, $new_h); imageCopyResampled($newimage,$im, 0, 0, 0, 0, $t_width, $t_height, $src_w, $src_h); ImageJpeg($newimage,$tsrc, 70); ?> Link to comment https://forums.phpfreaks.com/topic/42731-solved-cropping-pictures-with-gd-stretching-problem/#findComment-207419 Share on other sites More sharing options...
artacus Posted March 14, 2007 Share Posted March 14, 2007 Barand... first of all, you're too slow (and I type with my tail BTW) And secondly you're still going to stretch his image because you are not "cropping" it to make it fit that his aspect ratio. Link to comment https://forums.phpfreaks.com/topic/42731-solved-cropping-pictures-with-gd-stretching-problem/#findComment-207424 Share on other sites More sharing options...
Barand Posted March 14, 2007 Share Posted March 14, 2007 If his image is portrait format 1000x1500 then scale_w = 5, scale_h = 10 so I use the min value of 5. This gives imageCopyResampled($newimage,$im, 0, 0, 0, 0, 200, 150, 1000, 750); // top half of image If OTOH it's landscape 2000x450 then scale_w = 10, scale_h = 3 so I use the min value of 3. This gives imageCopyResampled($newimage,$im, 0, 0, 0, 0, 200, 150, 600, 450); // left 600 pix of image I'd say those were cropped. Link to comment https://forums.phpfreaks.com/topic/42731-solved-cropping-pictures-with-gd-stretching-problem/#findComment-207433 Share on other sites More sharing options...
artacus Posted March 14, 2007 Share Posted March 14, 2007 I see what you're doing, we're just grabbing different parts of the image. The code I pasted (which is why I was faster than you) was for thumbnailing portraits (head shots) so I always had to pull from the center of the image, so I didn't just grab the top of someone's head. Link to comment https://forums.phpfreaks.com/topic/42731-solved-cropping-pictures-with-gd-stretching-problem/#findComment-207443 Share on other sites More sharing options...
Barand Posted March 14, 2007 Share Posted March 14, 2007 Good idea, in which case I just need to add a couple of lines <?php $t_width=200; $t_height=150; $add="../images/temp/$filename.jpg"; $tsrc="$dir/th$filename.jpg"; $im=ImageCreateFromJPEG($add); $width=ImageSx($im); $height=ImageSy($im); /** * calc required scale factors */ $scale_h= $height/$t_height; $scale_w= $width/$t_width; /** * get MIN scale factor */ $scale_factor = min($scale_w, $scale_h); $src_w = $t_width * $scale_factor; $src_h = $t_height * $scale_factor; /** * to centre the cropped area //added code */ $sx = ($width - $src_w) / 2; $sy = ($height - $src_h) / 2; $newimage=imagecreatetruecolor($t_width, $t_height); // edited imageCopyResampled($newimage,$im, 0, 0, $sx, $sy, $t_width, $t_height, $src_w, $src_h); ImageJpeg($newimage,$tsrc, 70); ?> Link to comment https://forums.phpfreaks.com/topic/42731-solved-cropping-pictures-with-gd-stretching-problem/#findComment-207449 Share on other sites More sharing options...
skyer2000 Posted March 14, 2007 Author Share Posted March 14, 2007 Works great! Thanks guys! Link to comment https://forums.phpfreaks.com/topic/42731-solved-cropping-pictures-with-gd-stretching-problem/#findComment-207511 Share on other sites More sharing options...
skyer2000 Posted March 14, 2007 Author Share Posted March 14, 2007 Ah, when an image that is taller than it is wide, the code crops from the very top of the image, and not near the center, how could that be fixed? Link to comment https://forums.phpfreaks.com/topic/42731-solved-cropping-pictures-with-gd-stretching-problem/#findComment-207532 Share on other sites More sharing options...
Barand Posted March 14, 2007 Share Posted March 14, 2007 Didi you try my code in reply #8 where I added a coupe of lines to centre the cropped area? Link to comment https://forums.phpfreaks.com/topic/42731-solved-cropping-pictures-with-gd-stretching-problem/#findComment-207596 Share on other sites More sharing options...
skyer2000 Posted March 14, 2007 Author Share Posted March 14, 2007 Yeah, it centers it perfectly with images that are wider than they are tall. But images that are taller than they are wide it doesn't center properly. Link to comment https://forums.phpfreaks.com/topic/42731-solved-cropping-pictures-with-gd-stretching-problem/#findComment-207602 Share on other sites More sharing options...
Barand Posted March 15, 2007 Share Posted March 15, 2007 $newimage=imagecreatetruecolor($new_w, $new_h); should be $newimage=imagecreatetruecolor($t_width, $t_height); But apart from that, the vertical centering worked fine when I tested it. Link to comment https://forums.phpfreaks.com/topic/42731-solved-cropping-pictures-with-gd-stretching-problem/#findComment-207627 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.