How to use imagine ( crop, thumb, effects for images ) on Yii2

You are viewing revision #12 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version or see the changes made in this revision.

« previous (#11)next (#13) »

  1. Installation
  2. Examples of use
  3. Resources

Imagine adds most common image functions and also acts as a wrapper to Imagine image manipulation library.

Installation ¶

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist yiisoft/yii2-imagine "*"

or add

"yiisoft/yii2-imagine": "*"

to the require section of your composer.json and run:

php composer.phar update

Examples of use ¶

To try this examples please place any photo in web/img/test-photo.jpg and see the result on runtime folder.

Crop ¶
use yii\imagine\Image;

Image::crop(Yii::getAlias('@webroot/img/text-photo.jpg'))
->save(Yii::getAlias('@runtime/crop-photo.jpg'), ['quality' => 80]);
Thumbnail ¶
use yii\imagine\Image;

Image::thumbnail('@webroot/img/test-photo.jpg', 120, 120)
    ->save(Yii::getAlias('@runtime/thumb-test-photo.jpg'), ['quality' => 80]);
Effects ¶

More effects

Grayscale ¶
use yii\imagine\Image;

$image = yii\imagine\Image::getImagine();
$newImage = $image->open(Yii::getAlias('@webroot/img/test-photo.jpg'));

$newImage->effects()->grayscale();

$newImage->save(Yii::getAlias('@runtime/grayscale-test-photo.jpg'), ['quality' => 80]);
Blur ¶

Required Imagick or Gmagick php extension

use yii\imagine\Image;

$image = yii\imagine\Image::getImagine();
$newImage = $image->open(Yii::getAlias('@webroot/img/test-photo.jpg'));

$newImage->effects()->blur(3);

$newImage->save(Yii::getAlias('@runtime/blur-test-photo.jpg'), ['quality' => 80]);

Resources ¶