M
M
MOTORIST2014-03-23 18:51:56
Yii
MOTORIST, 2014-03-23 18:51:56

yii2 imagine. How to crop an image proportionally in width?

BaseImage::thumbnail($_FILES["attachment"]["tmp_name"]["file"],1000,0)->save($folder.$_FILES["attachment"]["name"]["file"]);

You need to crop the image proportionally to the width. If you set the height to 0, then an error appears. If you do not set - also an error:
exception 'Imagine\Exception\InvalidArgumentException' with message 'Length of either side cannot be 0 or negative, current size is 1000x0'

If you set the height, then everything works.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mike Grigorieff, 2015-01-10
@MOTORIST

$img = imagine\Image::getImagine()->open(Yii::getAlias($fullName));

$size = $img->getSize();
$ratio = $size->getWidth()/$size->getHeight();

$width = 200;
$height = round($width/$ratio);

$box = new Box($width, $height);
$img->resize($box)->save($filePath.'/thumb/' . $fileName);

// OR:

imagine\Image::thumbnail($fullName,$width,$height)
        ->save($filePath.'/thumb/' . $fileName);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question