| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
<?php class WaterGener{ private $default_text="Just for test"; private $default_waterpic="girl.jpg"; private $default_qulity=75; //預設使用的字型 private $font = ‘simhei.ttf‘; //定義字型 //預設的padding 的值 private $padding=5; /** 建構函式 **/ function __construct(){ } //擷取圖片類型 private function getImage($path){ if(!empty($path) && file_exists($path)) { $water_info = @getimagesize($path); $water_im; switch($water_info[2]) { //取得浮水印圖片的格式 case 1:$water_im = @imagecreatefromgif($path);break; case 2:$water_im = @imagecreatefromjpeg($path);break; case 3:$water_im = @imagecreatefrompng($path);break; default:return 1; } return $water_im; } return -1; } public function buildWaterImage($picture,$logo="",$savePath="demo.jpg"){ //需要判斷圖片的類型,浮水印圖片的類型 if(!empty($path) && file_exists($path))return -1; $logoImage =$this->getImage($logo===""?$this->default_waterpic:$logo); $photoImage =$this->getImage($picture); if($photoImage==-1){ echo "沒有找到圖片"; return; } imagealphablending($photoImage, true); $logo_size = getimagesize($logo); $logoW = $logo_size[0]; $logoH = $logo_size[1]; $picture_size = getimagesize($picture); ImageCopy($photoImage, $logoImage, $picture_size[0]-$logoW-$this->padding, $picture_size[1]-$logoH-$this->padding, 0, 0, $logoW, $logoH); ImageJPEG($photoImage,$savePath,$this->default_qulity); // output to browser or file ImageDestroy($photoImage); ImageDestroy($logoImage); echo "success..."; } public function buildWaterText($picture,$text="",$savePath="demo.jpg"){ //需要判斷 $photoImage = $this->getImage($picture); ImageAlphaBlending($photoImage, true); $picture_size = getimagesize($picture); $textcolor = imagecolorallocate($photoImage, 255,255, 255); //解決亂碼問題 //$text = iconv("GB2312", "UTF-8", $text); //將中文字轉換為UTF8 imagettftext($photoImage, 20, 0, $this->padding, $picture_size[1]-($this->padding*4), $textcolor, $this->font, $text);//將文字寫到圖片中 //imagestring($photoImage, 5, 0, 0,$text, $textcolor); ImageJPEG($photoImage,$savePath,$this->default_qulity); // output to browser ImageDestroy($photoImage); } } ?> |