当前位置:首页 > CMS教程 > DeDecms > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2021-06-16 10:01:29 浏览: 评论:0 

这篇文章主要介绍了摘自织梦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. { 
  13.  var $attachinfo; 
  14.  var $targetfile; //图片路径 
  15.  var $imagecreatefromfunc; 
  16.  var $imagefunc; 
  17.  var $attach; 
  18.  var $animatedgif; 
  19.  var $watermarkquality; 
  20.  var $watermarktext; 
  21.  var $thumbstatus; 
  22.  var $watermarkstatus; 
  23.  // 析构函数,兼容PHP4 
  24.  function image($targetfile, $cfg_thumb, $cfg_watermarktext, $photo_waterpos, $photo_diaphaneity, $photo_wheight, $photo_wwidth, $cfg_watermarktype, $photo_marktrans,$trueMarkimg, $attach = array()) 
  25.  { 
  26.   $this->__construct($targetfile, $cfg_thumb, $cfg_watermarktext, $photo_waterpos, $photo_diaphaneity, $photo_wheight, $photo_wwidth, $cfg_watermarktype, $photo_marktrans,$trueMarkimg, $attach); 
  27.  } 
  28.  // 析构函数 
  29.  function __construct($targetfile, $cfg_thumb, $cfg_watermarktext, $photo_waterpos, $photo_diaphaneity, $photo_wheight, $photo_wwidth, $cfg_watermarktype, $photo_marktrans,$trueMarkimg, $attach = array()) 
  30.  { 
  31.   $this->thumbstatus = $cfg_thumb; 
  32.   $this->watermarktext = $cfg_watermarktext; 
  33.   $this->watermarkstatus = $photo_waterpos; 
  34.   $this->watermarkquality = $photo_marktrans; 
  35.   $this->watermarkminwidth = $photo_wwidth; 
  36.   $this->watermarkminheight = $photo_wheight; 
  37.   $this->watermarktype = $cfg_watermarktype; 
  38.   $this->watermarktrans = $photo_diaphaneity; 
  39.   $this->animatedgif = 0; 
  40.   $this->targetfile = $targetfile; 
  41.   $this->attachinfo = @getimagesize($targetfile); 
  42.   $this->attach = $attach; 
  43.   switch($this->attachinfo['mime']) 
  44.   { 
  45.    case 'image/jpeg': 
  46.     $this->imagecreatefromfunc = function_exists('imagecreatefromjpeg') ? 'imagecreatefromjpeg' : ''; 
  47.     $this->imagefunc = function_exists('imagejpeg') ? 'imagejpeg' : ''; 
  48.     break; 
  49.    case 'image/gif': 
  50.     $this->imagecreatefromfunc = function_exists('imagecreatefromgif') ? 'imagecreatefromgif' : ''; 
  51.     $this->imagefunc = function_exists('imagegif') ? 'imagegif' : ''; 
  52.     break; 
  53.    case 'image/png': 
  54.     $this->imagecreatefromfunc = function_exists('imagecreatefrompng') ? 'imagecreatefrompng' : ''; 
  55.     $this->imagefunc = function_exists('imagepng') ? 'imagepng' : ''; 
  56.     break; 
  57.   }//为空则匹配类型的函数不存在 
  58.   $this->attach['size'] = emptyempty($this->attach['size']) ? @filesize($targetfile) : $this->attach['size']; 
  59.   if($this->attachinfo['mime'] == 'image/gif') 
  60.   { 
  61.    $fp = fopen($targetfile, 'rb'); 
  62.    $targetfilecontent = fread($fp, $this->attach['size']); 
  63.    fclose($fp); 
  64.    $this->animatedgif = strpos($targetfilecontent, 'NETSCAPE2.0') === false ? 0 : 1; 
  65.   } 
  66.  } 
  67.  /** 
  68.   * 生成缩略图 
  69.   * 
  70.   * @access public 
  71.   * @param  int $thumbwidth 图片宽度 
  72.   * @param  int $thumbheight 图片高度 
  73.   * @param  int $preview 是否预览 
  74.   * @return void 
  75.   */ 
  76.  function thumb($thumbwidth, $thumbheight, $preview = 0) 
  77.  { 
  78.   $this->thumb_gd($thumbwidth, $thumbheight, $preview); 
  79.    
  80.   if($this->thumbstatus == 2 && $this->watermarkstatus) 
  81.   { 
  82.    $this->image($this->targetfile, $this->attach); 
  83.    $this->attach['size'] = filesize($this->targetfile); 
  84.   } 
  85.  } 
  86.  /** 
  87.   * 图片水印 
  88.   * 
  89.   * @access public 
  90.   * @param  int $preview 是否预览 
  91.   * @return void 
  92.   */ 
  93.  function watermark($preview = 0) 
  94.  { 
  95.   if($this->watermarkminwidth && $this->attachinfo[0] <= $this->watermarkminwidth && $this->watermarkminheight && $this->attachinfo[1] <= $this->watermarkminheight) 
  96.   { 
  97.    return ; 
  98.   } 
  99.   $this->watermark_gd($preview); 
  100.  } 
  101.  /** 
  102.   * 使用gd生成缩略图 
  103.   * 
  104.   * @access public 
  105.   * @param  int $thumbwidth 图片宽度 
  106.   * @param  int $thumbheight 图片高度 
  107.   * @param  int $preview 是否预览 
  108.   * @return void 
  109.   */ 
  110.  function thumb_gd($thumbwidth, $thumbheight, $preview = 0) 
  111.  { 
  112.   if($this->thumbstatus && function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled') && function_exists('imagejpeg')) 
  113.   { 
  114.    $imagecreatefromfunc = $this->imagecreatefromfunc; 
  115.    $imagefunc = $this->thumbstatus == 1 ? 'imagejpeg' : $this->imagefunc; 
  116.    list($imagewidth, $imageheight) = $this->attachinfo; 
  117.    if(!$this->animatedgif && ($imagewidth >= $thumbwidth || $imageheight >= $thumbheight)) 
  118.    { 
  119.     $attach_photo = $imagecreatefromfunc($this->targetfile); 
  120.     $x_ratio = $thumbwidth / $imagewidth; 
  121.     $y_ratio = $thumbheight / $imageheight; 
  122.     if(($x_ratio * $imageheight) < $thumbheight) 
  123.     { 
  124.      $thumb['height'] = ceil($x_ratio * $imageheight); 
  125.      $thumb['width'] = $thumbwidth; 
  126.     } 
  127.     else 
  128.     { 
  129.      $thumb['width'] = ceil($y_ratio * $imagewidth); 
  130.      $thumb['height'] = $thumbheight; 
  131.     } 
  132.     $targetfile = !$preview ? ($this->thumbstatus == 1 ? $this->targetfile.'.thumb.jpg' : $this->targetfile) : './watermark_tmp.jpg'; 
  133.     $thumb_photo = imagecreatetruecolor($thumb['width'], $thumb['height']); 
  134.     imagecopyresampled($thumb_photo, $attach_photo, 0, 0, 0, 0, $thumb['width'], $thumb['height'], $imagewidth, $imageheight); 
  135.     if($this->attachinfo['mime'] == 'image/jpeg') 
  136.     { 
  137.      $imagefunc($thumb_photo, $targetfile, 100); 
  138.     } 
  139.     else 
  140.     { 
  141.      $imagefunc($thumb_photo, $targetfile); 
  142.     } 
  143.     $this->attach['thumb'] = $this->thumbstatus == 1 ? 1 : 0; 
  144.    } 
  145.   } 
  146.  } 
  147.  /** 
  148.   * 使用gd进行水印 
  149.   * 
  150.   * @access public 
  151.   * @param  int $preview 是否预览 
  152.   * @return void 
  153.   */ 
  154.  function watermark_gd($preview = 0) 
  155.  { 
  156.   if($this->watermarkstatus && function_exists('imagecopy') && function_exists('imagealphablending') && function_exists('imagecopymerge')) 
  157.   { 
  158.    $imagecreatefunc = $this->imagecreatefromfunc; 
  159.    $imagefunc = $this->imagefunc; 
  160.    list($imagewidth, $imageheight) = $this->attachinfo; 
  161.    if($this->watermarktype < 2) 
  162.    { 
  163.     $watermark_file = $this->watermarktype == 1 ? DEDEDATA.'/mark/mark.png' : DEDEDATA.'/mark/mark.gif'; 
  164.     $watermarkinfo = @getimagesize($watermark_file); 
  165.     $watermark_logo = $this->watermarktype == 1 ? @imagecreatefrompng($watermark_file) : @imagecreatefromgif($watermark_file); 
  166.     if(!$watermark_logo) 
  167.     { 
  168.      return ; 
  169.     } 
  170.     list($logowidth, $logoheight) = $watermarkinfo; 
  171.    } 
  172.    else 
  173.    { 
  174.     $box = @imagettfbbox($this->watermarktext['size'], $this->watermarktext['angle'], $this->watermarktext['fontpath'],$this->watermarktext['text']); 
  175.     $logowidth = max($box[2], $box[4]) - min($box[0], $box[6]); 
  176.     $logoheight = max($box[1], $box[3]) - min($box[5], $box[7]); 
  177.     $ax = min($box[0], $box[6]) * -1; 
  178.     $ay = min($box[5], $box[7]) * -1; 
  179.    } 
  180.    $wmwidth = $imagewidth - $logowidth; 
  181.    $wmheight = $imageheight - $logoheight; 
  182.    if(($this->watermarktype < 2 && is_readable($watermark_file) || $this->watermarktype == 2) && $wmwidth > 10 && $wmheight > 10 && !$this->animatedgif) 
  183.    { 
  184.     switch($this->watermarkstatus) 
  185.     { 
  186.      case 1: 
  187.    
  188.       $x = +5; 
  189.       $y = +5; 
  190.       break; 
  191.      case 2: 
  192.       $x = ($imagewidth - $logowidth) / 2; 
  193.       $y = +5; 
  194.       break; 
  195.      case 3: 
  196.       $x = $imagewidth - $logowidth - 5; 
  197.       $y = +5; 
  198.       break; 
  199.      case 4: 
  200.       $x = +5; 
  201.       $y = ($imageheight - $logoheight) / 2; 
  202.       break; 
  203.      case 5: 
  204.       $x = ($imagewidth - $logowidth) / 2; 
  205.       $y = ($imageheight - $logoheight) / 2; 
  206.       break; 
  207.      case 6: 
  208.       $x = $imagewidth - $logowidth - 5; 
  209.       $y = ($imageheight - $logoheight) / 2; 
  210.       break; 
  211.      case 7: 
  212.       $x = +5; 
  213.       $y = $imageheight - $logoheight - 5; 
  214.       break; 
  215.      case 8: 
  216.       $x = ($imagewidth - $logowidth) / 2; 
  217.       $y = $imageheight - $logoheight - 5; 
  218.       break; 
  219.      case 9: 
  220.       $x = $imagewidth - $logowidth - 5; 
  221.       $y = $imageheight - $logoheight -5; 
  222.       break; 
  223.     } 
  224.     $dst_photo = @imagecreatetruecolor($imagewidth, $imageheight); 
  225.     $target_photo = $imagecreatefunc($this->targetfile); 
  226.     imagecopy($dst_photo, $target_photo, 0, 0, 0, 0, $imagewidth, $imageheight); 
  227.     if($this->watermarktype == 1) 
  228.     { 
  229.      imagecopy($dst_photo, $watermark_logo, $x, $y, 0, 0, $logowidth, $logoheight); 
  230.     } 
  231.     elseif($this->watermarktype == 2) 
  232.     { 
  233.      if(($this->watermarktext['shadowx'] || $this->watermarktext['shadowy']) && $this->watermarktext['shadowcolor']) 
  234.      { 
  235.       $shadowcolorrgb = explode(',', $this->watermarktext['shadowcolor']); 
  236.       $shadowcolor = imagecolorallocate($dst_photo, $shadowcolorrgb[0], $shadowcolorrgb[1], $shadowcolorrgb[2]); 
  237.       imagettftext($dst_photo, $this->watermarktext['size'], $this->watermarktext['angle'], 
  238.       $x + $ax + $this->watermarktext['shadowx'], $y + $ay + $this->watermarktext['shadowy'], $shadowcolor, 
  239.       $this->watermarktext['fontpath'], $this->watermarktext['text']); 
  240.      } 
  241.      $colorrgb = explode(',', $this->watermarktext['color']); 
  242.      $color = imagecolorallocate($dst_photo, $colorrgb[0], $colorrgb[1], $colorrgb[2]); 
  243.      imagettftext($dst_photo, $this->watermarktext['size'], $this->watermarktext['angle'], 
  244.      $x + $ax, $y + $ay, $color, $this->watermarktext['fontpath'], $this->watermarktext['text']); 
  245.     } 
  246.     else 
  247.     { 
  248.      imagealphablending($watermark_logo, true); 
  249.      imagecopymerge($dst_photo, $watermark_logo, $x, $y, 0, 0, $logowidth, $logoheight, $this->watermarktrans); 
  250.     } 
  251.     $targetfile = !$preview ? $this->targetfile : './watermark_tmp.jpg'; 
  252.     if($this->attachinfo['mime'] == 'image/jpeg') 
  253.     { 
  254.      $imagefunc($dst_photo, $targetfile, $this->watermarkquality); 
  255.     } 
  256.     else 
  257.     { 
  258.      $imagefunc($dst_photo, $targetfile); 
  259.     } 
  260.     $this->attach['size'] = filesize($this->targetfile); 
  261.    } 
  262.   } 
  263.  } 
  264. }//End Class 

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

Tags: 织梦CMS图片处理类

分享到: