PHP添加PNG图片背景透明水印操作类定义与用法示例

这篇文章主要介绍了PHP添加PNG图片背景透明水印操作类定义与用法,涉及php操作图片的显示、保存、压缩、水印添加等相关操作技巧,需要的朋友可以参考下。

本文实例讲述了PHP添加PNG图片背景透明水印操作类定义与用法,分享给大家供大家参考,具体如下:

图片相关操作类

  1. class ImageTool
  2. {
  3. private $imagePath;//图片路径
  4. private $outputDir;//输出文件夹
  5. public $memoryImg;//内存图像
  6. public $path;
  7. public function __construct($imagePath, $outputDir = null)
  8. {
  9. $this->imagePath = $imagePath;
  10. $this->outputDir = $outputDir;
  11. $this->memoryImg = null;
  12. $this->path = null;
  13. }
  14. /**
  15. * 显示内存中的图片
  16. * @param $image
  17. */
  18. public function showImage()
  19. {
  20. if ($this->memoryImg != null) {
  21. $info = getimagesize($this->imagePath);
  22. $type = image_type_to_extension($info[2], false);
  23. header('Content-type:' . $info['mime']);
  24. $funs = "image{$type}";
  25. $funs($this->memoryImg);
  26. imagedestroy($this->memoryImg);
  27. $this->memoryImg = null;
  28. }
  29. }
  30. /**
  31. * 保存图片
  32. * @param $image 图片路径
  33. * @return string
  34. */
  35. private function saveImage($image)
  36. {
  37. $info = getimagesize($this->imagePath);
  38. $type = image_type_to_extension($info[2], false);
  39. $funs = "image{$type}";
  40. if (emptyempty($this->outputDir)) {
  41. $funs($image, md5($this->imagePath) . '.' . $type);
  42. return md5($this->imagePath) . '.' . $type;
  43. } else {
  44. $funs($image, $this->outputDir . md5($this->imagePath) . '.' . $type);
  45. return $this->outputDir . md5($this->imagePath) . '.' . $type;
  46. }
  47. }
  48. /**
  49. * 压缩图片
  50. * @param $width 压缩后宽度
  51. * @param $height 压缩后高度
  52. * @param bool $output 是否输出文件
  53. * @return resource
  54. */
  55. public function compressImage($width, $height, $output = false)
  56. {
  57. $image = null;
  58. $info = getimagesize($this->imagePath);
  59. $type = image_type_to_extension($info[2], false);
  60. $fun = "imagecreatefrom{$type}";
  61. $image = $fun($this->imagePath);
  62. imagesavealpha($image,true);//
  63. $thumbnail = imagecreatetruecolor($width, $height);
  64. imagealphablending($thumbnail,false);//这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;
  65. imagesavealpha($thumbnail,true);//
  66. imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $width, $height, $info[0], $info[1]);
  67. imagedestroy($image);
  68. if ($output) {
  69. $path = $this->saveImage($thumbnail);
  70. $this->path = $path;
  71. }
  72. $this->memoryImg = $thumbnail;
  73. return $this;
  74. }
  75. /**
  76. * 为图像添加文字标记
  77. *
  78. * @param $content 文本内容
  79. * @param $size 字体大小
  80. * @param $font 字体样式
  81. * @param bool $output 是否输出文件
  82. * @return $this
  83. */
  84. public function addTextmark($content, $size, $font, $output = false)
  85. {
  86. $info = getimagesize($this->imagePath);
  87. $type = image_type_to_extension($info[2], false);
  88. $fun = "imagecreatefrom{$type}";
  89. $image = $fun($this->imagePath);
  90. $color = imagecolorallocatealpha($image, 0, 0, 0, 80);
  91. $posX = imagesx($image) - strlen($content) * $size / 2;
  92. $posY = imagesy($image) - $size / 1.5;
  93. imagettftext($image, $size, 0, $posX, $posY, $color, $font, $content);
  94. if ($output) {
  95. $this->saveImage($image);
  96. }
  97. $this->memoryImg = $image;
  98. return $this;
  99. }
  100. /**
  101. * 为图片添加水印
  102. *
  103. * @param $watermark 水印图片路径
  104. * @param $alpha 水印透明度(0-100)
  105. * @param bool $output 是否输出文件
  106. * @return $this
  107. */
  108. public function addWatermark($watermark, $alpha, $output = false)
  109. {
  110. $image_info = getimagesize($this->imagePath);
  111. $image_type = image_type_to_extension($image_info[2], false);
  112. $image_fun = "imagecreatefrom{$image_type}";
  113. $image = $image_fun($this->imagePath);
  114. $mark_info = getimagesize($watermark);
  115. $mark_type = image_type_to_extension($mark_info[2], false);
  116. $mark_fun = "imagecreatefrom{$mark_type}";
  117. $mark = $mark_fun($watermark);
  118. $posX = imagesx($image) - imagesx($mark);
  119. $posY = imagesy($image) - imagesy($mark);
  120. imagesavealpha($mark, true);
  121. imagecopymerge($image, $mark, $posX, $posY, 0, 0, $mark_info[0], $mark_info[1], $alpha);
  122. imagesavealpha($mark, true);
  123. if ($output) {
  124. $path = $this->saveImage($image);
  125. $this->path = $path;
  126. }
  127. $this->memoryImg = $image;
  128. return $this;
  129. }
  130. //用给定角度旋转图像,以jpeg图像格式为例
  131. /**
  132. * 水印图片旋转
  133. * @param $degrees 旋转角度
  134. * @param bool $output 是否保存图片
  135. * @return $this
  136. */
  137. function rotateImage($degrees, $output = false)
  138. {
  139. $info = getimagesize($this->imagePath);
  140. $type = image_type_to_extension($info[2], false);
  141. $fun = "imagecreatefrom{$type}";
  142. $image = $fun($this->imagePath);
  143. $block = imagecreatetruecolor(170,170);//建立一个画板
  144. $bg = imagecolorallocatealpha($block , 0 , 0 , 0 , 127);//拾取一个完全透明的颜色
  145. $image = imagerotate($image, $degrees, $bg ,0);
  146. imagesavealpha($image, true);
  147. header("Content-type: image/{$type}");
  148. //旋转后的图片保存
  149. if ($output) {
  150. $path = $this->saveImage($image);
  151. $this->path = $path;
  152. }
  153. $this->memoryImg = $image;
  154. return $this;
  155. }
  156. /**
  157. * 添加PNG透明图片
  158. * $bigImgPath 目标图片路径
  159. * $smallImgPath 水印图片路径
  160. * $width 相对于目标图的x轴放置位置 左上角为 0
  161. * $height 相对于目标图的y轴放置位置 左上角为0
  162. * $bigImgPaths 合成后的图片路径 若路径名与第一张或第二张路径相同 直接覆盖原图
  163. */
  164. public function mergerImg($bigImgPath, $smallImgPath, $width, $height, $bigImgPaths)
  165. {
  166. $image_kuang = imagecreatefromstring(file_get_contents($smallImgPath));
  167. $image_photo = imagecreatefromstring(file_get_contents($bigImgPath));
  168. //创建一个新的,和大图一样大的画布
  169. $image_3 = imageCreatetruecolor(imagesx($image_photo), imagesy($image_photo));
  170. //为真彩色画布创建白色背景,再设置为透明
  171. $color = imagecolorallocate($image_3, 255, 255, 255);
  172. imagefill($image_3, 0, 0, $color);
  173. imageColorTransparent($image_3, $color);
  174. /**
  175. * 先copy图片,再copy画框,实现png的透明效果,将图片嵌入到画框里
  176. * imagecopymerge与imagecopy的不同:
  177. * imagecopymerge 函数可以支持两个图像叠加时,设置叠加层的透明度。imagecopymerge比imagecopy多一个参数,来设置透明度
  178. * PHP内部源码里,imagecopymerge在透明度参数为100时,直接调用imagecopy函数。
  179. * imagecopy 函数则不支持叠加透明,但拷贝时可以保留png图像的原透明信息,而imagecopymerge却不支持图片的本身的透明拷贝
  180. * 即:使用imagecopymerge函数,可以实现打上透明度为30%的淡淡的水印图标,但图片本身的png就会变得像IE6不支持png透明那样,背景不透明了。
  181. * 如果使用imagecopy函数,可以保留图片本身的透明信息,但无法实现30%的淡淡水印叠加,
  182. */
  183. imagecopyresampled($image_3,$image_photo,0,0,0,0,imagesx($image_photo),imagesy($image_photo),imagesx($image_photo),imagesy($image_photo));
  184. imagecopy($image_3,$image_kuang, $width,$height,0,0,imagesx($image_kuang),imagesy($image_kuang));
  185. //存储图片路径
  186. imagejpeg($image_3, $bigImgPaths);
  187. return $bigImgPaths;
  188. }
  189. }

控制器调用方法

  1. public function test()
  2. {
  3. $bigImgPath = 'ren.jpg';//原图路径
  4. $waterImgPath = 'tae.png';//水印图路径
  5. $imageTool = new ImageTool($waterImgPath, 'tmp/');//图片路径、输出文件夹
  6. $smallImgPath = $imageTool->rotateImage(45, true)->path;//旋转
  7. $width = 0;//水印所在X坐标
  8. $height = 0;//水印所在Y坐标
  9. $bigImgPaths = 'new.png';//生成原图加水印新图路径
  10. $path = $this->mergerImg($bigImgPath, $smallImgPath, $width, $height, $bigImgPaths);
  11. return view('image', compact('path'));
  12. }