Brett
Brett

Reputation: 20049

Cropping an image from the center into the largest square you can make in Yii2 Imagine?

I'm using the Yii2 extension Imagine and I need to make 150x150 images from user uploads.

Currently I am just doing something like this:

use yii\imagine\Image;

....

Image::thumbnail($save_path, $img_size, $img_size)->save($save_path);

Obviously this can cause issues if one of the dimensions is < 150px once resized.

So what I'm trying to primarily figure out is how to crop the image into a square before it is resized, so that when I resize it there won't be any aspect ratio issues.

Now, I know you can crop the image with something like:

Image::crop($save_path, $img_size, $img_size, [5, 5]);

But problem is doing this before resizing the image will likely not give you what you want since the image may be so large and cropping it after resizing won't work either as one dimension may already have been reduced to < 150px.

So what I'm trying to work out is how can I crop the image before resizing to the maximum size square possible and from the center out?

Edit:

Ok, I have worked out a way to handle this, but was wondering if there was anyway to accomplish the below easily or will I need to code it myself?

Upvotes: 1

Views: 3026

Answers (2)

Ayell
Ayell

Reputation: 620

Another try :p

<?php

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

// ...

$thumbnail = Image::thumbnail($save_path, $img_size, $img_size);
$size = $thumbnail->getSize();
if ($size->getWidth() < $img_size or $size->getHeight() < $img_size) {
    $white = Image::getImagine()->create(new Box($img_size, $img_size));
    $thumbnail = $white->paste($thumbnail, new Point($img_size / 2 - $size->getWidth() / 2, $img_size / 2 - $size->getHeight() / 2));
}
$thumbnail->save($save_path);

Upvotes: 3

Ayell
Ayell

Reputation: 620

Can't you just use the fourth parameter of Image::thumbnail()?

Image::thumbnail($save_path, $img_size, $img_size, Image\ImageInterface::THUMBNAIL_INSET)->save($save_path);

From http://www.yiiframework.com/doc-2.0/yii-imagine-baseimage.html#thumbnail()-detail:

If thumbnail mode is ImageInterface::THUMBNAIL_INSET, the original image is scaled down so it is fully contained within the thumbnail dimensions. The rest is filled with background that could be configured via yii\imagine\Image::$thumbnailBackgroundColor and yii\imagine\Image::$thumbnailBackgroundAlpha.

Upvotes: 0

Related Questions