SoFunction
Updated on 2025-03-10

Installation and use of Yii2 third-party library plug-in Imagine

Imagine

Imagine is an object-oriented PHP class library for image operations. This class library can handle some commonly used operations such as: resizing, cropping, applying filters, etc.

Its Color class library can be used to generate RGB values ​​for any color pair. It also provides some methods to draw figures such as arcs, ellipses, lines, slices, etc.

In addition, a flexible font class can be used to load any font file and then insert text into the picture.

Imagine is a class library that will be updated frequently, and will also implement functions such as chart generation, rounded corner filters, etc. in the future.

Yii2 Install Imagine

Imagine's manual download and installation will not be introduced here, but you can use Baidu on your own. Here we introduce the Composer method of Yii2 to install Imageine.

Method 1

php  require --prefer-dist yiisoft/yii2-imagine

Method 2

Add the following code to the require field in the project file:

"yiisoft/yii2-imagine": "~2.1.0"

Then execute the command (cmd) in the root directory of the project:

composer require yiisoft/yii2-imagine

Yii2 Using Imagine

use yii\imagine\Image;

$srcImg = Yii::getAlias('@webroot/');
$aimImg = Yii::getAlias('@webroot/');
$srcTTF = Yii::getAlias('@webroot/img/');

// Thumb down// Parameter inset indicates frame thumbnail// The complete picture is thumbnailed in the box of 200x100// Note: The width or height of the frame must have a size smaller than the actual size of the picture, otherwise it will directly return to the source image size.Image::thumbnail($srcImg, 200, 100, 'inset')->save($aimImg, ['quality'=>100]);

// Thumb down// Parameter outbound means that single size is preferred and centered// This parameter is the default value of the function, it will take as many pictures as possible but will not exceed the scope of the picture// Example: Source image 500x200, then abbreviate according to height 100 (changed to 250x100), and then center to intercept 200x100// Example: Source diagram is 400x350, then according to the width of 200 shrinkage (changed to 200x175), then center to intercept 200x100// Example: Source diagram 100x80, then the source diagram is not reduced or intercepted, and the source diagram is directly returned to 100x80Image::thumbnail($srcImg, 200, 100, 'outbound')->save($aimImg, ['quality'=>100]);

// Thumb down// Thumbnail by width 200, height adaptableImage::thumbnail($srcImg, 200, null)->save($aimImg, ['quality'=>100]);

// Thumb down// Thumbnail by height 100, width adaptableImage::thumbnail($srcImg, null, 100)->save($aimImg, ['quality'=>100]);

// Cut// Parameters: source diagram, width, height, starting point// Save the source file $srcImg to $aimImgImage::crop($srcImg, 400, 200, [100,100])->save($aimImg);

// Rotate// Not studiedImage::frame('@webroot/img/', 5, '666', 0)->rotate(-8)->save(Yii::getAlias('@webroot/img/'), ['quality' => 100]);

// Watermark// Not studiedImage::watermark('@webroot/img/', '@webroot/img/', [10,10])->save(Yii::getAlias('@webroot/img/'), ['quality' => 100]);

// Text watermark// Parameters: source image, text, font, starting point, font configurationImage::text($srcImg, 'hello world', $srcTTF, [100,100] ,['color'=>'000000','size'=>50])->save($aimImg, ['quality'=>100]);

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.