這篇文章主要介紹了ThinkPHP打浮水印及設定浮水印位置的方法,結合執行個體形式分析了thinkPHP列印與設定浮水印的相關操作步驟與具體實現技巧,需要的朋友可以參考下
最近在用Thinkphp的打浮水印的功能,發現只能打在左下角。 PHP打浮水印功還是很容易的,最要是用到
複製代碼 代碼如下:
bool imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct )
將 src_im 映像中座標從 src_x,src_y 開始,寬度為 src_w,高度為 src_h 的一部分拷貝到 dst_im 映像中座標為 dst_x 和 dst_y 的位置上。兩映像將根據 pct 來決定合并程度,其值範圍從 0 到 100。當 pct = 0 時,實際上什麼也沒做,當為 100 時對於調色盤映像本函數和 imagecopy() 完全一樣,它對真彩色映像實現了 alpha 透明。
浮水印demo圖:
我需要把浮水印打到圖片的真中間,查看Thinkphp代碼。發現,作者居然是寫死了,我只能做一個修改
/*** 為圖片添加浮水印* @static public* @param string $source 原檔案名稱* @param string $water 浮水印圖片* @param string $$savename 添加浮水印後的圖片名* @param string $postion 浮水印的具體位置 leftbottom rightbottom lefttop righttop center <新增>* @param string $alpha 浮水印的透明度* @return void*/static public function water($source, $water, $savename=null,$postion="center", $alpha=80) {//檢查檔案是否存在if (!file_exists($source) || !file_exists($water))return false;//圖片資訊$sInfo = self::getImageInfo($source);$wInfo = self::getImageInfo($water);//如果圖片小於浮水印圖片,不產生圖片if ($sInfo["width"] < $wInfo["width"] || $sInfo['height'] < $wInfo['height']) return false; //建立映像 $sCreateFun = "imagecreatefrom" . $sInfo['type']; $sImage = $sCreateFun($source); $wCreateFun = "imagecreatefrom" . $wInfo['type']; $wImage = $wCreateFun($water); //設定映像的混色模式 imagealphablending($wImage, true); //映像位置,預設為右下角靠右對齊 $posArr = $this->WaterPostion($postion,$sInfo,$wInfo); //新增 //產生混合映像 imagecopymerge($sImage, $wImage, $posArr[0], $posArr[1], 0, 0, $wInfo['width'], $wInfo['height'], $alpha); //輸出映像 $ImageFun = 'Image' . $sInfo['type']; //如果沒有給出儲存檔案名稱,預設為原映像名 if (!$savename) { $savename = $source; @unlink($source); } //儲存映像 $ImageFun($sImage, $savename); imagedestroy($sImage); } private function WaterPostion($postion,$sInfo,$wInfo) { $posY = $sInfo["height"] - $wInfo["height"]; $posX = $sInfo["width"] - $wInfo["width"]; switch($postion) { case "rightbottom": return array($posX,$posY); break; case "leftbottom": return array($wInfo["width"],$posY); break; case "lefttop": return array($wInfo["width"],$wInfo["height"]); break; case "righttop": return array($posX,$wInfo["height"]); break; case "center": return array($posX/2,$posY/2); break; }}
總結:以上就是本篇文的全部內容,希望能對大家的學習有所協助。