0

For an image resize tool I have a piece of code that should crop the image. Problem with this is that it doesn't resize it first.

Let's say we want to resize an image of 2000x2000 and the final sizes end up to be 50x20, I get a small piece of the center of the image leaving many content of the image being destroyed. Instead I want the 2000x2000 first to be resized, while maintaining it's original ratio and then crop to make sure the image isn't stretched (to delete the remaining 30px on the sides).

My current code looks as follows:

if((isset($options['crop']) && $options['crop'] == true) && ($maxWidth <= $oldWidth && $maxHeight <= $oldHeight)) {
    $cx = $oldWidth / 2;
    $cy = $oldHeight / 2;
    $x = $cx - $maxWidth / 2;
    $y = $cy - $maxHeight / 2;
    if ($x < 0) $x = 0;
    if ($y < 0) $y = 0;
    $thumb = imagecrop($source, ['x' => $x, 'y' => $y, 'width' => $maxWidth, 'height' => $maxHeight]);
}
IMarks
  • 187
  • 11
  • @deceze could you please remove the duplicate marker. It's totally not related as i'm asking for something far more specific. That question is regarding the size calculation, but my question is regarding static requested sizes. This question is regarding cropping in combination with resizing while yours cropping is not included. Please read more carefully before falsely marking posts as duplicates. – IMarks Sep 19 '17 at 11:32
  • The duplicate contains the full code to resize *and crop* an image to an arbitrary size. Have you fully comprehended what the code in the duplicate does? If so, please more specifically describe how your requirement differs. – deceze Sep 19 '17 at 11:48

0 Answers0