首页 > 开发 > PHP > 正文

摘自织梦CMS中的图片处理类

2024-05-04 23:38:39
字体:
来源:转载
供稿:网友

这篇文章主要介绍了摘自织梦CMS中的图片处理类,通过面向对象的方式较为详细的实现了php针对图片的缩略图生成及水印添加等操作技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了摘自织梦CMS中的图片处理类。分享给大家供大家参考。具体如下:

 

 
  1. <?php if(!defined('DEDEINC')) exit('dedecms'); 
  2. /** 
  3. * 图像处理类 
  4. * 
  5. * @version $Id: image.class.php 1 18:10 2010年7月5日Z tianya $ 
  6. * @package DedeCMS.Libraries 
  7. * @copyright Copyright (c) 2007 - 2010, DesDev, Inc. 
  8. * @license http://help.dedecms.com/usersguide/license.html 
  9. * @link http://www.dedecms.com 
  10. */ 
  11. class image 
  12. var $attachinfo
  13. var $targetfile//图片路径 
  14. var $imagecreatefromfunc
  15. var $imagefunc
  16. var $attach
  17. var $animatedgif
  18. var $watermarkquality
  19. var $watermarktext
  20. var $thumbstatus
  21. var $watermarkstatus
  22. // 析构函数,兼容PHP4 
  23. function image($targetfile$cfg_thumb$cfg_watermarktext$photo_waterpos$photo_diaphaneity$photo_wheight$photo_wwidth$cfg_watermarktype$photo_marktrans,$trueMarkimg$attach = array()) 
  24. $this->__construct($targetfile$cfg_thumb$cfg_watermarktext$photo_waterpos$photo_diaphaneity$photo_wheight$photo_wwidth$cfg_watermarktype$photo_marktrans,$trueMarkimg$attach); 
  25. // 析构函数 
  26. function __construct($targetfile$cfg_thumb$cfg_watermarktext$photo_waterpos$photo_diaphaneity$photo_wheight$photo_wwidth$cfg_watermarktype$photo_marktrans,$trueMarkimg$attach = array()) 
  27. $this->thumbstatus = $cfg_thumb
  28. $this->watermarktext = $cfg_watermarktext
  29. $this->watermarkstatus = $photo_waterpos
  30. $this->watermarkquality = $photo_marktrans
  31. $this->watermarkminwidth = $photo_wwidth
  32. $this->watermarkminheight = $photo_wheight
  33. $this->watermarktype = $cfg_watermarktype
  34. $this->watermarktrans = $photo_diaphaneity
  35. $this->animatedgif = 0; 
  36. $this->targetfile = $targetfile
  37. $this->attachinfo = @getimagesize($targetfile); 
  38. $this->attach = $attach
  39. switch($this->attachinfo['mime']) 
  40. case 'image/jpeg'
  41. $this->imagecreatefromfunc = function_exists('imagecreatefromjpeg') ? 'imagecreatefromjpeg' : ''
  42. $this->imagefunc = function_exists('imagejpeg') ? 'imagejpeg' : ''
  43. break
  44. case 'image/gif'
  45. $this->imagecreatefromfunc = function_exists('imagecreatefromgif') ? 'imagecreatefromgif' : ''
  46. $this->imagefunc = function_exists('imagegif') ? 'imagegif' : ''
  47. break
  48. case 'image/png'
  49. $this->imagecreatefromfunc = function_exists('imagecreatefrompng') ? 'imagecreatefrompng' : ''
  50. $this->imagefunc = function_exists('imagepng') ? 'imagepng' : ''
  51. break
  52. }//为空则匹配类型的函数不存在 
  53. $this->attach['size'] = emptyempty($this->attach['size']) ? @filesize($targetfile) : $this->attach['size']; 
  54. if($this->attachinfo['mime'] == 'image/gif'
  55. $fp = fopen($targetfile'rb'); 
  56. $targetfilecontent = fread($fp$this->attach['size']); 
  57. fclose($fp); 
  58. $this->animatedgif = strpos($targetfilecontent'NETSCAPE2.0') === false ? 0 : 1; 
  59. /** 
  60. * 生成缩略图 
  61. * 
  62. * @access public 
  63. * @param int $thumbwidth 图片宽度 
  64. * @param int $thumbheight 图片高度 
  65. * @param int $preview 是否预览 
  66. * @return void 
  67. */ 
  68. function thumb($thumbwidth$thumbheight$preview = 0) 
  69. $this->thumb_gd($thumbwidth$thumbheight$preview); 
  70.  
  71. if($this->thumbstatus == 2 && $this->watermarkstatus) 
  72. $this->image($this->targetfile, $this->attach); 
  73. $this->attach['size'] = filesize($this->targetfile); 
  74. /** 
  75. * 图片水印 
  76. * 
  77. * @access public 
  78. * @param int $preview 是否预览 
  79. * @return void 
  80. */ 
  81. function watermark($preview = 0) 
  82. if($this->watermarkminwidth && $this->attachinfo[0] <= $this->watermarkminwidth && $this->watermarkminheight && $this->attachinfo[1] <= $this->watermarkminheight) 
  83. return ; 
  84. $this->watermark_gd($preview); 
  85. /** 
  86. * 使用gd生成缩略图 
  87. * 
  88. * @access public 
  89. * @param int $thumbwidth 图片宽度 
  90. * @param int $thumbheight 图片高度 
  91. * @param int $preview 是否预览 
  92. * @return void 
  93. */ 
  94. function thumb_gd($thumbwidth$thumbheight$preview = 0) 
  95. if($this->thumbstatus && function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled') && function_exists('imagejpeg')) 
  96. $imagecreatefromfunc = $this->imagecreatefromfunc; 
  97. $imagefunc = $this->thumbstatus == 1 ? 'imagejpeg' : $this->imagefunc; 
  98. list($imagewidth$imageheight) = $this->attachinfo; 
  99. if(!$this->animatedgif && ($imagewidth >= $thumbwidth || $imageheight >= $thumbheight)) 
  100. $attach_photo = $imagecreatefromfunc($this->targetfile); 
  101. $x_ratio = $thumbwidth / $imagewidth
  102. $y_ratio = $thumbheight / $imageheight
  103. if(($x_ratio * $imageheight) < $thumbheight
  104. $thumb['height'] = ceil($x_ratio * $imageheight); 
  105. $thumb['width'] = $thumbwidth
  106. else 
  107. $thumb['width'] = ceil($y_ratio * $imagewidth); 
  108. $thumb['height'] = $thumbheight
  109. $targetfile = !$preview ? ($this->thumbstatus == 1 ? $this->targetfile.'.thumb.jpg' : $this->targetfile) : './watermark_tmp.jpg'
  110. $thumb_photo = imagecreatetruecolor($thumb['width'], $thumb['height']); 
  111. imagecopyresampled($thumb_photo$attach_photo, 0, 0, 0, 0, $thumb['width'], $thumb['height'], $imagewidth$imageheight); 
  112. if($this->attachinfo['mime'] == 'image/jpeg'
  113. $imagefunc($thumb_photo$targetfile, 100); 
  114. else 
  115. $imagefunc($thumb_photo$targetfile); 
  116. $this->attach['thumb'] = $this->thumbstatus == 1 ? 1 : 0; 
  117. /** 
  118. * 使用gd进行水印 
  119. * 
  120. * @access public 
  121. * @param int $preview 是否预览 
  122. * @return void 
  123. */ 
  124. function watermark_gd($preview = 0) 
  125. if($this->watermarkstatus && function_exists('imagecopy') && function_exists('imagealphablending') && function_exists('imagecopymerge')) 
  126. $imagecreatefunc = $this->imagecreatefromfunc; 
  127. $imagefunc = $this->imagefunc; 
  128. list($imagewidth$imageheight) = $this->attachinfo; 
  129. if($this->watermarktype < 2) 
  130. $watermark_file = $this->watermarktype == 1 ? DEDEDATA.'/mark/mark.png' : DEDEDATA.'/mark/mark.gif'
  131. $watermarkinfo = @getimagesize($watermark_file); 
  132. $watermark_logo = $this->watermarktype == 1 ? @imagecreatefrompng($watermark_file) : @imagecreatefromgif($watermark_file); 
  133. if(!$watermark_logo
  134. return ; 
  135. list($logowidth$logoheight) = $watermarkinfo
  136. else 
  137. $box = @imagettfbbox($this->watermarktext['size'], $this->watermarktext['angle'], $this->watermarktext['fontpath'],$this->watermarktext['text']); 
  138. $logowidth = max($box[2], $box[4]) - min($box[0], $box[6]); 
  139. $logoheight = max($box[1], $box[3]) - min($box[5], $box[7]); 
  140. $ax = min($box[0], $box[6]) * -1; 
  141. $ay = min($box[5], $box[7]) * -1; 
  142. $wmwidth = $imagewidth - $logowidth
  143. $wmheight = $imageheight - $logoheight
  144. if(($this->watermarktype < 2 && is_readable($watermark_file) || $this->watermarktype == 2) && $wmwidth > 10 && $wmheight > 10 && !$this->animatedgif) 
  145. switch($this->watermarkstatus) 
  146. case 1: 
  147.  
  148. $x = +5; 
  149. $y = +5; 
  150. break
  151. case 2: 
  152. $x = ($imagewidth - $logowidth) / 2; 
  153. $y = +5; 
  154. break
  155. case 3: 
  156. $x = $imagewidth - $logowidth - 5; 
  157. $y = +5; 
  158. break
  159. case 4: 
  160. $x = +5; 
  161. $y = ($imageheight - $logoheight) / 2; 
  162. break
  163. case 5: 
  164. $x = ($imagewidth - $logowidth) / 2; 
  165. $y = ($imageheight - $logoheight) / 2; 
  166. break
  167. case 6: 
  168. $x = $imagewidth - $logowidth - 5; 
  169. $y = ($imageheight - $logoheight) / 2; 
  170. break
  171. case 7: 
  172. $x = +5; 
  173. $y = $imageheight - $logoheight - 5; 
  174. break
  175. case 8: 
  176. $x = ($imagewidth - $logowidth) / 2; 
  177. $y = $imageheight - $logoheight - 5; 
  178. break
  179. case 9: 
  180. $x = $imagewidth - $logowidth - 5; 
  181. $y = $imageheight - $logoheight -5; 
  182. break
  183. $dst_photo = @imagecreatetruecolor($imagewidth$imageheight); 
  184. $target_photo = $imagecreatefunc($this->targetfile); 
  185. imagecopy($dst_photo$target_photo, 0, 0, 0, 0, $imagewidth$imageheight); 
  186. if($this->watermarktype == 1) 
  187. imagecopy($dst_photo$watermark_logo$x$y, 0, 0, $logowidth$logoheight); 
  188. elseif($this->watermarktype == 2) 
  189. if(($this->watermarktext['shadowx'] || $this->watermarktext['shadowy']) && $this->watermarktext['shadowcolor']) 
  190. $shadowcolorrgb = explode(','$this->watermarktext['shadowcolor']); 
  191. $shadowcolor = imagecolorallocate($dst_photo$shadowcolorrgb[0], $shadowcolorrgb[1], $shadowcolorrgb[2]); 
  192. imagettftext($dst_photo$this->watermarktext['size'], $this->watermarktext['angle'], 
  193. $x + $ax + $this->watermarktext['shadowx'], $y + $ay + $this->watermarktext['shadowy'], $shadowcolor
  194. $this->watermarktext['fontpath'], $this->watermarktext['text']); 
  195. $colorrgb = explode(','$this->watermarktext['color']); 
  196. $color = imagecolorallocate($dst_photo$colorrgb[0], $colorrgb[1], $colorrgb[2]); 
  197. imagettftext($dst_photo$this->watermarktext['size'], $this->watermarktext['angle'], 
  198. $x + $ax$y + $ay$color$this->watermarktext['fontpath'], $this->watermarktext['text']); 
  199. else 
  200. imagealphablending($watermark_logo, true); 
  201. imagecopymerge($dst_photo$watermark_logo$x$y, 0, 0, $logowidth$logoheight$this->watermarktrans); 
  202. $targetfile = !$preview ? $this->targetfile : './watermark_tmp.jpg'
  203. if($this->attachinfo['mime'] == 'image/jpeg'
  204. $imagefunc($dst_photo$targetfile$this->watermarkquality); 
  205. else 
  206. $imagefunc($dst_photo$targetfile); 
  207. $this->attach['size'] = filesize($this->targetfile); 
  208. }//End Class 

希望本文所述对大家的php程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表