一个php图片处理类,可以缩放图片,可以给图片加水印,有需要的朋友,可以参考下。代码如下:
path=rtrim($path, /)./; } /* 对图片进行缩放 * * 参数$name: 是需要处理的图片名称 * 参数$width:是缩放后的宽度 * 参数$height:是缩放后的高度 * 参数$qz: 是新图片的名称前缀 * 返回值:就是缩放后的图片名称,失败则返回false * */ function thumb($name, $width, $height, $qz=th_){ //获取图片信息 $imginfo=$this->getinfo($name); //图片的宽度,高度,类型 //获取图片资源, 各种类型的图片都可以创建资源 jpg, gif, png $srcimg=$this->getimg($name, $imginfo); //获取计算图片等比例之后的大小, $size[width], $size[height] $size=$this->getnewsize($name, $width, $height, $imginfo); //获取新的图片资源, 处理一下gif透明背景 $newimg=$this->kidofimage($srcimg, $size, $imginfo); //另存为一个新的图片,返回新的缩放后的图片名称 return $this->createnewimage($newimg, $qz.$name, $imginfo); } private function createnewimage($newimg, $newname, $imginfo){ 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 $return=imagepng($newimg, $this->path.$newname); break; } imagedestroy($newimg); return $newname; } private function kidofimage($srcimg, $size, $imginfo){ $newimg=imagecreatetruecolor($size[width], $size[height]); $otsc=imagecolortransparent($srcimg); if($otsc >=0 && $otsc path.$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; } return $img; } /* 功能:为图片加水印图片 * 参数$groundname: 背景图片,即需要加水印的图片 * 参数$watername: 水钱图片 * 参数#aterpost:水印位置, 10种状态, * 0为随机位置 * * 1. 为顶端居左 2. 为顶端居中 3 为顶端居右 * 4 为中部居左 5. 为中部居中 6 为中部居右 * 7 . 为底端居左 8. 为底端居中, 9. 为底端居右 * * 参数$qz : 是加水印后的图片名称前缀 * 返回值:就是处理后图片的名称 * */ function watermark($groundname, $watername, $waterpos=0, $qz=wa_){ if(file_exists($this->path.$groundname) && file_exists($this->path.$watername)){ $groundinfo=$this->getinfo($groundname); $waterinfo=$this->getinfo($watername); //水印的位置 if(!$pos=$this->position($groundinfo, $waterinfo, $waterpos)){ echo 水印不应该比背景图片小!; return; } $groundimg=$this->getimg($groundname, $groundinfo); $waterimg=$this->getimg($watername, $waterinfo); $groundimg=$this->copyimage($groundimg, $waterimg, $pos, $waterinfo); return $this->createnewimage($groundimg, $qz.$groundname, $groundinfo); }else{ echo 图片或水印图片不存在; return false; } } 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 position($groundinfo, $waterinfo, $waterpos){ //需要背景比水印图片大 if(($groundinfo[width]$posy); } }