Yii 使用intervention/image拓展实现图像处理

栏目: PHP · 发布时间: 5年前

内容简介:一:安装intervention/image拓展二:上传文件同时实现单图上传和多图上传

一:安装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(可选)

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

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

例:

//水印
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拓展的一些基本操作,详细可以参考: http://image.intervention.io/


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Natural Language Processing with Python

Natural Language Processing with Python

Steven Bird、Ewan Klein、Edward Loper / O'Reilly Media / 2009-7-10 / USD 44.99

This book offers a highly accessible introduction to Natural Language Processing, the field that underpins a variety of language technologies, ranging from predictive text and email filtering to autom......一起来看看 《Natural Language Processing with Python》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具