Home > Backend Development > PHP Tutorial > How to install and use the Yii2 plug-in Imagine

How to install and use the Yii2 plug-in Imagine

小云云
Release: 2023-03-20 06:50:02
Original
2163 people have browsed it

Imagine is an object-oriented PHP class library for image manipulation. 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 given color. It also provides some methods to draw graphics 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 image. Imagine is a class library that will be updated frequently. In the future, it will also implement functions such as chart generation and rounded corner filters. This article mainly introduces to you the installation and use of the Yii2 third-party library plug-in Imagine. It has certain reference value. Interested friends can refer to it. I hope it can help you.

Yii2 Installation of Imagine

The manual download and installation of Imagine will not be introduced here. You can Baidu it yourself. Here we introduce the Composer method of Yii2 to install Imageine.

Method one


php composer.phar require --prefer-dist yiisoft/yii2-imagine
Copy after login

Method two

Add the following code in the require field in the project composer.json file:


"yiisoft/yii2-imagine": "~2.1.0"
Copy after login

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


composer require yiisoft/yii2-imagine
Copy after login

Yii2 Use Imagine


use yii\imagine\Image;

$srcImg = Yii::getAlias('@webroot/test.jpg');
$aimImg = Yii::getAlias('@webroot/testdeal.jpg');
$srcTTF = Yii::getAlias('@webroot/img/symbol.ttf');

// 缩略
// 参数 inset 表示定框缩略
// 图片完整缩略在 200x100 的框内
// 备注:定框的宽度或高度必须有一个小于图片的实际尺寸,否则直接返回源图尺寸
Image::thumbnail($srcImg, 200, 100, 'inset')->save($aimImg, ['quality'=>100]);

// 缩略
// 参数 outbound 表示单尺寸优先缩略并居中截取
// 该参数为函数的默认值,它会为您尽可能多的截取图片但又不会超出图片范围
// 例:源图 500x200,那么按照高度 100 缩略(变为250x100),然后再居中截取 200x100
// 例:源图 400x350,那么按照宽度 200 缩率(变为200x175),然后再居中截取 200x100
// 例:源图 100x80,那么不缩率不截取,直接返回源图 100x80
Image::thumbnail($srcImg, 200, 100, 'outbound')->save($aimImg, ['quality'=>100]);

// 缩略
// 按宽度 200 缩略,高度自适应
Image::thumbnail($srcImg, 200, null)->save($aimImg, ['quality'=>100]);

// 缩略
// 按高度 100 缩略,宽度自适应
Image::thumbnail($srcImg, null, 100)->save($aimImg, ['quality'=>100]);

// 剪切
// 参数:源图、宽度、高度、起始点
// 将源文件 $srcImg 保存到 $aimImg
Image::crop($srcImg, 400, 200, [100,100])->save($aimImg);

// 旋转
// 未研究
Image::frame('@webroot/img/test-image.jpg', 5, '666', 0)->rotate(-8)->save(Yii::getAlias('@webroot/img/thumb-test-frame.jpg'), ['quality' => 100]);

// 水印
// 未研究
Image::watermark('@webroot/img/test-image.jpg', '@webroot/img/watermark.jpg', [10,10])->save(Yii::getAlias('@webroot/img/thumb-test-watermark.jpg'), ['quality' => 100]);

// 文字水印
// 参数:源图、文字、字体、起始点、字体配置
Image::text($srcImg, 'hello world', $srcTTF, [100,100] ,['color'=>'000000','size'=>50])->save($aimImg, ['quality'=>100]);
Copy after login

The above is the detailed content of How to install and use the Yii2 plug-in Imagine. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template