Jump to content

Crop various size images, but in the center?


scooter41

Recommended Posts

Hi There!

 

I have been using image resizing for a while now in php, but have only been able to crop images at their 0,0 position (so always from the top left) which results in odd thumbnails sometimes. What I would like to do is crop in the middle of the image, so calculate the centre of the image accordingly, but never managed to do this! I often use various sized images, so it has to be calculated for both landscape and portrait ratios

 

The script I am using to date is as follows, which bascially crops the top left to an image 126x105

 

$width = 126;$height =105;

 

$new_y = $height;

$new_x = $width_orig/($height_orig/$height);

 

if($new_x < $width){

$new_x = $width;

$new_y = $height_orig/($width_orig/$width);

}

 

 

 

$image_resized=imagecreatetruecolor($width, $height);

imagecopyresampled($image_resized, $image,0, 0, 0, 0, $new_x, $new_y, $width_orig, $height_orig);

 

$image_resizedCrop=imagecreatetruecolor(126, 105);

imagecopyresampled($image_resizedCrop, $image_resized, 0, 0, 0, 0, $width, $height, $width, $height);

 

ImageJPEG ($image_resizedCrop, "../prodImages/med/".$prodID."/".$counter.".jpg", 87);

 

 

 

Is there an easier way to do this and calculate the centre of the image ?

 

Thanks for any help in advance!

Might take a look here:

 

http://www.x-pose.org/blog/96/

 

quote from site -

 

The script locates the center of the image and cuts out a square of the assigned pixel size. Keep in mind that this effectively “cuts out” other sections of the source image to create the thumbnail. As a result, this script will work flawlessly with landscape images but needs some tweaking when it comes to portrait images.

Lite...

 

thanks litebearer, thats a great link.

 

I managed to get a little nearer using the 2nd script on that page:

 

 

 

$width = 126;$height =105;

 

 

///--------------------------------------------------------

//setting the crop size

//--------------------------------------------------------

if($width_orig > $height_orig) $biggestSide = $width_orig;

else $biggestSide = $height_orig;

 

//The crop size will be half that of the largest side

$cropPercent = .5;

$cropWidth  = $biggestSide*$cropPercent;

$cropHeight  = $biggestSide*$cropPercent;

 

 

//getting the top left coordinate

$c1 = array("x"=>($width_orig-$cropWidth)/2, "y"=>($height_orig-$cropHeight)/2);

 

 

$image_resized = imagecreatetruecolor($width, $height);

imagecopyresampled($image_resized, $image, 0, 0, $c1['x'], $c1['y'], $width, $height, $cropWidth, $cropHeight);

 

 

It works, and is a lot better than my current scenario, but it looses content top and bottom, in an ideal world I would like to have the smallest edge, then trip either top and bottom, or left and right, then keep the most amount of image detail.... where as this crops round all sides....

 

But still a lot better than previous so thanks!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.