Jump to content

create non-square thumbnail


deansatch

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.