代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
PHP
】
图片缩略水印类
作者:
qita.in
/ 发布于
2012/10/22
/
485
图片缩略水印类
<div>/**</div> <div>* 图片缩放水印类</div> <div>*</div> <div> * @version 1.0 ;</div> <div>*</div> <div>*/</div> <div>class cls_photo</div> <div>{</div> <div> protected $waterrate = 0.2; //水印图标在图片上的比例</div> <div> protected $width = 300; //缩略图默认宽度</div> <div> protected $height = 200; //缩略图默认高度</div> <div> protected $padding = 5; //水印图到边的距离</div> <div> protected $water_mark = "./water.png";</div> <div> protected $water_mark_pos = 5;//水印图片位置(1=左上角,2=右上角,3=左下角,4=右下角,5中央)</div> <div> protected $watermode = 0;// 0缩略图时不打水印 1缩略图时打水印</div> <div> protected $magick_handle;//图片操作句柄</div> <div> protected $format = array('jpg','gif','png','jpeg'); // 图片文件格式限定</div> <div> protected $smallpic_mode = 2;//默认模式 0为不生成缩略图, 1为裁切缩放 ,2为比例缩放 3为缩放填充模式</div> <div></div> <div> /**</div> <div> * 设置图片类参数</div> <div> *</div> <div> * @param $arg 图片参数 多次可放入数组里 如下</div> <div> * @param $protected 参数值</div> <div> * array(</div> <div> * 'waterrate'=>0.2,</div> <div> * 'water_mark'=>'./water.png',</div> <div> * 'water_mark_pos'=>4,</div> <div> * 'smallpic_mode'=>1</div> <div> * );</div> <div> * @return ture/false</div> <div> */</div> <div> public function set_args($arg,$val="")</div> <div> {</div> <div> $params = array('waterrate','water_mark','water_mark_pos','smallpic_mode','watermode','width','height');</div> <div> if(is_array($arg))</div> <div> {</div> <div> foreach ($arg as $k =>$v)</div> <div> {</div> <div> if(in_array($k,$params))</div> <div> {</div> <div> $this->$k = $v;</div> <div> } <div> } <div> } <div> else</div> <div> {</div> <div> <span class="Apple-tab-span" style="white-space:pre"> </span>if(empty($val))</div> <div> <span class="Apple-tab-span" style="white-space:pre"> </span>{</div> <div> <span class="Apple-tab-span" style="white-space:pre"> </span> return false;</div> <div> <span class="Apple-tab-span" style="white-space:pre"> </span>} <div> <span class="Apple-tab-span" style="white-space:pre"> </span>else</div> <div> <span class="Apple-tab-span" style="white-space:pre"> </span>{</div> <div> <span class="Apple-tab-span" style="white-space:pre"> </span>if(in_array($arg,$params))</div> <div> {</div> <div> $this->$arg = $val;</div> <div> } <div> <span class="Apple-tab-span" style="white-space:pre"> </span>} <div> } <div> return true;</div> <div> } <div></div> <div> /**</div> <div> * 图片缩放</div> <div> *</div> <div> * @param $src_file 源文件路径</div> <div> * @param $dst_file 目标文件路径</div> <div> * @return 缩略图片路径/false</div> <div> */</div> <div> public function scale($src_file,$dst_file="")</div> <div> {</div> <div> $dst_width = $this->width;</div> <div> $dst_height = $this->height;</div> <div> $mode = $this->smallpic_mode;</div> <div> $magic_water_handle = NewMagickWand();</div> <div> if (!MagickReadImage($magic_water_handle, $src_file))return false;</div> <div></div> <div> //类型</div> <div> $srcext = strtolower(MagickGetImageFormat($magic_water_handle));</div> <div> if($srcext=='bmp')</div> <div> {</div> <div> $srcext = 'jpeg';</div> <div> } <div> if(!in_array($srcext,$this->format))return false;</div> <div> //尺寸</div> <div> $src_width = MagickGetImageWidth($magic_water_handle);</div> <div> $src_height = MagickGetImageHeight($magic_water_handle);</div> <div></div> <div> //裁切缩放模式</div> <div> if($mode == 1)</div> <div> {</div> <div> $pos_x=$pos_y = 0;//裁切临时位置</div> <div> $src_widthc = $src_width;//裁切临时宽度</div> <div> $src_heightc = $src_height;//裁切临时高度</div> <div> if($src_width/$src_height>$dst_width/$dst_height)</div> <div> {</div> <div> $src_widthc = $src_height*$dst_width/$dst_height;</div> <div> $pos_x = ($src_width-$src_widthc)/2;</div> <div></div> <div> } <div> else</div> <div> {</div> <div> $src_heightc = $src_width*$dst_height/$dst_width;</div> <div> $pos_y = ($src_height-$src_heightc)/2;</div> <div> } <div> MagickCropImage($magic_water_handle,$src_widthc,$src_heightc,$pos_x,$pos_y);//裁切</div> <div> //因为MagickCropImage函数后,Gif 图像改,但画布不变</div> <div> $this->magick_handle = NewMagickWand();</div> <div> MagickNewImage($this->magick_handle,$src_widthc,$src_heightc,'#ffffff');</div> <div> MagickSetFormat($this->magick_handle,$srcext);</div> <div> MagickCompositeImage($this->magick_handle,$magic_water_handle,MW_OverCompositeOp,0,0);</div> <div> //缩放</div> <div> MagickScaleImage($this->magick_handle, $dst_width, $dst_height);</div> <div></div> <div> } <div> //比例缩放模式</div> <div> if($mode == 2)</div> <div> {</div> <div> if($src_width/$src_height>$dst_width/$dst_height)</div> <div> {</div> <div> $dst_height=$dst_width*$src_height/$src_width;</div> <div> } <div> else</div> <div> {</div> <div> $dst_width=$dst_height*$src_width/$src_height;</div> <div> } <div> $this->magick_handle=$magic_water_handle;//替换</div> <div> MagickScaleImage($this->magick_handle, $dst_width, $dst_height);//缩放</div> <div> } <div> //缩放填充模式</div> <div> if($mode == 3)</div> <div> {</div> <div> if($src_width/$src_height>$dst_width/$dst_height)</div> <div> {</div> <div> $dst_heightc=$dst_width*$src_height/$src_width;</div> <div> $dst_widthc=$dst_width;</div> <div> } <div> else</div> <div> {</div> <div> $dst_widthc=$dst_height*$src_width/$src_height;</div> <div> $dst_heightc=$dst_height;</div> <div> } <div> MagickScaleImage($magic_water_handle, $dst_widthc, $dst_heightc);//缩放</div> <div> $this->magick_handle = NewMagickWand();</div> <div> MagickNewImage($this->magick_handle,$dst_width,$dst_height,$this->smallpic_bgcolor);</div> <div> MagickSetFormat($this->magick_handle,$srcext);</div> <div> MagickCompositeImage($this->magick_handle,$magic_water_handle,MW_OverCompositeOp,($dst_width-$dst_widthc)/2,($dst_height-$dst_heightc)/2);</div> <div> } <div> //打水印</div> <div> if($this->watermode == 1)</div> <div> {</div> <div> $this->set_mark();</div> <div> } <div> if(empty($dst_file))</div> <div> {</div> <div> //建立临时文件</div> <div> $dst_file = tempnam($_SERVER["SINASRV_CACHE_DIR"],"TMP_IMG");</div> <div> } <div> MagickWriteImage($this->magick_handle, $dst_file);</div> <div> return $dst_file;</div> <div> } </div> <div></div> <div> /**</div> <div> * 打水印</div> <div> *</div> <div> * @param $src_file 要打水印的图片路径</div> <div> * @param $dst_file 生产水印的文件保存路径,为空则生产随机临时文件</div> <div> * @return 水印文件路径/false</div> <div> */</div> <div> public function water_mark($src_file,$dst_file="")</div> <div> {</div> <div> $this->magick_handle = NewMagickWand();</div> <div> if (!MagickReadImage($this->magick_handle, $src_file))</div> <div> return false;</div> <div> $this->set_mark();</div> <div> if(empty($dst_file))</div> <div> {</div> <div> //建立临时文件</div> <div> $dst_file = tempnam($_SERVER["SINASRV_CACHE_DIR"],"TMP_IMG");</div> <div> } <div> MagickWriteImage($this->magick_handle, $dst_file);</div> <div> return $dst_file;</div> <div> } <div></div> <div> /**</div> <div> * 对内接口</div> <div> * 给图片打水印</div> <div> *</div> <div> */</div> <div> protected function set_mark()</div> <div> {</div> <div></div> <div> //尺寸</div> <div> $dst_width = MagickGetImageWidth($this->magick_handle);</div> <div> $dst_height = MagickGetImageHeight($this->magick_handle);</div> <div> //处理水印图</div> <div> if ($this->water_mark && is_file($this->water_mark))</div> <div> {</div> <div> $magic_water_handle = NewMagickWand();</div> <div> MagickRemoveImage($magic_water_handle);</div> <div> if (MagickReadImage($magic_water_handle, $this->water_mark))</div> <div> {</div> <div> MagickScaleImage($magic_water_handle, $dst_width*$this->waterrate, $dst_width*$this->waterrate*MagickGetImageHeight($magic_water_handle)/MagickGetImageWidth($magic_water_handle));//缩放水印到图片的1/5</div> <div> if ($this->water_mark_pos == 1)</div> <div> {</div> <div> $left = $this->padding;</div> <div> $top = $this->padding;</div> <div> } <div> elseif ($this->water_mark_pos == 2)</div> <div> {</div> <div> $left = $dst_width-$this->padding-MagickGetImageWidth($magic_water_handle);</div> <div> $top = $this->padding;</div> <div> } <div> elseif ($this->water_mark_pos == 3)</div> <div> {</div> <div> $left = $this->padding;</div> <div> $top = $dst_height -$this->padding-MagickGetImageHeight($magic_water_handle);</div> <div> } <div> elseif ($this->water_mark_pos == 4)</div> <div> {</div> <div> $left = $dst_width-$this->padding-MagickGetImageWidth($magic_water_handle);</div> <div> $top =$dst_height -$this->padding-MagickGetImageHeight($magic_water_handle);</div> <div> } <div> elseif ($this->water_mark_pos == 5)</div> <div> {</div> <div> $left = ($dst_width-MagickGetImageWidth($magic_water_handle))/2;</div> <div> $top =($dst_height -MagickGetImageHeight($magic_water_handle))/2;</div> <div> } <div> MagickCompositeImage($this->magick_handle,$magic_water_handle,MW_OverCompositeOp,$left,$top);</div> <div> } <div> } <div> } <div>}
试试其它关键字
图片缩略水印类
同语言下
.
用net匹配并替换iOS标准的emoji表情符号
.
处理带Emoji表情的的字符串
.
获取微信昵称时 过滤特殊字符
.
通过判断上传文件的头字符来判断文件的类型
.
模拟百度URL加密解密算法
.
以太坊检查地址是否合法
.
实现crontab解析类
.
获取每个月的开始和结束时间
.
图片上传工具类
.
APP手机应用信息采集
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
qita.in
贡献的其它代码
(
28
)
.
跨站刷票代码
.
计算一个文件夹的大小
.
全屏点弹代码
.
替代标题
.
实现字符串翻转(包含中文汉字)
.
PHP中英文断句无乱码
.
各种过滤字符函数
.
获取当前页面完整URL地址
.
将html 转成wml WAP标记语言
.
检查用户名是否符合规定
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3