/baccarat/watermark.php

https://gitlab.com/hulupiao/baccarat · PHP · 225 lines · 175 code · 16 blank · 34 comment · 10 complexity · c7c09dcc0482f4b717c2f0f16141174e MD5 · raw file

  1. <?php
  2. /**
  3. * GD库 图片水印程序
  4. * @author hulupiao@sina.cn
  5. */
  6. class baccarat_watermark
  7. {
  8. public $type = 0;//0图片水印,1文字水印
  9. public $bgImg = 'water.png';//水印背景图片
  10. public $text = '';//水印背景文字
  11. public $font = '';//水印文字字体
  12. public $fontSize = 12;//水印文字字体大小
  13. public $fontColor = '#cccccc';//水印文字颜色
  14. public $image = '';//水印图片
  15. //0随机,1左上,2左下,3右上,4右下,5居中
  16. public $position = 0;//水印位置
  17. public $angle = 0;//水印角度
  18. public $newFile = '';
  19. public $debug = true;
  20. public function __construct($conf = array())
  21. {
  22. if(!extension_loaded('gd'))
  23. {
  24. $this->errorReporting('请先安装GD库', __METHOD__);
  25. }
  26. if(is_array($conf))
  27. {
  28. foreach($conf as $key => $value)
  29. {
  30. if(isset($this->$key))
  31. {
  32. $this->$key = $value;
  33. }
  34. }
  35. }
  36. }
  37. public function render()
  38. {
  39. if(!file_exists($this->image))
  40. {
  41. $this->errorReporting('图片不存在', __METHOD__);
  42. }
  43. $this->getImageInfo();
  44. if($this->type == 0)
  45. {
  46. $this->imgRender();
  47. }
  48. else
  49. {
  50. $this->fontRender();
  51. }
  52. switch($this->imageInfo[2])
  53. {
  54. case 1:
  55. imagegif($this->imageIm, $this->newFile);
  56. break;
  57. case 2;
  58. imagejpeg($this->imageIm, $this->newFile);
  59. break;
  60. case 3:
  61. imagepng($this->imageIm, $this->newFile);
  62. break;
  63. default:
  64. $this->errorReporting('不支持的图片格式', __METHOD__);
  65. }
  66. imagedestroy($this->imageIm);
  67. }
  68. //取图片信息
  69. public function getImageInfo()
  70. {
  71. $this->imageInfo = getimagesize($this->image);
  72. //取得水印图片的格式
  73. switch($this->imageInfo[2])
  74. {
  75. case 1:
  76. $this->imageIm = imagecreatefromgif($this->image);
  77. break;
  78. case 2:
  79. $this->imageIm = imagecreatefromjpeg($this->image);
  80. break;
  81. case 3:
  82. $this->imageIm = imagecreatefrompng($this->image);
  83. break;
  84. default:
  85. $this->errorReporting('不支持的图片格式', __METHOD__);
  86. }
  87. }
  88. public function getCoordinate($w, $h)
  89. {
  90. $imgW = $this->imageInfo[0];
  91. $imgH = $this->imageInfo[1];
  92. //
  93. $coordinate = array(
  94. 'x' => rand(0,($imgW - $w)),
  95. 'y' => rand(0,($imgH - $h)),
  96. );
  97. switch($this->position)
  98. {
  99. case 1://左上
  100. $coordinate['x'] = 0;
  101. $coordinate['y'] = 0;
  102. break;
  103. case 2://左下
  104. $coordinate['x'] = 0;
  105. $coordinate['y'] = $imgH - $h;
  106. break;
  107. case 3://右上
  108. $coordinate['x'] = $imgW - $w;
  109. $coordinate['y'] = 0;
  110. break;
  111. case 4://右下
  112. $coordinate['x'] = $imgW - $w;
  113. $coordinate['y'] = $imgH - $h;
  114. break;
  115. case 5://居中
  116. $coordinate['x'] = ($imgW - $w) / 2;
  117. $coordinate['y'] = ($imgH - $h) / 2;
  118. break;
  119. }
  120. return $coordinate;
  121. }
  122. //图片水印
  123. public function imgRender()
  124. {
  125. if(!file_exists($this->bgImg))
  126. {
  127. $this->errorReporting('不存在水印图片', __METHOD__);
  128. }
  129. //水印坐标位置
  130. $bgInfo = getimagesize($this->bgImg);
  131. $w = $bgInfo[0];
  132. $h = $bgInfo[1];
  133. $coordinate = $this->getCoordinate($w, $h);
  134. //取得背景图片的格式
  135. switch($bgInfo[2])
  136. {
  137. case 1:
  138. $bgIm = imagecreatefromgif($this->bgImg);
  139. break;
  140. case 2:
  141. $bgIm = imagecreatefromjpeg($this->bgImg);
  142. break;
  143. case 3:
  144. $bgIm = imagecreatefrompng($this->bgImg);
  145. break;
  146. default:
  147. $this->errorReporting('不支持的水印图片格式', __METHOD__);
  148. }
  149. //设定图像的混色模式
  150. imagealphablending($this->imageIm, true);
  151. //生成水印图片
  152. $rs = imagecopy($this->imageIm, $bgIm, $coordinate['x'], $coordinate['y'], 0, 0, $w, $h);
  153. //var_dump($rs);
  154. imagedestroy($bgIm);
  155. }
  156. //文字水印
  157. public function fontRender()
  158. {
  159. if(empty($this->text))
  160. {
  161. $this->errorReporting('未定义水印文字', __METHOD__);
  162. }
  163. if(!file_exists($this->font))
  164. {
  165. $this->errorReporting('未指定水印文字字体', __METHOD__);
  166. }
  167. //原图信息
  168. $this->getImageInfo();
  169. //颜色
  170. $R = hexdec(substr($this->fontColor,1,2));
  171. $G = hexdec(substr($this->fontColor,3,2));
  172. $B = hexdec(substr($this->fontColor,5));
  173. $color = imagecolorallocate($this->imageIm, $R, $G, $B);
  174. //水印坐标位置
  175. $box = imagettfbbox($this->fontSize, $this->angle, $this->font, $this->text);
  176. $w = $box[2] - $box[6];
  177. $h = $box[3] - $box[7];
  178. $coordinate = $this->getCoordinate($w, $h);
  179. //
  180. imagettftext ($this->imageIm, $this->fontSize, $this->angle, $coordinate['x'], $coordinate['y'], $color, $this->font, $this->text);
  181. }
  182. public function errorReporting($msg, $method)
  183. {
  184. if($this->debug)
  185. {
  186. trigger_error($method.'--'.$msg, E_USER_ERROR);
  187. exit;
  188. }
  189. else
  190. {
  191. $this->error = $msg;
  192. }
  193. }
  194. }
  195. /*
  196. //Example
  197. ini_set('display_errors', 1);
  198. error_reporting(E_ALL);
  199. $imageWater_obj = new imageWater();
  200. $imageWater_obj->image = 'sfz2.jpg';
  201. $imageWater_obj->type = 0;
  202. $imageWater_obj->position = 5;
  203. $imageWater_obj->text = '仅限于购买房屋使用';//仅限于域名实名使用
  204. $imageWater_obj->font = 'c:\windows\fonts\simhei.ttf';
  205. $imageWater_obj->fontSize = 50;
  206. $imageWater_obj->newFile = 'test.jpg';
  207. $imageWater_obj->render();
  208. */