当前位置:Gxlcms > PHP教程 > php图片处理类(附实例)

php图片处理类(附实例)

时间:2021-07-01 10:21:17 帮助过:22人阅读

分享一个php实现的图片处理类,可以设置文字水印与图片水印等,有需要的朋友参考下。

本节分享一个图片处理类,简单实现了文字水印与图片水印,是学习php图片操作的小例子。

代码:

  1. <!--?php
  2. class ImageModifier {
  3. /**
  4. * 实现在图片上保存文本信息
  5. *
  6. * default is array()
  7. *
  8. * @access private
  9. */
  10. var $aTextData = array();
  11. /**
  12. * 保存文本信息到图片上
  13. *
  14. * default is array()
  15. *
  16. * @access private
  17. */
  18. var $aImageData = array();
  19. /**
  20. * imagick的资源标识符
  21. *
  22. * default is FALSE
  23. *
  24. * @access private
  25. */
  26. var $image = "";
  27. /**
  28. * 错误消息级别
  29. *
  30. * default is 0
  31. *
  32. * @varinteger
  33. */
  34. var $sError = 0;
  35. /**
  36. * 构造函数
  37. *
  38. * @param string $tplImage template image
  39. * @access private
  40. */
  41. public function __construct($tplImage) {
  42. // Check Imagick class exist or not if not show error.
  43. if (!class_exists("Imagick", false)) {
  44. exit("Unable to load class: Imagick\n. Imagick Image Library Missing.");
  45. }
  46. // create a object of Imagick template image
  47. $this--->image = new Imagick($tplImage);
  48. }
  49. /**
  50. * 设置文本属性
  51. *
  52. * @param string $sText text to print on the image (i.e. Buy 1 Get 1 Free )
  53. * @param integer $x text to print from x codinates
  54. * @param integer $y text to print from y codinates
  55. * @param integer $font text size for printing
  56. * @param string $color text color for print
  57. * @param integer $text_anglerotate text from 0-360
  58. * @param string $font_style installed font name and path (i.e /usr/share/fonts/liberation/LiberationSans-Italic.ttf)
  59. * @Creating an array of text properties
  60. */
  61. public function setText($sText, $x = 0, $y = 0, $font = 12, $color = 'black', $text_angle = 0, $font_style = './LiberationSans-Italic.ttf') {
  62. $this->aTextData[] = array("text"=>$sText, "font_color"=>$color, "font_size"=>$font,"x"=>$x,"y"=>$y, "font_style"=>$font_style,
  63. "text_angle"=>$text_angle);
  64. }
  65. /**
  66. * 设置图片属性
  67. *
  68. * @param string $sImage text to print on the image (i.e. /home/httpd/images/brand.jpg )
  69. * @param integer $x text to print from x codinates
  70. * @param integer $y text to print from y codinates
  71. * @param integer $text_anglerotate text from 0-180
  72. * @Creating an array of image properties
  73. */
  74. public function setImage($sImage, $x = 0, $y = 0, $angle=0) {
  75. $this->aImageData[] = array("image"=>$sImage, "x"=>$x, "y"=>$y, "angle"=>$angle);
  76. }
  77. /**
  78. * 从文字和图片属性生成最终图像
  79. *
  80. * @param string $sImage Output image Name
  81. * @return boolean returns TRUE on success and FALSE upon failure
  82. */
  83. public function generateImage($sImage) {
  84. foreach ($this->aImageData as $aImageValue) {
  85. if (!trim($aImageValue["image"])) {
  86. $sError = 1;
  87. break;
  88. }
  89. $oImg = new Imagick($aImageValue["image"]);
  90. $oImg->rotateImage("transparent", $aImageValue["angle"]);
  91. $this->image->compositeImage($oImg, $oImg->getImageCompose(), $aImageValue["x"], $aImageValue["y"]);
  92. unset($oImg);
  93. }
  94. foreach ($this->aTextData as $aTextValue){
  95. if (!trim($aTextValue['text'])) {
  96. $sError = 2;
  97. break;
  98. }
  99. $oDraw = new ImagickDraw();
  100. $oDraw->setFont($aTextValue['font_style']);
  101. $oDraw->setFontSize($aTextValue['font_size']);
  102. $oDraw->setFillColor($aTextValue['font_color']);
  103. $this->image->annotateImage($oDraw, $aTextValue['x'], $aTextValue['y'], $aTextValue['text_angle'], $aTextValue['text']);
  104. unset($oDraw);
  105. }
  106. if ($sError == 1) {
  107. exit("Unable to generate Image. Check \"setImage\" Properties");
  108. }elseif ($sError == 2) {
  109. exit("Unable to generate Image. Check \"setText\" Properties");
  110. }
  111. $this->image->setImageFormat("jpg");
  112. return $this->image->writeImage($sImage);
  113. }
  114. }
  115. ?>

2,调用示例:

  1. <!--?php
  2. //调用类文件
  3. require_once "ImageModifier.class.php";
  4. //示例
  5. $oImageMagick = new ImageModifier('template.jpg'); // Image Template on which you have to manupulate
  6. $oImageMagick--->setText("This is one", 350, 20, 22, "red");
  7. $oImageMagick->setText("This is Two", 50, 50, 25, "blue","50");
  8. $oImageMagick->setImage("brand.jpg", 160, 90, 0);
  9. $oImageMagick->setImage("tata.jpg", 160, 20);
  10. $newImagename = "mynewImage.jpg";
  11. $oImageMagick->generateImage($newImagename);
  12. ?>

人气教程排行