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 相关文章推荐
php的正则处理函数总结分析
Jun 20 PHP
PHP运行时强制显示出错信息的代码
Apr 20 PHP
Linux下PHP安装mcrypt扩展模块笔记
Sep 10 PHP
php中in_array函数用法分析
Nov 15 PHP
PHP内存使用情况如何获取
Oct 10 PHP
php PDO异常处理详解
Nov 20 PHP
PHP重定向与伪静态区别
Feb 19 PHP
PHP针对伪静态的注入总结【附asp与Python相关代码】
Aug 01 PHP
PDO::beginTransaction讲解
Jan 27 PHP
CI(CodeIgniter)框架中URL特殊字符处理与SQL注入隐患分析
Feb 28 PHP
在laravel中实现事务回滚的方法
Oct 10 PHP
PHP连接MSSQL数据库案例,PHPWAMP多个PHP版本连接SQL Server数据库
Apr 16 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防注入代码
2010/04/07 PHP
PHP扩展程序实现守护进程
2015/04/16 PHP
在Linux系统的服务器上隐藏PHP版本号的方法
2015/06/06 PHP
ThinkPHP和UCenter接口冲突的解决方法
2016/07/25 PHP
PHP实现上传图片到 zimg 服务器
2016/10/19 PHP
简单实现php上传文件功能
2017/09/21 PHP
PHP7 其他语言层面的修改
2021/03/09 PHP
JS宝典学习笔记(下)
2007/01/10 Javascript
20个非常棒的Jquery实用工具 国外文章
2010/01/01 Javascript
Jquery Ajax学习实例5 向WebService发出请求,返回泛型集合数据的异步调用
2010/03/17 Javascript
js动态添加删除,后台取数据(示例代码)
2013/11/25 Javascript
jQuery仿淘宝网产品品牌隐藏与显示效果
2015/09/01 Javascript
javascript实现3D切换焦点图
2015/10/16 Javascript
基于BootStrap Metronic开发框架经验小结【四】Bootstrap图标的提取和利用
2016/05/12 Javascript
Bootstrap零基础学习第一课之模板
2016/07/18 Javascript
获取JS中网页各种高宽与位置的方法总结
2016/07/27 Javascript
jQuery实现的手风琴侧边菜单效果
2017/03/29 jQuery
详解使用Vue Router导航钩子与Vuex来实现后退状态保存
2017/09/11 Javascript
React如何利用相对于根目录进行引用组件详解
2017/10/09 Javascript
Vue.js 的移动端组件库mint-ui实现无限滚动加载更多的方法
2017/12/23 Javascript
Vue项目分环境打包的实现步骤
2018/04/02 Javascript
vue中tab选项卡的实现思路
2018/11/25 Javascript
对于防止按钮重复点击的尝试详解
2019/04/22 Javascript
koa2+vue实现登陆及登录状态判断
2019/08/15 Javascript
layui 实现二级弹窗弹出之后 关闭一级弹窗的方法
2019/09/18 Javascript
vue 子组件修改data或调用操作
2020/08/07 Javascript
JS前端基于canvas给图片添加水印
2020/11/11 Javascript
一起深入理解js中的事件对象
2021/02/06 Javascript
如何使用pyinstaller打包32位的exe程序
2019/05/26 Python
keras Lambda自定义层实现数据的切片方式,Lambda传参数
2020/06/11 Python
查看keras各种网络结构各层的名字方式
2020/06/11 Python
virtualenv介绍及简明教程
2020/06/23 Python
大学生党员批评与自我批评
2014/09/28 职场文书
2014年幼儿园安全工作总结
2014/11/10 职场文书
对领导班子的意见和建议
2015/06/08 职场文书
Docker安装MySql8并远程访问的实现
2022/07/07 Servers