Я разработал простой код для поддержания соотношения сторон изображения.
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]
);
}
Вы можете использовать его, как:
(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,
]);
Да, это то, что я только что сделал! :) 'use Imagine \ Gd; использовать Imagine \ Image \ Box; использовать Imagine \ Image \ BoxInterface; ... $ photo = $ imagine-> open ($ path); $ photo-> thumbnail (new Box (1200, 1200)) -> save ($ path, ['quality' => 90]); ' Благодарим за ответы! – ps202
Ошибка Class 'app \ controllers \ Image' не найден? –