1

I know this subject seems to appear frequently on this site but nobody seems to have the answer to the problem. I do have a script that rezises an image to show it as a thumbnail of size 200x200px. The image is rezized correctly but when you take a look at the output image you can see that the real size of the thumb is the same ratio as the original and a black stripe fills the rest of the space. Any idea to prevent this and make a REAL crop of the image?

foreach( $images as $image ) {
$dn = dirname($image);
$thumbsDir = $dn; // path to the thumbnails destination directory

$imageName = "thumb.jpg"; // returns "cheeta.jpg"
$thumbnail = $thumbsDir."/".$imageName; // thumbnail full path and name, i.e "./gallery/thumbs/cheeta.jpg"
// for each image, get width and height
$imageSize = getimagesize( $image ); // image size 
$imageWidth = $imageSize[0];  // extract image width 
$imageHeight = $imageSize[1]; // extract image height
// set the thumb size
if( $imageHeight > $imageWidth ){
// images is portrait so set thumbnail width to 100px and calculate height keeping aspect ratio
$thumbWidth = 200;
$thumbHeight = floor( $imageHeight * ( 200 / $imageWidth ) );           
$thumbPosition  = "margin-top: -" . floor( ( $thumbHeight - 200 ) / 2 ) . "px; margin-left: 0";
} else {
// image is landscape so set thumbnail height to 100px and calculate width keeping aspect ratio
$thumbHeight = 200;
$thumbWidth = floor( $imageWidth * ( 200 / $imageHeight ) ); 
$thumbPosition  = "margin-top: 0; margin-left: -" . floor( ( $thumbWidth - 200 ) / 2 ) . "px";
} // END else if
// verify if thumbnail exists, otherwise create it
if ( !file_exists( $thumbnail ) ){
$createFromjpeg = imagecreatefromjpeg( $image );
$thumb_temp = imagecreatetruecolor( $thumbWidth, $thumbHeight );
imagecopyresampled( $thumb_temp, $createFromjpeg, 0 - ($thumbWidth - 200) / 2, 00 - ($thumbHeight - 200) / 2, 0, 0, $thumbWidth, $thumbHeight, $imageWidth, $imageHeight );
imagejpeg( $thumb_temp, $thumbnail, 80 );
} // END if()

Thank you for your help!

See linked image as reference https://i.stack.imgur.com/U743p.jpg

Outline
  • 63
  • 1
  • 8
  • Do you want the uploaded image to be resized properly ? I mean no Croping of the image, because your title says crop and your script explain resizing of the image. Do you want your image to be resized ?? – Nepal12 Mar 12 '15 at 15:48
  • I want my image to be resized to keep the original aspect ratio and then cropped to fit 200 x 200px. Example: an image of 800 x 600 would be resized to 266 x 200 and then cropped to 200 x 200 from the middle ((266 - 200)/2), so 33px would be erased on each side. – Outline Mar 12 '15 at 17:31
  • Is the size that you have give constant, or is there some dynamic resizing. Because "resizing and image keeping the aspect" has tons of output. – Nepal12 Mar 12 '15 at 18:51
  • Please see the following : http://stackoverflow.com/questions/16774521/scale-image-using-php-and-maintaining-aspect-ratio and http://stackoverflow.com/questions/6891352/crop-image-from-center-php – Nepal12 Mar 12 '15 at 19:02
  • In two above links, there are solutions for resizing image and croping image from center. – Nepal12 Mar 12 '15 at 19:03

0 Answers0