简体   繁体   中英

PHP Intervention Image Resize image to fit shortest side to aspect ratio

How do I resize an image using Intervention Image maintaining the aspect ratio but making the image's shortest side fit the desired resize ratio.

Eg a 800x400 image resized to fit 100x100 will be resized to 200x100

I tried this:

$image->resize($width, $height, function ($constraint) {
    $constraint->aspectRatio();
});

But it resizes the longest side to fit (eg 100x50).

Set width as null :

$height = 100;
$image = Image::make('800x400.jpg')->resize(null, $height, function ($constraint) {
    $constraint->aspectRatio();
});
$image->save('200X100.jpg', 60);

Programatically speaking, just find which side is larger and set it to null , ie:

$width = 100;
$height = 100;
$image = Image::make('400x800.png');
$image->width() > $image->height() ? $width=null : $height=null;
$image->resize($width, $height, function ($constraint) {
    $constraint->aspectRatio();
});
$image->save('100x200.jpg', 60);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM