How to crop a part of the image? If Maximum Size = The original size is working fine, but it does not work :(

Original photo size 300x423px
Maximum size (Photo size for cropping) 213x300px

$filename = '/test.jpg'; $image = imagecreatefromjpeg($filename); $x = 0; $y = 0; $width = 213; $height = 213; $new_width = 200; $new_height = 200; $thumb = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($thumb, $image, 0, 0, $x, $y, $new_width, $new_height, $width, $height); header('Content-Type: image/jpeg'); imagejpeg($thumb, null, 90); 

Photos before and after trimming. enter image description here

Photos should be like this.
enter image description here

  • And this code for these sizes or left took just can not substitute the necessary numbers? - Jean-Claude
  • for example, it’s not quite clear to me .. should I first reduce the photo to the maximum width, and only then cut it? - Jean-Claude
  • @ Jean-Claude updated the question - KYRAN

1 answer 1

So find out the size of the picture first:

 $size=getimagesize($filename) $width = $size[0]; $height = $size[1]; 

You have the size of the downloaded image is registered in the code when it should be obtained from the downloaded file.

http://php.net/manual/ru/function.getimagesize.php

  • Thanks, I know about it. For example, if the width is 300px then x = 15, y = 15 and if the width is 213px then x =?, Y =?. Different photos, different x, y (expand) - KYRAN