PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/upload/src/library/image/PwImageWatermark.php

https://gitlab.com/wuhang2003/phpwind
PHP | 244 lines | 171 code | 26 blank | 47 comment | 36 complexity | b477145c8abefc6a71d8f7c9a3cf9748 MD5 | raw file
  1. <?php
  2. defined('WEKIT_VERSION') || exit('Forbidden');
  3. /**
  4. * 缩略水印
  5. *
  6. * the last known user to change this file in the repository <$LastChangedBy: jieyin $>
  7. * @author Jianmin Chen <sky_hold@163.com>
  8. * @version $Id: PwImageWatermark.php 24724 2013-02-17 10:05:52Z jieyin $
  9. * @package lib.image
  10. */
  11. class PwImageWatermark {
  12. protected $image;
  13. protected $water;
  14. protected $dstfile; //目标文件
  15. protected $position; //水印位置 1-9 分别为九宫格的对应位置
  16. protected $transparency; //水印透明度
  17. protected $quality; //图片质量
  18. protected $type; //水印类型 <1.图片水印 2.文字水印>
  19. protected $file;
  20. protected $text; //水印文字
  21. protected $fontfamily; //水印字体
  22. protected $fontsize; //字体大小
  23. protected $fontcolor; //字体颜色
  24. protected $fontfile;
  25. public function __construct(PwImage $image) {
  26. $this->image = $image;
  27. }
  28. public function setDstfile($file) {
  29. $this->dstfile = $file;
  30. return $this;
  31. }
  32. /**
  33. * 设置位置
  34. */
  35. public function setPosition($position) {
  36. $this->position = intval($position);
  37. return $this;
  38. }
  39. /**
  40. * 设置水印透明度
  41. */
  42. public function setTransparency($transparency) {
  43. $this->transparency = intval($transparency);
  44. return $this;
  45. }
  46. /**
  47. * 设置图片质量
  48. */
  49. public function setQuality($quality) {
  50. $this->quality = intval($quality);
  51. return $this;
  52. }
  53. /**
  54. * 设置缩略方式 <1.图片水印 1.文字水印>
  55. */
  56. public function setType($type) {
  57. $this->type = $type;
  58. return $this;
  59. }
  60. /**
  61. * 设置水印图片
  62. */
  63. public function setFile($file) {
  64. $this->file = $file;
  65. return $this;
  66. }
  67. /**
  68. * 设置水印文字
  69. */
  70. public function setText($text) {
  71. $this->text = $text;
  72. return $this;
  73. }
  74. /**
  75. * 设置水印字体
  76. */
  77. public function setFontfamily($fontfamily) {
  78. $this->fontfamily = $fontfamily;
  79. return $this;
  80. }
  81. /**
  82. * 设置水印字体大小
  83. */
  84. public function setFontsize($fontsize) {
  85. $this->fontsize = $fontsize;
  86. return $this;
  87. }
  88. /**
  89. * 设置水印字体颜色
  90. */
  91. public function setFontcolor($fontcolor) {
  92. $this->fontcolor = $fontcolor;
  93. return $this;
  94. }
  95. public function getPosition($water) {
  96. if ($this->position >= 1 && $this->position <= 9) {
  97. $px = ($this->position - 1) % 3;
  98. $py = intval(($this->position - 1) / 3);
  99. switch ($px) {
  100. case 0:
  101. $offsetX = 5;break;
  102. case 1:
  103. $offsetX = ($this->image->width - $water->width) / 2;break;
  104. default:
  105. $offsetX = $this->image->width - $water->width - 5;
  106. }
  107. switch ($py) {
  108. case 0:
  109. $offsetY = 5;break;
  110. case 1:
  111. $offsetY = ($this->image->height - $water->height) / 2;break;
  112. default:
  113. $offsetY = $this->image->height - $water->height - 5;
  114. }
  115. } else {
  116. $offsetX = rand(5, $this->image->width - $water->width - 5);
  117. $offsetY = rand(5, $this->image->height - $water->height - 5);
  118. }
  119. return array($offsetX, $offsetY);
  120. }
  121. public function initWaterWay() {
  122. if ($this->type == 1) {
  123. $water = new PwImage(Wind::getRealDir('REP:mark') . '/' . $this->file);
  124. if (!$water->isImage() || !$water->getSource()) {
  125. return false;
  126. }
  127. } else {
  128. if (!$this->text || strlen($this->fontcolor) != 7) {
  129. return false;
  130. }
  131. empty($this->fontfamily) && $this->fontfamily = 'en_arial.ttf';
  132. empty($this->fontsize) && $this->fontsize = 12;
  133. $this->fontfile = Wind::getRealDir('REP:font') . '/' . $this->fontfamily;
  134. $temp = imagettfbbox($this->fontsize, 0, $this->fontfile, $this->text); //取得使用 TrueType 字体的文本的范围
  135. $water = new stdClass();
  136. $water->width = $temp[2] - $temp[6];
  137. $water->height = $temp[3] - $temp[7];
  138. unset($temp);
  139. }
  140. return $water;
  141. }
  142. /**
  143. * 生成缩略图
  144. */
  145. public function execute() {
  146. if (!$this->image->getSource()) {
  147. return false;
  148. }
  149. if (!function_exists('image' . $this->image->type)) {
  150. return false;
  151. }
  152. if (($water = $this->initWaterWay()) === false) {
  153. return false;
  154. }
  155. list($offsetX, $offsetY) = $this->getPosition($water);
  156. $source = $this->image->getSource();
  157. if ($this->image->type == 'png') {
  158. imagealphablending($source, false);
  159. imagesavealpha($source, true);
  160. } else {
  161. imagealphablending($source, true);
  162. }
  163. /*
  164. if ($this->image->type != 'png') {
  165. $tmp = imagecreatetruecolor($this->image->width, $this->image->height);
  166. imagecopy($tmp, $source, 0, 0, 0, 0, $this->image->width, $this->image->height);
  167. $source = $tmp;
  168. }*/
  169. if ($this->type == 1) {
  170. $source = $this->doImage($source, $water, $offsetX, $offsetY, $this->transparency);
  171. } else {
  172. $source = $this->doText($source, $water, $offsetX, $offsetY);
  173. }
  174. $this->dstfile || $this->dstfile = $this->image->filename;
  175. $this->makeImage($this->image->type, $source, $this->dstfile, $this->quality);
  176. imagedestroy($source);
  177. return true;
  178. }
  179. public function doImage($source, $water, $offsetX, $offsetY, $transparency) {
  180. $ws = $water->getSource();
  181. if ($water->type == 'png') {
  182. //imagealphablending($source, true);
  183. imagecolortransparent($source, imagecolorallocatealpha($source, 0, 0, 0, 0));
  184. imagecopyresampled($source, $ws, $offsetX, $offsetY, 0, 0, $water->width, $water->height, $water->width, $water->height);
  185. //imagecopy($source, $ws, $offsetX, $offsetY, 0, 0, $water->width, $water->height);
  186. } else {
  187. imagealphablending($ws, true);
  188. imagecopymerge($source, $ws, $offsetX, $offsetY, 0, 0, $water->width, $water->height, $transparency);
  189. }
  190. imagedestroy($ws);
  191. unset($water);
  192. return $source;
  193. }
  194. public function doText($source, $water, $offsetX, $offsetY) {
  195. $R = hexdec(substr($this->fontcolor, 1, 2));
  196. $G = hexdec(substr($this->fontcolor, 3, 2));
  197. $B = hexdec(substr($this->fontcolor, 5));
  198. //imagestring($sourcedb['source'],$w_font,$wX,$wY,$w_text,imagecolorallocate($sourcedb['source'],$R,$G,$B));
  199. if (strpos($this->fontfamily, 'ch') === 0 && strtoupper(Wekit::V('charset')) != 'UTF-8') {
  200. $this->text = WindConvert::WindConvert($this->text, 'UTF-8', Wekit::V('charset'));
  201. }
  202. imagettftext($source, $this->fontsize, 0, $offsetX, $offsetY + $water->height, imagecolorallocate($source, $R, $G, $B), $this->fontfile, $this->text);
  203. unset($water);
  204. return $source;
  205. }
  206. public function makeImage($type, $image, $filename, $quality = '90') {
  207. $makeimage = 'image' . $type;
  208. if (!function_exists($makeimage)) {
  209. return false;
  210. }
  211. if ($type == 'jpeg') {
  212. $makeimage($image, $filename, $quality);
  213. } else {
  214. $makeimage($image, $filename);
  215. }
  216. return true;
  217. }
  218. }