php常用圖片處理類
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); //調用Function Compute出浮水印載入的位置 $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); } } ?>