這篇文章主要為大家詳細介紹了PHP圖片浮水印類的封裝,具有一定的參考價值,感興趣的小夥伴們可以參考一下
封裝PHP的圖片浮水印的類,供大家參考,具體內容如下
<?phpheader('Content-type:text/html;charset=utf8');$img = new Image();// $img->water('2a.jpg','logo.gif',0);class Image{ //路徑 protected $path; //是否啟用隨機名字 protected $isRandName; //要儲存的映像類型 protected $type; //通過構造方法隊成員屬性進行初始化 function __construct($path='./',$isRandName=true,$type='png'){ $this->path = $path; $this->isRandName = $isRandName; $this->type = $type; } //對外公開的浮水印方法 /** * @param char $image 原圖 * @param char $water 浮水印圖片 * @param char $postion 位置 * @param int $tmp 透明度 * @param char $prefix 首碼 */ function water($image,$water,$postion,$tmp=100,$prefix='water_'){ //判斷這兩個圖片是否存在 if(!file_exists($image)||!file_exists($water)){ die('圖片資源不存在'); } //得到原圖和浮水印圖片的寬高 $imageInfo = self::getImageInfo($image); $waterInfo = self::getImageInfo($water); //判斷浮水印圖片是否能貼上來 if (!$this->checkImage($imageInfo,$waterInfo)){ die('浮水印圖片太大'); } //開啟圖片 $imageRes = self::openAnyImage($image); $waterRes = self::openAnyImage($water); //根據浮水印圖片的位置計算浮水印圖片的座標 $pos = $this->getPosition($postion,$imageInfo,$waterInfo); //將浮水印圖片貼過來 imagecopymerge($imageRes, $waterRes, $pos['x'], $pos['y'], 0, 0, $waterInfo["width"], $waterInfo["height"], $tmp); //得到要儲存圖片的檔案名稱 $newName = $this->createNewName($image,$prefix); //得到儲存圖片的路徑,也就是檔案的全路徑 $newPath = rtrim($this->path,'/').'/'.$newName; //儲存圖片 $this->saveImage($imageRes,$newPath); //銷毀資源 imagedestroy($imageRes); imagedestroy($waterRes); //返迴路徑 return $newPath; } //儲存映像資源 protected function saveImage($imageRes,$newPath){ $func = 'image'.$this->type; //通過變數函數進行儲存 $func($imageRes,$newPath); } //得到檔案名稱函數 protected function createNewName($imagePath,$prefix){ if ($this->isRandName){ $name = $prefix.uniqid().'.'.$this->type; }else { $name = $prefix.pathinfo($imagePath)['filename'].'.'.$this->type; } return $name; } //根據位置計算浮水印圖片的座標 protected function getPosition($postion,$imageInfo,$waterInfo){ switch ($postion){ case 1: $x = 0; $y = 0; break; case 2: $x = ($imageInfo['width']-$waterInfo["width"])/2; $y = 0; break; case 3: $x = $imageInfo["width"]- $waterInfo["width"]; $y = 0; break; case 4: $x = 0; $y = ($imageInfo["height"]-$waterInfo["height"])/2; break; case 5: $x = ($imageInfo['width']-$waterInfo["width"])/2; $y = ($imageInfo["height"]-$waterInfo["height"])/2; break; case 6: $x = $imageInfo["width"]- $waterInfo["width"]; $y = ($imageInfo["height"]-$waterInfo["height"])/2; break; case 7: $x = 0; $y = $imageInfo['height'] - $waterInfo["height"]; break; case 8: $x = ($imageInfo['width']-$waterInfo["width"])/2; $y = $imageInfo['height'] - $waterInfo["height"]; break; case 9: $x = $imageInfo["width"]- $waterInfo["width"]; $y = $imageInfo['height'] - $waterInfo["height"]; break; case 0: $x = mt_rand(0, $imageInfo["width"]- $waterInfo["width"]); $y = mt_rand(0, $imageInfo['height'] - $waterInfo["height"]); break; } return ['x'=>$x , 'y'=>$y]; } protected function checkImage($imageInfo,$waterInfo){ if (($waterInfo['width'] > $imageInfo['width'])||($waterInfo['height'] > $imageInfo['height'])){ return false; } return true; } //靜態方法。根據圖片的路徑得到圖片的資訊,寬度,高度、mime類型 static function getImageInfo($imagePath){ $info = getimagesize($imagePath); $data['width']=$info[0]; $data['height']=$info[1]; $data['mime'] = $info['mime']; return $data; } static function openAnyImage($imagePath){ //得到映像的mime類型 $mime = self::getImageInfo($imagePath)['mime']; //根據不同的mime類型開啟不同的映像 switch ($mime){ case 'image/png': $image = imagecreatefrompng($imagePath); break; case 'image/gif': $image = imagecreatefromgif($imagePath); break; case 'image/jpeg': $image = imagecreatefromjpeg($imagePath); break; case 'image/wbmp': $image = imagecreatefromwbmp($imagePath); break; } return $image; } }