PHP图片处理类

上传人:豆浆 文档编号:3157006 上传时间:2017-07-30 格式:DOC 页数:6 大小:51KB
返回 下载 相关 举报
PHP图片处理类_第1页
第1页 / 共6页
PHP图片处理类_第2页
第2页 / 共6页
PHP图片处理类_第3页
第3页 / 共6页
PHP图片处理类_第4页
第4页 / 共6页
PHP图片处理类_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《PHP图片处理类》由会员分享,可在线阅读,更多相关《PHP图片处理类(6页珍藏版)》请在金锄头文库上搜索。

1、* 裁剪: if(!$imgs-thumb($targetFile,600,500)die(-图像上传失败!);* 水印: $imgs-water($targetFile,$water,$pos=0,$pct=50); $targetFile为已经上传到服务器的文件路径*/class image / 当前图片protected $img;/ 图像types 对应表protected $types = array(1 = gif,2 = jpg,3 = png,6 = bmp);/* 初始化图片类* param string $img: 原图片路径*/public function _const

2、ruct($img)$this-img = $img;/* 获取图片基本信息* param String $img: 图片路径* return unknown*/protected function getImageInfo($img)$info = getimagesize($img);if(isset($this-types$info2)$infoext = $infotype = $this-types$info2; else$infoext = $infotype = jpg;$infotype = jpg & $infotype = jpeg; $infosize = filesiz

3、e($img);return $info;/* 裁剪图片* param String $filename: 图片保存路径* param number $new_w: 图片裁剪后宽度* param number $new_h: 图片裁剪后高度* param number $cut: 图片裁剪方式:0 = 等比例裁剪;1=居中裁剪;2=0,0坐标裁剪* param boolean $big: 是否允许放大* return boolean|String: false| 图片保存路径 */public function thumb($filename,$new_w=160,$new_h=120,$cu

4、t=0,$big=false)/ 获取原图信息$info = $this-getImageInfo($this-img);if(!empty($info0) $old_w = $info0;$old_h = $info1;$type = $infotype;$ext = $infoext;unset($info);if(!$big)if($new_h $old_h | $new_w $old_w)return false;/ 裁剪图片if($cut = 0) / 等比列$scale = min($new_w/$old_w, $new_h/$old_h); / 计算缩放比例$width = (i

5、nt)($old_w*$scale); / 缩略图尺寸$height = (int)($old_h*$scale);$start_w = $start_h = 0;$end_w = $old_w;$end_h = $old_h; elseif($cut = 1) / center center 裁剪$scale1 = round($new_w/$new_h,2); $scale2 = round($old_w/$old_h,2);if($scale1 $scale2)$end_h = round($old_w/$scale1,2);$start_h = ($old_h-$end_h)/2;$s

6、tart_w = 0;$end_w = $old_w; else$end_w = round($old_h*$scale1,2);$start_w = ($old_w-$end_w)/2;$start_h = 0;$end_h = $old_h;$width = $new_w;$height= $new_h; elseif($cut = 2) / left top 裁剪$scale1 = round($new_w/$new_h,2);$scale2 = round($old_w/$old_h,2);if($scale1 $scale2)$end_h = round($old_w/$scale1

7、,2);$end_w = $old_w; else$end_w = round($old_h*$scale1,2);$end_h = $old_h;$start_w = 0;$start_h = 0;$width = $new_w;$height= $new_h;/ 载入原图$createFun = ImageCreateFrom.$type;$oldimg = $createFun($this-img);/ 创建缩略图if($type!=gif & function_exists(imagecreatetruecolor)$newimg = imagecreatetruecolor($wid

8、th, $height); else$newimg = imagecreate($width, $height);/ 复制图片if(function_exists(ImageCopyResampled)ImageCopyResampled($newimg, $oldimg, 0, 0, $start_w, $start_h, $width, $height, $end_w,$end_h); else ImageCopyResized($newimg, $oldimg, 0, 0, $start_w, $start_h, $width, $height, $end_w,$end_h);/ 对jp

9、eg图形设置隔行扫描$type = jpeg & imageinterlace($newimg,1);/ 生成图片$imageFun = image.$type;!$imageFun($newimg,$filename,100) & die(保存失败!检查目录是否存在并且可写?);ImageDestroy($newimg);ImageDestroy($oldimg);return $filename;return false;/* 图片添加水印* param String $filename: 新图片保存地址* param unknown $water: 水印图片路径* param numbe

10、r $pos: 水印位置* 0=随机;* 1=顶端居左; 2=顶端居中; 3=顶端居右; * 4=中部居左; 5=中部居中; 6=中部居右; * 7=底部居左; 8=底部居中; 9=底部居右* 其它=随机* param number $pct: 透明度* return boolean|String: false| 图片保存路径*/public function water($filename,$water,$pos=0,$pct=50)/ 加载水印图片$info = $this-getImageInfo($water);if(!empty($info0)$water_w = $info0;$w

11、ater_h = $info1;$type = $infotype;$fun = imagecreatefrom.$type;$waterimg = $fun($water); elsereturn false;/ 加载背景图片$info = $this-getImageInfo($this-img); if(!empty($info0)$old_w = $info0;$old_h = $info1;$type = $infotype;$fun = imagecreatefrom.$type;$oldimg = $fun($this-img); elsereturn false;/ 剪切水印$

12、water_w $old_w & $water_w = $old_w;$water_h $old_h & $water_h = $old_h;/ 水印位置switch($pos)case 0:/随机$posX = rand(0,($old_w - $water_w);$posY = rand(0,($old_h - $water_h);break;case 1:/1为顶端居左$posX = 0;$posY = 0;break;case 2:/2为顶端居中$posX = ($old_w - $water_w) / 2;$posY = 0;break;case 3:/3为顶端居右$posX = $

13、old_w - $water_w;$posY = 0;break;case 4:/4为中部居左$posX = 0;$posY = ($old_h - $water_h) / 2;break;case 5:/5为中部居中$posX = ($old_w - $water_w) / 2;$posY = ($old_h - $water_h) / 2;break;case 6:/6为中部居右$posX = $old_w - $water_w;$posY = ($old_h - $water_h) / 2;break;case 7:/7为底端居左 $posX = 0;$posY = $old_h - $

14、water_h;break;case 8:/8为底端居中$posX = ($old_w - $water_w) / 2;$posY = $old_h - $water_h;break;case 9:/9为底端居右$posX = $old_w - $water_w;$posY = $old_h - $water_h;break;default: /随机$posX = rand(0,($old_w - $water_w);$posY = rand(0,($old_h - $water_h);break;/ 设定图像的混色模式imagealphablending($oldimg, true);/ 添加水印imagecopymerge($oldimg, $waterimg, $posX, $posY, 0, 0, $water_w,$water_h,$pct);$fun = image.$type;!$fun($oldimg, $filename,100) & die(保存失败! 检查目录是否存在并且可写?);imagedestroy($oldimg);imagedestroy($waterimg);return $filename;?

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 其它文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号