本图像处理类可以完成对图片的缩放、加水印和裁剪的功能,支持多种图片类型的处理,缩放时进行优化等。
path = rtrim($path,/)./; } /** * 对指定的图像进行缩放 * @param string $name 是需要处理的图片名称 * @param int $width 缩放后的宽度 * @param int $height 缩放后的高度 * @param string $qz 是新图片的前缀 * @return mixed 是缩放后的图片名称,失败返回false; */ function thumb($name, $width, $height,$qz=th_){ /* 获取图片宽度、高度、及类型信息 */ $imginfo = $this->getinfo($name); /* 获取背景图片的资源 */ $srcimg = $this->getimg($name, $imginfo); /* 获取新图片尺寸 */ $size = $this->getnewsize($name,$width, $height,$imginfo); /* 获取新的图片资源 */ $newimg = $this->kidofimage($srcimg, $size,$imginfo); /* 通过本类的私有方法,保存缩略图并返回新缩略图的名称,以th_为前缀 */ return $this->createnewimage($newimg, $qz.$name,$imginfo); } /** * 为图片添加水印 * @param string $groundname 背景图片,即需要加水印的图片,暂只支持gif,jpg,png格式 * @param string $watername 图片水印,即作为水印的图片,暂只支持gif,jpg,png格式 * @param int $waterpos 水印位置,有10种状态,0为随机位置; * 1为顶端居左,2为顶端居中,3为顶端居右; * 4为中部居左,5为中部居中,6为中部居右; * 7为底端居左,8为底端居中,9为底端居右; * @param string $qz 加水印后的图片的文件名在原文件名前面加上这个前缀 * @return mixed 是生成水印后的图片名称,失败返回false */ function watermark($groundname, $watername, $waterpos=0, $qz=wa_){ /*获取水印图片是当前路径,还是指定了路径*/ $curpath = rtrim($this->path,/)./; $dir = dirname($watername); if($dir == .){ $wpath = $curpath; }else{ $wpath = $dir./; $watername = basename($watername); } /*水印图片和背景图片必须都要存在*/ if(file_exists($curpath.$groundname) && file_exists($wpath.$watername)){ $groundinfo = $this->getinfo($groundname); //获取背景信息 $waterinfo = $this->getinfo($watername, $dir); //获取水印图片信息 /*如果背景比水印图片还小,就会被水印全部盖住*/ if(!$pos = $this->position($groundinfo, $waterinfo, $waterpos)){ echo '水印不应该比背景图片小!'; return false; } $groundimg = $this->getimg($groundname, $groundinfo); //获取背景图像资源 $waterimg = $this->getimg($watername, $waterinfo, $dir); //获取水印图片资源 /* 调用私有方法将水印图像按指定位置复制到背景图片中 */ $groundimg = $this->copyimage($groundimg, $waterimg, $pos, $waterinfo); /* 通过本类的私有方法,保存加水图片并返回新图片的名称,默认以wa_为前缀 */ return $this->createnewimage($groundimg, $qz.$groundname, $groundinfo); }else{ echo '图片或水印图片不存在!'; return false; } } /** * 在一个大的背景图片中剪裁出指定区域的图片 * @param string $name 需要剪切的背景图片 * @param int $x 剪切图片左边开始的位置 * @param int $y 剪切图片顶部开始的位置 * @param int $width 图片剪裁的宽度 * @param int $height 图片剪裁的高度 * @param string $qz 新图片的名称前缀 * @return mixed 裁剪后的图片名称,失败返回false; */ function cut($name, $x, $y, $width, $height, $qz=cu_){ $imginfo=$this->getinfo($name); //获取图片信息 /* 裁剪的位置不能超出背景图片范围 */ if( (($x+$width) > $imginfo['width']) || (($y+$height) > $imginfo['height'])){ echo 裁剪的位置超出了背景图片范围!; return false; } $back = $this->getimg($name, $imginfo); //获取图片资源 /* 创建一个可以保存裁剪后图片的资源 */ $cutimg = imagecreatetruecolor($width, $height); /* 使用imagecopyresampled()函数对图片进行裁剪 */ imagecopyresampled($cutimg, $back, 0, 0, $x, $y, $width, $height, $width, $height); imagedestroy($back); /* 通过本类的私有方法,保存剪切图并返回新图片的名称,默认以cu_为前缀 */ return $this->createnewimage($cutimg, $qz.$name,$imginfo); } /* 内部使用的私有方法,用来确定水印图片的位置 */ private function position($groundinfo, $waterinfo, $waterpos){ /* 需要加水印的图片的长度或宽度比水印还小,无法生成水印 */ if( ($groundinfo[width]$posy); } /* 内部使用的私有方法,用于获取图片的属性信息(宽度、高度和类型) */ private function getinfo($name, $path=.) { $spath = $path==. ? rtrim($this->path,/)./ : $path.'/'; $data = getimagesize($spath.$name); $imginfo[width] = $data[0]; $imginfo[height] = $data[1]; $imginfo[type] = $data[2]; return $imginfo; } /*内部使用的私有方法, 用于创建支持各种图片格式(jpg,gif,png三种)资源 */ private function getimg($name, $imginfo, $path='.'){ $spath = $path==. ? rtrim($this->path,/)./ : $path.'/'; $srcpic = $spath.$name; switch ($imginfo[type]) { case 1: //gif $img = imagecreatefromgif($srcpic); break; case 2: //jpg $img = imagecreatefromjpeg($srcpic); break; case 3: //png $img = imagecreatefrompng($srcpic); break; default: return false; break; } return $img; } /* 内部使用的私有方法,返回等比例缩放的图片宽度和高度,如果原图比缩放后的还小保持不变 */ private function getnewsize($name, $width, $height, $imginfo){ $size[width] = $imginfo[width]; //原图片的宽度 $size[height] = $imginfo[height]; //原图片的高度 if($width path = rtrim($this->path,/)./; switch ($imginfo[type]) { case 1: //gif $result = imagegif($newimg, $this->path.$newname); break; case 2: //jpg $result = imagejpeg($newimg,$this->path.$newname); break; case 3: //png $result = imagepng($newimg, $this->path.$newname); break; } imagedestroy($newimg); return $newname; } /* 内部使用的私有方法,用于加水印时复制图像 */ private function copyimage($groundimg, $waterimg, $pos, $waterinfo){ imagecopy($groundimg, $waterimg, $pos[posx], $pos[posy], 0, 0, $waterinfo[width],$waterinfo[height]); imagedestroy($waterimg); return $groundimg; } /* 内部使用的私有方法,处理带有透明度的图片保持原样 */ private function kidofimage($srcimg, $size, $imginfo){ $newimg = imagecreatetruecolor($size[width], $size[height]); $otsc = imagecolortransparent($srcimg); if( $otsc >= 0 && $otsc < imagecolorstotal($srcimg)) { $transparentcolor = imagecolorsforindex( $srcimg, $otsc ); $newtransparentcolor = imagecolorallocate( $newimg, $transparentcolor['red'], $transparentcolor['green'], $transparentcolor['blue'] ); imagefill( $newimg, 0, 0, $newtransparentcolor ); imagecolortransparent( $newimg, $newtransparentcolor ); } imagecopyresized( $newimg, $srcimg, 0, 0, 0, 0, $size[width], $size[height], $imginfo[width], $imginfo[height] ); imagedestroy($srcimg); return $newimg; }}