3

I got this code in yii2:

Image::thumbnail($path, 100, 100)->save($thumbnail, ['quality' => 50]);

I thought that it will resize the original image maintaining the aspect ratio. But it just creates a box... What can be wrong?

ps202
  • 95
  • 2
  • 11

3 Answers3

6

You may use like that:

use Imagine\Image\Box;

Image::frame($path)
->thumbnail(new Box(100, 100))
->save($thumbnail, ['quality' => 50]);
vitalik_74
  • 4,573
  • 2
  • 19
  • 27
  • 1
    Yes, that's what I just did! :) `use Imagine\Gd; use Imagine\Image\Box; use Imagine\Image\BoxInterface; ... $photo = $imagine->open($path); $photo->thumbnail(new Box(1200, 1200))->save($path, ['quality' => 90]);` Thank you for the quick responses! – ps202 May 18 '15 at 21:11
1

I am using like this in Yii2 and it works fine. It also keeps the aspect ratio perfect releted to width.

use yii\imagine\Image;
use Imagine\Image\Box;

 ...

$imagine = Image::getImagine()
->open($resizeImagePath)
->thumbnail(new Box(120, 120))
->save($thumbnailImagePath, ['quality' => 90]);
Jai
  • 151
  • 1
  • 7
0

I have made a simple code to maintain the aspect ratio of an image.

use yii\imagine\Image;

.................................................................

public function doResize($imageLocation, $imageDestination, Array $options = null)
{
    $newWidth = $newHeight = 0;
    list($width, $height) = getimagesize($imageLocation);

    if(isset($options['newWidth']) || isset($options['newHeight']))
    {
        if(isset($options['newWidth']) && isset($options['newHeight']))
        {
            $newWidth = $options['newWidth'];
            $newHeight = $options['newHeight'];
        }

        else if(isset($options['newWidth']))
        {
            $deviationPercentage = (($width - $options['newWidth']) / (0.01 * $width)) / 100;

            $newWidth = $options['newWidth'];
            $newHeight = $height - ($height * $deviationPercentage);
        }

        else
        {
            $deviationPercentage = (($height - $options['newHeight']) / (0.01 * $height)) / 100;

            $newWidth = $width - ($width * $deviationPercentage);
            $newHeight = $options['newHeight'];
        }
    }

    else
    {
        // reduce image size up to 20% by default
        $reduceRatio = isset($options['reduceRatio']) ? $options['reduceRatio'] : 20;

        $newWidth = $width * ((100 - $reduceRatio) / 100);
        $newHeight = $height * ((100 - $reduceRatio) / 100);
    }

    return Image::thumbnail(
        $imageLocation, 
        (int) $newWidth, 
        (int) $newHeight
    )->save(
        $imageDestination,
        ['quality' => isset($options['quality']) ? $options['quality'] : 100]
    );
}

You can use it, like:

(new TheClass)->doResize($imageLocation, $imageDestination, [
    'quality' => 70,
    'reduceRatio' => 50
]);

// or like this
(new TheClass)->doResize($imageLocation, $imageDestination, [
    'quality' => 70,
    'width' => 500,
]);

// or like this
(new TheClass)->doResize($imageLocation, $imageDestination, [
    'quality' => 70,
    'height' => 500,
]);

// and like this (but will break the aspect ratio)
(new TheClass)->doResize($imageLocation, $imageDestination, [
    'quality' => 70,
    'width' => 100
    'height' => 500,
]);
  • That's cool, but why bother if you alreasy have the box method? – ps202 Sep 15 '16 at 05:13
  • that's cool but there is an error in your examples. have to be: `(new TheClass)->doResize($imageLocation, $imageDestination, [ 'quality' => 70, 'newWidth' => 100 'newHeight' => 500, ]);` – Gabriele Carbonai Dec 26 '16 at 12:04