Yii uses intervention and image extension to realize image processing function
- 2021-12-12 08:06:48
- OfStack
1: Install intervention/image extensions
composer require intervention/image
2: Upload a file
\Intervention\Image\ImageManagerStatic::make($_FILES['file']['tmp_name'])->save('upload.jpg');//file Object for uploading a form name Name
\Intervention\Image\ImageManagerStatic::make($_FILES['file']['tmp_name'])->resize(300, 200)->save('upload.jpg');//file Object for uploading a form name Name , And compress the uploaded picture into 300,200
Simultaneously realize single graph uploading and multi-graph uploading
if ($_FILES['file']) {
$image = $_FILES['file']['tmp_name'];
if (is_array($image)) {
// Multi-graph upload
foreach ($image as $key => $item) {
\Intervention\Image\ImageManagerStatic::make($item)->save($key.'upload.jpg');
}
} else {
// Single graph upload
\Intervention\Image\ImageManagerStatic::make($image)->save('upload.jpg');
}
}
3: Add Watermark
1: Add text watermark
Adding text watermark mainly uses text method
text Method Parameter Description
x (Optional)
x defines the base point of the first character. Default: 0
y (Optional)
y defines the base point of the first character. Default: 0
callback (Optional)
Close the callback of the font object, which can be configured:
(1) file: Configure the watermark font
(2) size: Configure Watermark Size
(3) color: Configure Watermark Color
(4) align: Configuring Watermark Horizontal Alignment
(5) valign: Configuration is vertically aligned
(6) angle: Configuring Watermark Rotation Angle
Example:
// Watermark
ImageManagerStatic::make('upload.jpg')->text(' Watermark text ',20,30,function($font){
// Configure watermark fonts
$font->file(\Yii::getAlias('@webroot') . '/simsun.ttc');
// Configure Watermark Size
$font->size(30);
// Configure Watermark Color
$font->color('#fff');
// Configure the watermark level to the left ( left, right and center)
$font->align('left');
// Configure the watermark to live vertically (top, bottom and middle)
$font->valign('bottom');
// Configure the watermark rotation angle
$font->angle(45);
})->save('uploadWater.jpg');
2: Add image watermark
Adding image watermark mainly uses insert method
insert method parameter description:
source: Watermark Image Address
position: Set the location of the image to be inserted, and the parameter configurable items are:
(1)top-left (default)
(2)top
(3)top-right
(4)left
(5)center
(6)right
(7)bottom-left
(8)bottom
(9)bottom-right
x: Horizontal offset, default 0
y: Vertical offset, default 0
Example:
ImageManagerStatic::make('upload.jpg')->insert('water.jpg','bottom-left',10,10)->save('uploadWater.jpg');
Here are some basic operations of intervention/image expansion. For details, please refer to: http://image.intervention.io/
Summarize
The above is the site to introduce you Yii using intervention/image expansion to achieve image processing functions, I hope to help you, if you have any questions welcome to leave me a message, this site will reply to you in time!