<?php class Image { private $path; //構造方法用來對圖片所在位置進行初使化 function __construct($path="./"){ $this->path=rtrim($path, "/")."/"; $this -> 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 Header("Content-type: image/gif"); $result=imageGif($newImg); break; case 2://jpg Header("Content-type: image/jpg"); $result=imageJPEG($newImg); break; case 3://png Header("Content-type: image/png"); $return=imagepng($newImg); break; } imagedestroy($newImg); return $newName; } private function kidOfImage($srcImg, $size, $imgInfo){ $newImg=imagecreatetruecolor($size["width"], $size["height"]); $otsc=imagecolortransparent($srcImg); if($otsc >=0 && $otsc <= imagecolorstotal($srcImg)){ $tran=imagecolorsforindex($srcImg, $otsc); $newt=imagecolorallocate($newImg, $tran["red"], $tran["green"], $tran["blue"]); imagefill($newImg, 0, 0, $newt); imagecolortransparent($newImg, $newt); } imagecopyresized($newImg, $srcImg, 0, 0, 0, 0, $size["width"], $size["height"], $imgInfo["width"], $imgInfo["height"]); imagedestroy($srcImg); return $newImg; } private function getNewSize($name, $width, $height, $imgInfo){ $size["width"]=$imgInfo["width"]; $size["height"]=$imgInfo["height"]; //縮放的寬度若是比原圖小才從新設置寬度 if($width < $imgInfo["width"]){ $size["width"]=$width; } //縮放的高度若是比原圖小才從新設置高度 if($height < $imgInfo["height"]){ $size["height"]=$height; } //圖片等比例縮放的算法 if($imgInfo["width"]*$size["width"] > $imgInfo["height"] * $size["height"]){ $size["height"]=round($imgInfo["height"]*$size["width"]/$imgInfo["width"]); }else{ $size["width"]=round($imgInfo["width"]*$size["height"]/$imgInfo["height"]); } return $size; } public function getInfo($name){ $data=getImageSize($this->path.$name); $imageInfo["width"]=$data[0]; $imageInfo["height"]=$data[1]; $imageInfo["type"]=$data[2]; return $imageInfo; } private function getImg($name, $imgInfo){ $srcPic=$this->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_"){ $groundInfo=$this->getInfo($groundName); $waterInfo=$this->getInfo($waterName); //水印的位置 if(!$pos=$this->position($groundInfo, $waterInfo, $waterPos)){ return "Picture too small!"; } $groundImg=$this->getImg($groundName, $groundInfo); $waterImg=$this->getImg($waterName, $waterInfo); $groundImg=$this->copyImage($groundImg, $waterImg, $pos, $waterInfo); return $this->createNewImage($groundImg, $qz.$groundName, $groundInfo); } 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"]< $waterInfo["width"]) ||($groundInfo["height"] < $waterInfo["height"])){ return false; } switch($waterPos){ case 1: $posX=0; $posY=0; break; case 2: $posX=($groundInfo["width"]-$waterInfo["width"])/2; $posY=0; break; case 3: $posX=$groundInfo["width"]-$waterInfo["width"]; $posY=0; break; case 4: $posX=0; $posY=($groundInfo["height"]-$waterInfo["height"]) /2; break; case 5: $posX=($groundInfo["width"]-$waterInfo["width"])/2; $posY=($groundInfo["height"]-$waterInfo["height"]) /2; break; case 6: $posX=$groundInfo["width"]-$waterInfo["width"]; $posY=($groundInfo["height"]-$waterInfo["height"]) /2; break; case 7: $posX=0; $posY=$groundInfo["height"]-$waterInfo["height"]; break; case 8: $posX=($groundInfo["width"]-$waterInfo["width"])/2; $posY=$groundInfo["height"]-$waterInfo["height"]; break; case 9: $posX=$groundInfo["width"]-$waterInfo["width"]; $posY=$groundInfo["height"]-$waterInfo["height"]; break; case 0: default: $posX=rand(0, ($groundInfo["width"]-$waterInfo["width"])); $posY=rand(0, ($groundInfo["height"]-$waterInfo["height"])); break; } return array("posX"=>$posX, "posY"=>$posY); } }