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) {
  } else {
    // Single graph upload 

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

// 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 
  // Configure Watermark Color 
  // Configure the watermark level to the left ( left, right and center)
  // Configure the watermark to live vertically (top, bottom and middle)
  // Configure the watermark rotation angle 

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)

x: Horizontal offset, default 0

y: Vertical offset, default 0



Here are some basic operations of intervention/image expansion. For details, please refer to: http://image.intervention.io/


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!

Related articles: