一:安装intervention/image拓展

composer require intervention/image

二:上传文件

\intervention\image\imagemanagerstatic::make($_files['file']['tmp_name'])->save('upload.jpg');//file为上传表单的name名
\intervention\image\imagemanagerstatic::make($_files['file']['tmp_name'])->resize(300, 200)->save('upload.jpg');//file为上传表单的name名,并将上传的图片压缩成300,200

同时实现单图上传和多图上传

if ($_files['file']) {
  $image = $_files['file']['tmp_name'];
  if (is_array($image)) {
    //多图上传
    foreach ($image as $key => $item) {
      \intervention\image\imagemanagerstatic::make($item)->save($key.'upload.jpg');
    }
  } else {
    //单图上传
    \intervention\image\imagemanagerstatic::make($image)->save('upload.jpg');
  }
}

三:添加水印

1:添加文字水印

添加文字水印主要使用到text方法

text方法参数说明

x(可选)

x定义第一个字符的基点。默认值:0

y(可选)

y定义第一个字符的基点。默认值:0

callback(可选)

关闭字体对象的回调,回调可配置:

(1)file:配置水印字体
(2)size:配置水印大小
(3)color:配置水印颜色
(4)align:配置水印水平对齐方式
(5)valign:配置是垂直对齐方式
(6)angle:配置水印旋转角度
例:

//水印
imagemanagerstatic::make('upload.jpg')->text('水印文字',20,30,function($font){
  //配置水印字体
  $font->file(\yii::getalias('@webroot') . '/simsun.ttc');
  //配置水印大小
  $font->size(30);
  //配置水印颜色
  $font->color('#fff');
  //配置水印水平居左( left, right and center)
  $font->align('left');
  //配置水印垂直居下(top, bottom and middle)
  $font->valign('bottom');
  //配置水印旋转角度
  $font->angle(45);
})->save('uploadwater.jpg');

2:添加图片水印

添加图片水印主要使用到insert方法

insert方法参数说明:

source:水印图片地址

position:设置插入图像的位置,参数可配置项为:

(1)top-left (default)
(2)top
(3)top-right
(4)left
(5)center
(6)right
(7)bottom-left
(8)bottom
(9)bottom-right

x:水平偏移量,默认0

y:垂直偏移量,默认0

例:

imagemanagerstatic::make('upload.jpg')->insert('water.jpg','bottom-left',10,10)->save('uploadwater.jpg');

这里是intervention/image拓展的一些基本操作,详细可以参考: 

总结

以上所述是www.887551.com给大家介绍的yii 使用intervention/image拓展实现图像处理功能,希望对大家有所帮助