开发者

PHP crop tall images

This is my code, but I dont know why but when I upload a vertical image it is resized and I get two black stripes on the sides. With wide images it works well most of the time but with some images I get black stripes as well. How can I fix it?

My purpose is to crop every image, horizontal just a hint to fit my box, and vertical I want to crop so the width is the same and they are cut no top and bottom.

$thumb_height  = 200;
$thumb_width   = 300;

    $thumb = imagecreatetruecolor($thumb_width, $thumb_height);
    if($width >= $height) {
       // If image is wider than thumbnail (in aspect ratio sense)
       $new_height = $thumb_height;
       $new_width = $width / ($height / $thumb_height);
    } else {
       // If the thumbnail is wider than the image
       $new_width = $thumb_width;
       $new_height = $height / ($width / $thumb_width);
    }
    $output_filename_mid = 'uploads/'.IMG_L.$fileName;      
    imagecopyresampled($thumb,
                       $image,
                       0 - ($new_width - $thumb_width) / 2, 开发者_如何学JAVA// Center the image horizontally
                       0 - ($new_height - $thumb_height) / 2, // Center the image vertically
                       0, 0,
                       $new_width, $new_height,
                       $width, $height);
    imagejpeg($thumb, $output_filename_mid, 85);


You're almost there. You've figured out that you need to determine the ratio between the old height and the destination height to resize the side that'll be cropped. However, you need to determine this in relation to the destination ratio.

if(($width / $height) > ($thumb_width / $thumb_height)) {
   // If image is wider than thumbnail (in aspect ratio sense)
   $new_height = $thumb_height;
   $new_width = $width / ($height / $thumb_height);
} else {
   // If the thumbnail is wider than the image
   $new_width = $thumb_width;
   $new_height = $height / ($width / $thumb_width);
}


I'm actually working with this a lot. Here is my code which is VERY similar to yours. Can you find the difference/problem yourself?

My code works 100% and does exactly what you need and ONLY that. Feel free to use it!

Variables explained: $imagewidth and $imageheight are naturally the original pixel sizes of the input image. $crop_ratio_w is crop ratio width and $crop_ratio_h is crop ratio height.

So for my code vs. your code: $crop_ratio_w = $thumb_width and $crop_ratio_h = $thumb_height

//do this if we can start cropping by width (there's enough space on height)
   if (($imagewidth * $crop_ratio_h / $crop_ratio_w) < $imageheight){
      //count new res
         $new_width = $imagewidth;
         $new_height = ($imagewidth * $crop_ratio_h / $crop_ratio_w);
      //count the height difference, so that new image is cropped in HEIGHT center
         $difference = ($imageheight - $new_height);
         $y_offset = ($difference / 2);
      //create new empty image
         $croppedImage = imagecreatetruecolor($new_width, $new_height);
      //copy wanted area to the new empty image
         imagecopy($croppedImage, $originalImage, 0, 0, 0, $y_offset, $new_width, $new_height);
   }
//else on previous condition -- do this if we have to start cropping by height (there's not enough space on height)
   else{
      //count new res
         $new_width = ($imageheight * $crop_ratio_w / $crop_ratio_h);
         $new_height = $imageheight;
      //count the height difference, so that new image is cropped in WIDTH center
         $difference = ($imagewidth - $new_width);
         $x_offset = ($difference / 2);
      //create new empty image
         $croppedImage = imagecreatetruecolor($new_width, $new_height);
      //copy wanted area to the new empty image
         imagecopy($croppedImage, $originalImage, 0, 0, $x_offset, 0, $new_width, $new_height);
   }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜