本文实例讲述了服务器端编程语言(专业超文本预处理器的缩写)实现可添加水印与生成缩略图的图片处理工具类。分享给大家供大家参考,具体如下:
ImageTool.class.php
?phpclass ImageTool { private $ imagePath;//图片路径private $ outputDir//输出文件夹private $ memoryImg//内存图像public function _ _ construct($ imagePath,$ outputDir=null){ $ this-imagePath=$ imagePath;$ this-OutPutdir=$ OutPutdir;$ this-memoryImg=null;} /** * 显示内存中的图片* @param $image */public函数showImage() { if ($this-memoryImg!=null){ $ info=getimagesize($ this-imagePath);$ type=image _ type _ to _ extension($ info[2],false);标题('内容类型: '。$ info[' mime ']);$ funs=' image { $ type } $ funs($ this-memoryImg);图像销毁($ this-memoryImg);$ this-memoryImg=null;} } /**将图片以文件形式保存* @param $image */private函数保存图像($ image){ $ info=getimagesize($ this-imagePath);$ type=image _ type _ to _ extension($ info[2],false);$ funs=' image { $ type }if(空($this-outputDir)) { $funs($image,md5($this-imagePath).'.'。$ type);} else { $funs($image,$this-outputDir .md5($this-imagePath).'.'。$ type);} } /** * 压缩图片* @param $width压缩后宽度* @ param $高度压缩后高度* @ param bool $输出是否输出文件* @ return resource */public function compressImage($ width,$height,$ output=false){ $ image=null;$ info=getimagesize($ this-imagePath);$ type=image _ type _ to _ extension($ info[2],false);$ fun=' imagecreatefrom { $ type } $ image=$ fun($ this-imagePath);$ thumbnail=imagecreatetrue color($ width,$ height);imagecopy已重新采样($缩略图,$image,0,0,0,$width,$height,$info[0],$ info[1]);图像破坏($ image);if($ output){ $ this-save image($缩略图);} $ this-memoryImg=$缩略图;返回$ this} /** *为图像添加文字标记* * @ param $内容文本内容* @ param $大小字体大小* @ param $字体字体样式* @ param bool $输出是否输出文件* @返回$this */public函数addTextmark($content,$size,$font,$ output=false){ $ info=getimagesize($ this-imagePath);$ type=image _ type _ to _ extension($ info[2],false);$ fun=' imagecreatefrom { $ type } $ image=$ fun($ this-imagePath);$ color=imagecolordallocatelpha($ image,0,0,0,80);$ POSx=imagesx($ image)-strlen($ content)* $ size/2;$ POSy=imagesy($ image)-$ size/1.5;imagettftext($image,$size,0,$posX,$posY,$color,$font,$ content);if($ output){ $ this-save image($ image);} $ this-memoryImg=$ image;返回$ this} /** *为图片添加水印* * @ param $水印水印图片路径* @param $alpha水印透明度(0-100)* @ param bool $输出是否输出文件* @返回$this */public函数增加水印($watermark,$alpha,$ output=false){ $ image _ info=getimagesize($ this-imagePath);$ image _ type=image _ type _ to _ extension($ image _ info[2],false);$ image _ fun=' imagecreatefrom { $ image _ type } ';$ image=$ image _ fun($ this-imagePath);$ mark _ info=getimagesize($ watermark);$ mark _ type=image _ type _ to _ extension($ mark _ info[2],false);$ mark _ fun=' imagecreatefrom { $ mark _ type } ';$ mark=$ mark _ fun($水印);$ POSx=imagesx($ image)-imagesx($ mark);$ POSy=imagesy($ image)-imagesy($ mark);imagecopymerge($image,$mark,$posX,$posY,0,0,$mark_info[0],$mark_info[1],$ alpha);if($ output){ $ this-save image($ image);} $ this-memoryImg=$ image;返回$ this}}ImageTool使用
首先导入ImageTool工具:
require _ once ' imagetool。上课。PHP ';然后实例化ImageTool对象:
$ imageTool=new imageTool(' img/opp man。JPEG ',' out/');//图片路径、输出文件夹一、生成压缩图片
$imageTool-compressImage(350,250,真);//压缩宽度,压缩高度,是否保存$ imageTool-showImage();
二、添加文字水印
$imageTool-addTextmark('一拳超人',50,' res/micro.ttf ',true);//内容、大小、字体、是否保存$ imageTool-showImage();
第三,添加图片水印
$ imageTool-add水印(' res/logo.jpeg ',100,true);//水印路径,透明度,是否保存$ imageTool-showImage();
仅作为临时图像输出:
$imageTool-addTextmark('快速输出',50,' RES/micro . TTF ')-show image();PS:这里推荐几个实用的图像处理工具,供大家参考:
在线图像切割/生成工具:http://tools.jb51.net/aideddesign/imgcut
BASE64在线图片转换工具:http://tools.jb51.net/transcoding/img2base64
ICO图标在线生成工具:http://tools.jb51.net/aideddesign/ico_img
在线电子邮件图标制作工具:http://tools.jb51.net/email/emaillogo
在线图像格式转换工具:http://tools.jb51.net/aideddesign/picext
更多对PHP相关内容感兴趣的读者可以查看本网站专题:《PHP图形与图片操作技巧汇总》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php面向对象程序设计入门教程》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》、《php常见数据库操作技巧汇总》。
希望本文对PHP编程有所帮助。