PHP圖片水印類的封裝
封裝PHP的圖片水印的類,供大家參考,具體內(nèi)容如下
<?php header('Content-type:text/html;charset=utf8'); $img = new Image(); // $img->water('2a.jpg','logo.gif',0); class Image{ //路徑 protected $path; //是否啟用隨機(jī)名字 protected $isRandName; //要保存的圖像類型 protected $type; //通過(guò)構(gòu)造方法隊(duì)成員屬性進(jìn)行初始化 function __construct($path='./',$isRandName=true,$type='png'){ $this->path = $path; $this->isRandName = $isRandName; $this->type = $type; } //對(duì)外公開的水印方法 /** * @param char $image 原圖 * @param char $water 水印圖片 * @param char $postion 位置 * @param int $tmp 透明度 * @param char $prefix 前綴 */ function water($image,$water,$postion,$tmp=100,$prefix='water_'){ //判斷這兩個(gè)圖片是否存在 if(!file_exists($image)||!file_exists($water)){ die('圖片資源不存在'); } //得到原圖和水印圖片的寬高 $imageInfo = self::getImageInfo($image); $waterInfo = self::getImageInfo($water); //判斷水印圖片是否能貼上來(lái) if (!$this->checkImage($imageInfo,$waterInfo)){ die('水印圖片太大'); } //打開圖片 $imageRes = self::openAnyImage($image); $waterRes = self::openAnyImage($water); //根據(jù)水印圖片的位置計(jì)算水印圖片的坐標(biāo) $pos = $this->getPosition($postion,$imageInfo,$waterInfo); //將水印圖片貼過(guò)來(lái) 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; //通過(guò)變量函數(shù)進(jìn)行保存 $func($imageRes,$newPath); } //得到文件名函數(shù) protected function createNewName($imagePath,$prefix){ if ($this->isRandName){ $name = $prefix.uniqid().'.'.$this->type; }else { $name = $prefix.pathinfo($imagePath)['filename'].'.'.$this->type; } return $name; } //根據(jù)位置計(jì)算水印圖片的坐標(biāo) 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; } //靜態(tài)方法。根據(jù)圖片的路徑得到圖片的信息,寬度,高度、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']; //根據(jù)不同的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; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
微信網(wǎng)頁(yè)授權(quán)(OAuth2.0) PHP 源碼簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了微信網(wǎng)頁(yè)授權(quán)(OAuth2.0) PHP 源碼簡(jiǎn)單實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08php將session放入memcached的設(shè)置方法
這篇文章主要介紹了php將session放入memcached的設(shè)置方法,需要的朋友可以參考下2014-02-02詳解PHP編碼轉(zhuǎn)換函數(shù)應(yīng)用技巧
在PHP語(yǔ)言中mb_convert_encoding是一個(gè)PHP編碼轉(zhuǎn)換函數(shù),可以幫助我們用來(lái)實(shí)現(xiàn)對(duì)多字節(jié)字符串編碼的轉(zhuǎn)換。下面將會(huì)為大家進(jìn)行詳細(xì)介紹。2016-10-10ThinkPHP采用GET方式獲取中文參數(shù)查詢無(wú)結(jié)果的解決方法
這篇文章主要介紹了ThinkPHP采用GET方式獲取中文參數(shù)查詢無(wú)結(jié)果的解決方法,需要的朋友可以參考下2014-06-06php實(shí)現(xiàn)的一個(gè)很好用HTML解析器類可用于采集數(shù)據(jù)
下面就是這個(gè) HTML解析類及用法,下面的功能是采集www.opendir.cn這個(gè)網(wǎng)站的百度收錄數(shù)據(jù),需要的朋友可以測(cè)試下2013-09-09PHP解密支付寶小程序的加密數(shù)據(jù)、手機(jī)號(hào)的示例代碼
這篇文章主要介紹了PHP解密支付寶小程序的加密數(shù)據(jù)、手機(jī)號(hào)的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02