php水印
封装PHP的图片水印的类。
1 <?php 2 header('Content-type:text/html;charset=utf8'); 3 $img = new Image(); 4 // $img->water('2a.jpg','logo.gif',0); 5 class Image{ 6 //路径 7 protected $path; 8 //是否启用随机名字 9 protected $isRandName; 10 //要保存的图像类型 11 protected $type; 12 13 //通过构造方法队成员属性进行初始化 14 function __construct($path='./',$isRandName=true,$type='png'){ 15 $this->path = $path; 16 $this->isRandName = $isRandName; 17 $this->type = $type; 18 } 19 //对外公开的水印方法 20 21 /** 22 * @param char $image 原图 23 * @param char $water 水印图片 24 * @param char $postion 位置 25 * @param int $tmp 透明度 26 * @param char $prefix 前缀 27 */ 28 function water($image,$water,$postion,$tmp=100,$prefix='water_'){ 29 //判断这两个图片是否存在 30 if(!file_exists($image)||!file_exists($water)){ 31 die('图片资源不存在'); 32 } 33 //得到原图和水印图片的宽高 34 $imageInfo = self::getImageInfo($image); 35 $waterInfo = self::getImageInfo($water); 36 //判断水印图片是否能贴上来 37 if (!$this->checkImage($imageInfo,$waterInfo)){ 38 die('水印图片太大'); 39 } 40 //打开图片 41 $imageRes = self::openAnyImage($image); 42 $waterRes = self::openAnyImage($water); 43 //根据水印图片的位置计算水印图片的坐标 44 $pos = $this->getPosition($postion,$imageInfo,$waterInfo); 45 //将水印图片贴过来 46 imagecopymerge($imageRes, $waterRes, $pos['x'], $pos['y'], 0, 0, $waterInfo["width"], $waterInfo["height"], $tmp); 47 //得到要保存图片的文件名 48 $newName = $this->createNewName($image,$prefix); 49 //得到保存图片的路径,也就是文件的全路径 50 $newPath = rtrim($this->path,'/').'/'.$newName; 51 //保存图片 52 $this->saveImage($imageRes,$newPath); 53 //销毁资源 54 imagedestroy($imageRes); 55 imagedestroy($waterRes); 56 57 //返回路径 58 return $newPath; 59 } 60 //保存图像资源 61 protected function saveImage($imageRes,$newPath){ 62 $func = 'image'.$this->type; 63 //通过变量函数进行保存 64 $func($imageRes,$newPath); 65 } 66 //得到文件名函数 67 protected function createNewName($imagePath,$prefix){ 68 if ($this->isRandName){ 69 $name = $prefix.uniqid().'.'.$this->type; 70 }else { 71 $name = $prefix.pathinfo($imagePath)['filename'].'.'.$this->type; 72 } 73 return $name; 74 } 75 //根据位置计算水印图片的坐标 76 protected function getPosition($postion,$imageInfo,$waterInfo){ 77 switch ($postion){ 78 case 1: 79 $x = 0; 80 $y = 0; 81 break; 82 case 2: 83 $x = ($imageInfo['width']-$waterInfo["width"])/2; 84 $y = 0; 85 break; 86 case 3: 87 $x = $imageInfo["width"]- $waterInfo["width"]; 88 $y = 0; 89 break; 90 case 4: 91 $x = 0; 92 $y = ($imageInfo["height"]-$waterInfo["height"])/2; 93 break; 94 case 5: 95 $x = ($imageInfo['width']-$waterInfo["width"])/2; 96 $y = ($imageInfo["height"]-$waterInfo["height"])/2; 97 break; 98 case 6: 99 $x = $imageInfo["width"]- $waterInfo["width"]; 100 $y = ($imageInfo["height"]-$waterInfo["height"])/2; 101 break; 102 case 7: 103 $x = 0; 104 $y = $imageInfo['height'] - $waterInfo["height"]; 105 break; 106 case 8: 107 $x = ($imageInfo['width']-$waterInfo["width"])/2; 108 $y = $imageInfo['height'] - $waterInfo["height"]; 109 break; 110 case 9: 111 $x = $imageInfo["width"]- $waterInfo["width"]; 112 $y = $imageInfo['height'] - $waterInfo["height"]; 113 break; 114 case 0: 115 $x = mt_rand(0, $imageInfo["width"]- $waterInfo["width"]); 116 $y = mt_rand(0, $imageInfo['height'] - $waterInfo["height"]); 117 break; 118 } 119 return ['x'=>$x , 'y'=>$y]; 120 } 121 protected function checkImage($imageInfo,$waterInfo){ 122 if (($waterInfo['width'] > $imageInfo['width'])||($waterInfo['height'] > $imageInfo['height'])){ 123 return false; 124 } 125 return true; 126 } 127 //静态方法。根据图片的路径得到图片的信息,宽度,高度、mime类型 128 static function getImageInfo($imagePath){ 129 $info = getimagesize($imagePath); 130 $data['width']=$info[0]; 131 $data['height']=$info[1]; 132 $data['mime'] = $info['mime']; 133 return $data; 134 } 135 static function openAnyImage($imagePath){ 136 //得到图像的mime类型 137 $mime = self::getImageInfo($imagePath)['mime']; 138 //根据不同的mime类型打开不同的图像 139 switch ($mime){ 140 case 'image/png': 141 $image = imagecreatefrompng($imagePath); 142 break; 143 case 'image/gif': 144 $image = imagecreatefromgif($imagePath); 145 break; 146 case 'image/jpeg': 147 $image = imagecreatefromjpeg($imagePath); 148 break; 149 case 'image/wbmp': 150 $image = imagecreatefromwbmp($imagePath); 151 break; 152 } 153 return $image; 154 } 155 156 }