• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Some cropping help


Well-known member
Hi guys,

I need some help with the cropping logic for specific images. Basically, I want the image to be exactly 1024x576px. For the width, I can do this easy with the thumbnail() function but my goal is to actually crop the image after to exactly 576px.

Thinking out loud:
$dimensions = array(
    'width' => 1024,
    'height' => 576
$ratio = ($image->getWidth() / $image->getHeight());
if ($ratio > ($dimensions['width'] / $dimensions['height']))
    // crop top and bottom only, to 576px
If an user uploads a 1300x2000px image, the thumbnail() function will fix the width to 1024px and crop() with adjust the image height proportionally to 576px. Any better methods or suggestions are welcome.

Thank you for your help.
Last edited:


Well-known member
I got it fixed, here it is the logic for someone who wants to know:
if ($image->getHeight() > $maxDimensions['height'])
    $cropY = floor(($image->getHeight() - $maxDimensions['height']) / 2);
    $image->crop(0, $cropY, $maxDimensions['width'], $maxDimensions['height']);