deansatch Posted March 23, 2010 Share Posted March 23, 2010 I currently have this little bit of code which successfully resizes an image down then crops it square. I want it to crop the center of the image but as a set sized rectangle (220x160) but can't seem to figure it out. $thumb_sizex = 220; $thumb_sizey = 220; $size = getimagesize($source); $width = $size[0]; $height = $size[1]; if($width > $height) { $x = ceil(($width - $height) / 2 ); $width = $height; } elseif($height > $width) { $y = ceil(($height - $width) / 2); $height = $width; } $new_im = ImageCreatetruecolor($thumb_sizex,$thumb_sizey); imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_sizex,$thumb_sizey,$width,$height); imagejpeg($new_im,$dest2,100); If I change thumb_sizey to 160 it just stretches the image. It does work on landscape pics but not on portrait. Any help appreciated. NOTE: I don't want to simply shrink it down with a maximum width of 220 or a maximum height of 160. I want the finished dimensions to be 220x160 and crop off any excess. Link to comment https://forums.phpfreaks.com/topic/196236-create-non-square-thumbnail/ Share on other sites More sharing options...
litebearer Posted March 23, 2010 Share Posted March 23, 2010 this thread may help http://www.phpfreaks.com/forums/index.php/topic,290840.msg1377599.html#msg1377599 Link to comment https://forums.phpfreaks.com/topic/196236-create-non-square-thumbnail/#findComment-1030530 Share on other sites More sharing options...
deansatch Posted March 23, 2010 Author Share Posted March 23, 2010 That seems to be a thread about creating a square thumbnail which I have already done. I wam trying to figure out how to create a set-sized rectangle. Link to comment https://forums.phpfreaks.com/topic/196236-create-non-square-thumbnail/#findComment-1030541 Share on other sites More sharing options...
litebearer Posted March 23, 2010 Share Posted March 23, 2010 If you look at the script I provided in that thread, it allows you to set the dimensions of the crop. You have the ability to make it square OR rectangular AND to position where ever you chose on the original Link to comment https://forums.phpfreaks.com/topic/196236-create-non-square-thumbnail/#findComment-1030554 Share on other sites More sharing options...
deansatch Posted March 23, 2010 Author Share Posted March 23, 2010 I need it to resize before cropping though. The code on that thread will just crop a portion of the full size image. Link to comment https://forums.phpfreaks.com/topic/196236-create-non-square-thumbnail/#findComment-1030568 Share on other sites More sharing options...
litebearer Posted March 23, 2010 Share Posted March 23, 2010 Ok, step by step... 1. Use your code to resize the image. At this point you have created an image to the size you want and have saved it somewhere. 2. Run my script to take your resized image and crop the part you want. Now you have the resized image and the cropped image Make sense? Link to comment https://forums.phpfreaks.com/topic/196236-create-non-square-thumbnail/#findComment-1030570 Share on other sites More sharing options...
deansatch Posted March 23, 2010 Author Share Posted March 23, 2010 Thanks anyway. I managed to do it in the end with this: $size = getimagesize($source); $image_width = $size[0]; $image_height = $size[1]; $dest_x = 220; $dest_y = 160; if ($image_width > $dest_x or $image_height > $dest_y) { if ($image_height >= $image_width){$new_image_width = $dest_x; $new_image_height = $image_height *($dest_x/$image_width);} else {$new_image_width = $image_width *($dest_y/$image_height); $new_image_height = $dest_y;} } else {$new_image_width = $image_width; $new_image_height = $image_height;} $new_image = imagecreatetruecolor(220,160); imagecopyresampled($new_image, $im ,0, 0, 0, 0, $new_image_width, $new_image_height, $image_width, $image_height); imagejpeg($new_image, $dest, 100); Link to comment https://forums.phpfreaks.com/topic/196236-create-non-square-thumbnail/#findComment-1030571 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.