Some cropping help

Floren

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:
Code:
$dimensions = array(
    'width' => 1024,
    'height' => 576
);
$image->thumbnail($dimensions['width']);
$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:
I got it fixed, here it is the logic for someone who wants to know:
Code:
$image->thumbnail($maxDimensions['width']);
if ($image->getHeight() > $maxDimensions['height'])
{
    $cropY = floor(($image->getHeight() - $maxDimensions['height']) / 2);
    $image->crop(0, $cropY, $maxDimensions['width'], $maxDimensions['height']);
}
 
Top Bottom