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]);
}