參考phpcms所寫的圖片新增水印功能
阿新 • • 發佈:2019-02-19
<?php class water{ //待新增圖片資源,水印資源 public $src_im,$water_im; //水印位置 public $posW,$posH; //待加水印圖片寬高 private $fileW,$fileH; //水印圖片寬高和按比例生成的寬高 private $waterW,$waterH,$waterNW,$waterNH; //水印尺寸 private $size; //建構函式 public function __construct($file,$water,$size,$pos){ $this->size = $this->setSize($size); $this->setFileInfo($file); $this->setWaterInfo($water); $this->setPos($pos); $this->dealImage(); } /** * 得到水印大小 * @param $size int 尺寸 */ private function setSize($size){ switch($size){ case 1: return 0.5; break; case 2: return 0.75; break; case 3: return 0.98; break; default: return 0.75; } } /** * 得到圖片寬高和圖片資源 * @param $file string 待加水印圖片 */ private function setFileInfo($file){ $arr = getimagesize($file); $this->src_im = $this->setSourse($file,$arr['mime']); $this->fileW = $arr[0]; $this->fileH = $arr[1]; return true; } /** * 得到水印圖片寬高和按比例縮小的寬高和水印圖片資源 * @param $water string 水印圖片 */ private function setWaterInfo($water){ $arr = getimagesize($water); $this->water_im = $this->setSourse($water,$arr['mime']); $this->waterW = $arr[0]; $this->waterH = $arr[1]; //按比例得到新水印的寬高 $this->waterNW = $this->fileW * $this->size; $this->waterNH = $this->waterNW * ($this->waterH / $this->waterW); return true; } /** * 生成水印 */ private function dealImage(){ imagecopyresized($this->src_im,$this->water_im,$this->posW,$this->posH,0,0,$this->waterNW,$this->waterNH,$this->waterW,$this->waterH); } /** * 按字尾得到圖片資源 * @param $file string 圖片 * @param $ext string 圖片字尾 */ private function setSourse($file,$ext){ switch($ext){ case 'image/jpeg': return imagecreatefromjpeg($file); break; case 'image/png': return imagecreatefrompng($file); break; case 'image/gif': return imagecreatefromgif($file); break; } } /** * 設定水印生成位置 * @param $pos int 位置 */ private function setPos($pos){ switch($pos){ case 1: $this->posW = ($this->fileW - $this->waterNW) / 5; $this->posH = ($this->fileW - $this->waterNW) / 5; break; case 2: $this->posW = ($this->fileW - $this->waterNW) / 2; $this->posH = ($this->fileW - $this->waterNW) / 5; break; case 3: $this->posW = $this->fileW - $this->waterNW; $this->posH = ($this->fileW - $this->waterNW) / 5; break; case 4: $this->posW = ($this->fileW - $this->waterNW) / 5; $this->posH = ($this->fileH - $this->waterNH) / 2; break; case 5: $this->posW = ($this->fileW - $this->waterNW) / 2; $this->posH = ($this->fileH - $this->waterNH) / 2; break; case 6: $this->posW = $this->fileW - $this->waterNW; $this->posH = ($this->fileH - $this->waterNH) / 2; break; case 7: $this->posW = ($this->fileW - $this->waterNW) / 5; $this->posH = ($this->fileH - $this->waterNH) / 1.1; break; case 8: $this->posW = ($this->fileW - $this->waterNW) / 2; $this->posH = ($this->fileH - $this->waterNH) / 1.1; break; case 9: $this->posW = $this->fileW - $this->waterNW; $this->posH = ($this->fileH - $this->waterNH) / 1.1; break; } } /** * 返回生成水印後的圖片 */ public function getImage(){ return $this->src_im; } } if(isset($_POST['submit'])){ if(isset($_POST['file'])&&isset($_POST['water'])&&isset($_POST['size'])&&isset($_POST['pos'])){ $file = $_POST['file']; $water = $_POST['water']; $size = $_POST['size']; $pos = $_POST['pos']; $im = new water($file,$water,$size,$pos); imagejpeg($im->getImage(),'newImage.jpg'); }else{ echo "<script>alert('資料不完整')</script>"; } } ?> <html> <head> <title>水印生成器</title> </head> <body> <form name="myform" action="" method="POST"> <p>待生成水印圖片:</p> <input type="text" name="file" value="1.jpeg"/><br /> <p>水印圖片:</p> <input type="text" name="water" value="logo.png"/><br /> <p>大小:</p> <input type="radio" name="size" value="1" />小 <input type="radio" name="size" value="2" />中 <input type="radio" name="size" value="3" />大<br /> <p>位置:</p> <table border="1px"> <tr> <td><input type="radio" name="pos" value="1" />左上</td> <td><input type="radio" name="pos" value="2" />中上</td> <td><input type="radio" name="pos" value="3" />右上</td> </tr> <tr> <td><input type="radio" name="pos" value="4" />左中</td> <td><input type="radio" name="pos" value="5" />中間</td> <td><input type="radio" name="pos" value="6" />右中</td> </tr> <tr> <td><input type="radio" name="pos" value="7" />左下</td> <td><input type="radio" name="pos" value="8" />中下</td> <td><input type="radio" name="pos" value="9" />右下</td> </tr> </table> <input type="submit" name="submit" value="生成" /> </form> <hr /> <img src="newImage.jpg" /> </body> </html>