php-image-resize icon indicating copy to clipboard operation
php-image-resize copied to clipboard

How can I get Resized Image With

Open sabriunal opened this issue 3 years ago • 2 comments
trafficstars

I use php-image-resize via composer.

I have a crop action wich a bit special for my script.

At some point, I need to know the result of resizeToHeight action as image sizes. But the method does not keep the width result on properties or return as a result.

Here is my example.

<?php

//load composer
require_once './vendor/autoload.php';

use \Gumlet\ImageResize;

$sourceFilename = 'test.jpg';
$destFilename = 'result.jpg';
$imageCropWidth = 202;
$imageCropHeight = 267;

$image = new ImageResize($sourceFilename);

$image->resizeToHeight($imageCropHeight);
if ( $image->getSourceWidth() >= $image->getSourceHeight() )
{
    $image->crop($imageCropWidth, $imageCropHeight);
}
else
{
    //ALERT
    //in this part, I need to know resized image Width
    //$resizedWith = $image->getResizedImageWidth();
    if( $resizedWith > $imageCropWidth )
    {
        $image->crop($imageCropWidth, $imageCropHeight);
    }
}

$image->save($destFilename, IMAGETYPE_JPEG);

sabriunal avatar Jan 11 '22 15:01 sabriunal

I tested again and I found a way wich my help others.

I used ratio calculation as you do.

$resizedWith = (int) ( $imageCropWidth * ( $imageCropHeight / $image->getSourceHeight() ) );

ghost avatar Jan 11 '22 16:01 ghost

As a suggestion, you may implement this calculation as a method.

ghost avatar Jan 11 '22 16:01 ghost

If you can create a PR, I am happy to merge it.

adityapatadia avatar Oct 11 '23 14:10 adityapatadia