php常用图片处理类


Posted in PHP onMarch 16, 2016

本文为大家分享的php常用图片处理类,供大家参考学习,具体内容如下

<?php
/*已知问题:1.在图片缩放功能中,使用imagecreatetruecolor函数创建画布,并使用透明处理算法,但PNG格式的图片无法透明。用imagecreate函数创建画布可以解决这个问题,但是缩放出来的图片色数太少了
*
*
*type值:
* (1):代表使用图片缩放功能,此时,$value1代表缩放后图片的宽度,$value2代表缩放后图片的高度
* (2):代表使用图片裁剪功能,此时,$value1代表裁剪开始点的坐标,例:从原点开始即是“0,0”前面是x轴后面是y轴,中间用,分隔,$value2代表裁剪的宽度和高度,同样也是“20,20”的形式使用
* (3):代表使用加图片水印功能,此时,$value1代表水印图片的文件名,$value2代表水印在图片中的位置,有10值个可以选,1代表左上,2代表左中,3代表左右,4代表中左,5代表中中,6代表中右,7代表下做,8代表下中,9代表下右,0代表随机位置
*
*/
class image{
private $types; //使用的功能编号,1为图片缩放功能 2为图片裁剪功能 3,为图片加图片水印功能
private $imgtype;//图片的格式
private $image; //图片资源
private $width;//图片宽度
private $height;//图片高度
private $value1;//根据所传type值的不同,$value1分别代表不同的值
private $value2;//根据所传type值的不同,$value2分别代表不同的值
private $endaddress;//输出后的地址+文件名
function __construct($imageaddress, $types, $value1="", $value2="", $endaddress){
$this->types=$types;
$this->image=$this->imagesources($imageaddress);
$this->width=$this->imagesizex();
$this->height=$this->imagesizey();
$this->value1=$value1;
$this->value2=$value2;
$this->endaddress=$endaddress;
}
function outimage(){ //根据传入type值的不同,输出不同的功能
switch($this->types){
case 1:
$this->scaling();
break;
case 2:
$this->clipping();
break;
case 3:
$this->imagewater();
break;
default:
return false;
}
}
private function imagewater(){ //http://www.hzhuti.com 加图片水印功能
//用函数获取水印文件的长和宽
$imagearrs=$this->getimagearr($this->value1);
//调用函数计算出水印加载的位置
$positionarr=$this->position($this->value2, $imagearrs[0], $imagearrs[1]);
//加水印
imagecopy($this->image, $this->imagesources($this->value1), $positionarr[0], $positionarr[1], 0, 0, $imagearrs[0], $imagearrs[1]);
//调用输出方法保存
$this->output($this->image);
}
private function clipping(){ //图片裁剪功能
//将传进来的值分别赋给变量
list($src_x, $src_y)=explode(",", $this->value1);
list($dst_w, $dst_h)=explode(",", $this->value2);
if($this->width < $src_x+$dst_w || $this->height < $src_y+$dst_h){ //这个判断就是限制不能截取到图片外面去
return false;
}
//创建新的画布资源
$newimg=imagecreatetruecolor($dst_w, $dst_h);
//进行裁剪
imagecopyresampled($newimg, $this->image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $dst_w, $dst_h);
//调用输出方法保存
$this->output($newimg);
}
private function scaling(){ //图片缩放功能
//获取等比缩放的宽和高
$this-> proimagesize();
//根据参数进行缩放,并调用输出函数保存处理后的文件
$this->output($this->imagescaling());
}
private function imagesources($imgad){ //获取图片类型并打开图像资源
$imagearray=$this->getimagearr($imgad);
switch($imagearray[2]){
case 1://gif
$this->imgtype=1;
$img=imagecreatefromgif($imgad);
break;
case 2://jpeg
$this->imgtype=2;
$img=imagecreatefromjpeg($imgad);
break;
case 3://png
$this->imgtype=3;
$img=imagecreatefrompng($imgad);
break;
default:
return false;
}
return $img;
}
private function imagesizex(){ //获得图片宽度
return imagesx($this->image);
}
private function imagesizey(){ //获取图片高度
return imagesy($this->image);
}
private function proimagesize(){ //计算等比缩放的图片的宽和高
if($this->value1 && ($this->width < $this->height)) { //等比缩放算法
$this->value1=round(($this->value2/ $this->height)*$this->width);
}else{
$this->value2=round(($this->value1/ $this->width) * $this->height);
}
}
private function imagescaling(){//图像缩放功能,返回处理后的图像资源
$newimg=imagecreatetruecolor($this->value1, $this->value2);
$tran=imagecolortransparent($this->image);//处理透明算法
if($tran >= 0 && $tran < imagecolorstotal($this->image)){
$tranarr=imagecolorsforindex($this->image, $tran);
$newcolor=imagecolorallocate($newimg, $tranarr['red'], $tranarr['green'], $tranarr['blue']);
imagefill($newimg, 0, 0, $newcolor);
imagecolortransparent($newimg, $newcolor);
}
imagecopyresampled($newimg, $this->image, 0, 0, 0, 0, $this->value1, $this->value2, $this->width, $this->height);
return $newimg;
}
private function output($image){//输出图像
switch($this->imgtype){
case 1:
imagegif($image, $this->endaddress);
break;
case 2:
imagejpeg($image, $this->endaddress);
break;
case 3:
imagepng($image, $this->endaddress);
break;
default:
return false;
}
}
private function getimagearr($imagesou){//返回图像属性数组方法
return getimagesize($imagesou);
}
private function position($num, $width, $height){//根据传入的数字返回一个位置的坐标,$width和$height分别代表插入图像的宽和高
switch($num){
case 1:
$positionarr[0]=0;
$positionarr[1]=0;
break;
case 2:
$positionarr[0]=($this->width-$width)/2;
$positionarr[1]=0;
break;
case 3:
$positionarr[0]=$this->width-$width;
$positionarr[1]=0;
break;
case 4:
$positionarr[0]=0;
$positionarr[1]=($this->height-$height)/2;
break;
case 5:
$positionarr[0]=($this->width-$width)/2;
$positionarr[1]=($this->height-$height)/2;
break;
case 6:
$positionarr[0]=$this->width-$width;
$positionarr[1]=($this->height-$height)/2;
break;
case 7:
$positionarr[0]=0;
$positionarr[1]=$this->height-$height;
break;
case 8:
$positionarr[0]=($this->width-$width)/2;
$positionarr[1]=$this->height-$height;
break;
case 9:
$positionarr[0]=$this->width-$width;
$positionarr[1]=$this->height-$height;
break;
case 0:
$positionarr[0]=rand(0, $this->width-$width);
$positionarr[1]=rand(0, $this->height-$height);
break;
}
return $positionarr;
}
function __destruct(){
imagedestroy($this->image);
}
}
?>

以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。

PHP 相关文章推荐
NT IIS下用ODBC连接数据库
Oct 09 PHP
PHP获取http请求的头信息实现步骤
Dec 16 PHP
解析php中eclipse 用空格替换 tab键
Jun 24 PHP
PHP连接局域网MYSQL数据库的简单实例
Aug 26 PHP
使用PHP破解防盗链图片的一个简单方法
Jun 07 PHP
Symfony2学习笔记之控制器用法详解
Mar 17 PHP
PHP list() 将数组中的值赋给变量的简单实例
Jun 13 PHP
PHP实现的文件操作类及文件下载功能示例
Dec 24 PHP
PHP 进度条函数的简单实例
Sep 19 PHP
PHP+jQuery实现双击修改table表格功能示例
Feb 21 PHP
PHP中strtr与str_replace函数运行性能简单测试示例
Jun 22 PHP
解决laravel(5.5)访问public报错的问题
Oct 12 PHP
PHP匿名函数和use子句用法实例
Mar 16 #PHP
PHP Static延迟静态绑定用法分析
Mar 16 #PHP
PHP5.5迭代生成器用法实例详解
Mar 16 #PHP
Laravel执行migrate命令提示:No such file or directory的解决方法
Mar 16 #PHP
PHP 中提示undefined index如何解决(多种方法)
Mar 16 #PHP
Laravel中Trait的用法实例详解
Mar 16 #PHP
Laravel中注册Facades的步骤详解
Mar 16 #PHP
You might like
php根据指定位置和长度获得子字符串的方法
2015/03/17 PHP
8个PHP数组面试题
2015/06/23 PHP
PHP时间类完整代码实例
2021/02/26 PHP
用js实现控制内容的向上向下滚动效果
2007/06/26 Javascript
jquery 学习笔记 传智博客佟老师附详细注释
2020/09/12 Javascript
JQuery实现倒计时按钮的实现代码
2012/03/23 Javascript
JS中toFixed()方法引起的问题如何解决
2012/11/20 Javascript
jQuery写的日历(包括日历的样式及功能)
2013/04/23 Javascript
jQuery实现注册会员时密码强度提示信息功能示例
2017/09/05 jQuery
vue父子组件的嵌套的示例代码
2017/09/08 Javascript
基于vue-simplemde实现图片拖拽、粘贴功能
2018/04/12 Javascript
vue项目使用axios发送请求让ajax请求头部携带cookie的方法
2018/09/26 Javascript
使用rollup打包JS的方法步骤
2018/12/05 Javascript
Vue vm.$attrs使用场景详解
2020/03/08 Javascript
从0到1学习JavaScript编写贪吃蛇游戏
2020/07/28 Javascript
vue addRoutes路由动态加载操作
2020/08/04 Javascript
详解webpack的clean-webpack-plugin插件报错
2020/10/16 Javascript
python基础教程之自定义函数介绍
2014/08/29 Python
Windows下PyMongo下载及安装教程
2015/04/27 Python
python socket多线程通讯实例分析(聊天室)
2016/04/06 Python
Python 中的 else详解
2016/04/23 Python
Python爬虫抓取代理IP并检验可用性的实例
2018/05/07 Python
Python类和实例的属性机制原理详解
2020/03/21 Python
pandas数据拼接的实现示例
2020/04/16 Python
Python 如何对文件目录操作
2020/07/10 Python
Python tkinter之Bind(绑定事件)的使用示例
2021/02/05 Python
Css3新特性应用之视觉效果实例
2016/12/12 HTML / CSS
用canvas画心电图的示例代码
2018/09/10 HTML / CSS
迪卡侬比利时官网:Decathlon比利时
2019/12/28 全球购物
JSP&Servlet技术面试题
2015/05/21 面试题
爽歪歪广告词
2014/03/20 职场文书
新学期开学演讲稿
2014/05/24 职场文书
运动会表扬稿
2015/01/16 职场文书
优秀大学生自荐信
2015/03/26 职场文书
瞿秋白纪念馆观后感
2015/06/10 职场文书
建议书的格式及范文
2015/09/14 职场文书