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

Some cropping help

Discussion in 'XenForo Development Discussions' started by Floren, May 12, 2014.

  1. Floren

    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: May 12, 2014
  2. Floren

    Floren Well-Known Member

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

Share This Page