dedecms图片类

时间:2010-11-09 00:28 作者:php 点击:
dedecms图片类 ?php class image { var $attachinfo ; var $targetfile ; //图片路径 var $imagecreatefromfunc ; var $imagefunc ; var $attach ; var $animatedgif ; var $watermarkquality ; var $watermarktext ; var $thumbstatus ; var $waterma

  

dedecms图片类

 

  1. <?php  
  2. class image  
  3. {  
  4.     var $attachinfo;  
  5.     var $targetfile;    //图片路径  
  6.     var $imagecreatefromfunc;  
  7.     var $imagefunc;  
  8.     var $attach;  
  9.     var $animatedgif;  
  10.     var $watermarkquality;  
  11.     var $watermarktext;  
  12.     var $thumbstatus;  
  13.     var $watermarkstatus;  
  14.  
  15.     function image($targetfile$cfg_thumb$cfg_watermarktext$photo_waterpos$photo_diaphaneity$photo_wheight$photo_wwidth$cfg_watermarktype$photo_marktrans,$trueMarkimg$attach = array())  
  16.     {  
  17.         $this->__construct($targetfile$cfg_thumb$cfg_watermarktext$photo_waterpos$photo_diaphaneity$photo_wheight$photo_wwidth$cfg_watermarktype$photo_marktrans,$trueMarkimg$attach);  
  18.     }  
  19.  
  20.     function __construct($targetfile$cfg_thumb$cfg_watermarktext$photo_waterpos$photo_diaphaneity$photo_wheight$photo_wwidth$cfg_watermarktype$photo_marktrans,$trueMarkimg$attach = array())  
  21.     {  
  22.         $this->thumbstatus = $cfg_thumb;  
  23.         $this->watermarktext = $cfg_watermarktext;  
  24.         $this->watermarkstatus = $photo_waterpos;  
  25.         $this->watermarkquality = $photo_marktrans;  
  26.         $this->watermarkminwidth = $photo_wwidth;  
  27.         $this->watermarkminheight = $photo_wheight;  
  28.         $this->watermarktype = $cfg_watermarktype;  
  29.         $this->watermarktrans = $photo_diaphaneity;  
  30.         $this->animatedgif = 0;  
  31.         $this->targetfile = $targetfile;  
  32.         $this->attachinfo = @getimagesize($targetfile);  
  33.         $this->attach = $attach;  
  34.  
  35.  
  36.         switch($this->attachinfo['mime'])  
  37.         {  
  38.             case 'image/jpeg':  
  39.                 $this->imagecreatefromfunc = function_exists('imagecreatefromjpeg') ? 'imagecreatefromjpeg' : '';  
  40.                 $this->imagefunc = function_exists('imagejpeg') ? 'imagejpeg' : '';  
  41.                 break;  
  42.             case 'image/gif':  
  43.                 $this->imagecreatefromfunc = function_exists('imagecreatefromgif') ? 'imagecreatefromgif' : '';  
  44.                 $this->imagefunc = function_exists('imagegif') ? 'imagegif' : '';  
  45.                 break;  
  46.             case 'image/png':  
  47.                 $this->imagecreatefromfunc = function_exists('imagecreatefrompng') ? 'imagecreatefrompng' : '';  
  48.                 $this->imagefunc = function_exists('imagepng') ? 'imagepng' : '';  
  49.                 break;  
  50.         }//为空则匹配类型的函数不存在  
  51.  
  52.         $this->attach['size'] = emptyempty($this->attach['size']) ? @filesize($targetfile) : $this->attach['size'];  
  53.         if($this->attachinfo['mime'] == 'image/gif')  
  54.         {  
  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.     function thumb($thumbwidth$thumbheight$preview = 0)  
  63.     {  
  64.         $this->thumb_gd($thumbwidth$thumbheight$preview);  
  65.  
  66.         if($this->thumbstatus == 2 && $this->watermarkstatus)  
  67.         {  
  68.             $this->image($this->targetfile, $this->attach);  
  69.             $this->attach['size'] = filesize($this->targetfile);  
  70.         }  
  71.     }  
  72.  
  73.     function watermark($preview = 0)  
  74.     {  
  75.         if($this->watermarkminwidth && $this->attachinfo[0] <= $this->watermarkminwidth && $this->watermarkminheight && $this->attachinfo[1] <= $this->watermarkminheight)  
  76.         {  
  77.             return ;  
  78.         }  
  79.         $this->watermark_gd($preview);  
  80.     }  
  81.  
  82.     function thumb_gd($thumbwidth$thumbheight$preview = 0)  
  83.     {  
  84.  
  85.         if($this->thumbstatus && function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled') && function_exists('imagejpeg'))  
  86.         {  
  87.             $imagecreatefromfunc = $this->imagecreatefromfunc;  
  88.             $imagefunc = $this->thumbstatus == 1 ? 'imagejpeg' : $this->imagefunc;  
  89.             list($imagewidth$imageheight) = $this->attachinfo;  
  90.             if(!$this->animatedgif && ($imagewidth >= $thumbwidth || $imageheight >= $thumbheight))  
  91.             {  
  92.                 $attach_photo = $imagecreatefromfunc($this->targetfile);  
  93.                 $x_ratio = $thumbwidth / $imagewidth;  
  94.                 $y_ratio = $thumbheight / $imageheight;  
  95.                 if(($x_ratio * $imageheight) < $thumbheight)  
  96.                 {  
  97.                     $thumb['height'] = ceil($x_ratio * $imageheight);  
  98.                     $thumb['width'] = $thumbwidth;  
  99.                 }  
  100.                 else 
  101.                 {  
  102.                     $thumb['width'] = ceil($y_ratio * $imagewidth);  
  103.                     $thumb['height'] = $thumbheight;  
  104.                 }  
  105.                 $targetfile = !$preview ? ($this->thumbstatus == 1 ? $this->targetfile.'.thumb.jpg' : $this->targetfile) : './watermark_tmp.jpg';  
  106.                 $thumb_photo = imagecreatetruecolor($thumb['width'], $thumb['height']);  
  107.                 imagecopyresampled($thumb_photo$attach_photo, 0, 0, 0, 0, $thumb['width'], $thumb['height'], $imagewidth$imageheight);  
  108.                 if($this->attachinfo['mime'] == 'image/jpeg')  
  109.                 {  
  110.                     $imagefunc($thumb_photo$targetfile, 100);  
  111.                 }  
  112.                 else 
  113.                 {  
  114.                     $imagefunc($thumb_photo$targetfile);  
  115.                 }  
  116.                 $this->attach['thumb'] = $this->thumbstatus == 1 ? 1 : 0;  
  117.             }  
  118.         }  
  119.     }  
  120.  
  121.     function watermark_gd($preview = 0)  
  122.     {  
  123.         if($this->watermarkstatus && function_exists('imagecopy') && function_exists('imagealphablending') && function_exists('imagecopymerge'))  
  124.         {  
  125.             $imagecreatefunc = $this->imagecreatefromfunc;  
  126.             $imagefunc = $this->imagefunc;  
  127.             list($imagewidth$imageheight) = $this->attachinfo;  
  128.             if($this->watermarktype < 2)  
  129.             {  
  130.                 $watermark_file = $this->watermarktype == 1 ? DEDEROOT.'/data/mark/mark.png' : DEDEROOT.'/data/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.                 {  
  135.                     return ;  
  136.                 }  
  137.                 list($logowidth$logoheight) = $watermarkinfo;  
  138.             }  
  139.             else 
  140.             {  
  141.                 $box = @imagettfbbox($this->watermarktext['size'], $this->watermarktext['angle'], $this->watermarktext['fontpath'],$this->watermarktext['text']);  
  142.                 $logowidth = max($box[2], $box[4]) - min($box[0], $box[6]);  
  143.                 $logoheight = max($box[1], $box[3]) - min($box[5], $box[7]);  
  144.                 $ax = min($box[0], $box[6]) * -1;  
  145.                 $ay = min($box[5], $box[7]) * -1;  
  146.             }  
  147.             $wmwidth = $imagewidth - $logowidth;  
  148.             $wmheight = $imageheight - $logoheight;  
  149.             if(($this->watermarktype < 2 && is_readable($watermark_file) || $this->watermarktype == 2) && $wmwidth > 10 && $wmheight > 10 && !$this->animatedgif)  
  150.             {  
  151.                 switch($this->watermarkstatus)  
  152.                 {  
  153.                     case 1:  
  154.  
  155.                         $x = +5;  
  156.                         $y = +5;  
  157.                         break;  
  158.                     case 2:  
  159.                         $x = ($imagewidth - $logowidth) / 2;  
  160.                         $y = +5;  
  161.                         break;  
  162.                     case 3:  
  163.                         $x = $imagewidth - $logowidth - 5;  
  164.                         $y = +5;  
  165.                         break;  
  166.                     case 4:  
  167.                         $x = +5;  
  168.                         $y = ($imageheight - $logoheight) / 2;  
  169.                         break;  
  170.                     case 5:  
  171.                         $x = ($imagewidth - $logowidth) / 2;  
  172.                         $y = ($imageheight - $logoheight) / 2;  
  173.                         break;  
  174.                     case 6:  
  175.                         $x = $imagewidth - $logowidth - 5;  
  176.                         $y = ($imageheight - $logoheight) / 2;  
  177.                         break;  
  178.                     case 7:  
  179.                         $x = +5;  
  180.                         $y = $imageheight - $logoheight - 5;  
  181.                         break;  
  182.                     case 8:  
  183.                         $x = ($imagewidth - $logowidth) / 2;  
  184.                         $y = $imageheight - $logoheight - 5;  
  185.                         break;  
  186.                     case 9:  
  187.                         $x = $imagewidth - $logowidth - 5;  
  188.                         $y = $imageheight - $logoheight -5;  
  189.                         break;  
  190.                 }  
  191.                 $dst_photo = @imagecreatetruecolor($imagewidth$imageheight);  
  192.                 $target_photo = $imagecreatefunc($this->targetfile);  
  193.                 imagecopy($dst_photo$target_photo, 0, 0, 0, 0, $imagewidth$imageheight);  
  194.                 if($this->watermarktype == 1)  
  195.                 {  
  196.                     imagecopy($dst_photo$watermark_logo$x$y, 0, 0, $logowidth$logoheight);  
  197.                 }  
  198.                 elseif($this->watermarktype == 2)  
  199.                 {  
  200.                     if(($this->watermarktext['shadowx'] || $this->watermarktext['shadowy']) && $this->watermarktext['shadowcolor'])  
  201.                     {  
  202.                         $shadowcolorrgb = explode(','$this->watermarktext['shadowcolor']);  
  203.                         $shadowcolor = imagecolorallocate($dst_photo$shadowcolorrgb[0], $shadowcolorrgb[1], $shadowcolorrgb[2]);  
  204.                         imagettftext($dst_photo$this->watermarktext['size'], $this->watermarktext['angle'],  
  205.                         $x + $ax + $this->watermarktext['shadowx'], $y + $ay + $this->watermarktext['shadowy'], $shadowcolor,  
  206.                         $this->watermarktext['fontpath'], $this->watermarktext['text']);  
  207.                     }  
  208.                     $colorrgb = explode(','$this->watermarktext['color']);  
  209.                     $color = imagecolorallocate($dst_photo$colorrgb[0], $colorrgb[1], $colorrgb[2]);  
  210.                     imagettftext($dst_photo$this->watermarktext['size'], $this->watermarktext['angle'],  
  211.                     $x + $ax$y + $ay$color$this->watermarktext['fontpath'], $this->watermarktext['text']);  
  212.                 }  
  213.                 else 
  214.                 {  
  215.                     imagealphablending($watermark_logo, true);  
  216.                     imagecopymerge($dst_photo$watermark_logo$x$y, 0, 0, $logowidth$logoheight$this->watermarktrans);  
  217.                 }  
  218.                 $targetfile = !$preview ? $this->targetfile : './watermark_tmp.jpg';  
  219.                 if($this->attachinfo['mime'] == 'image/jpeg')  
  220.                 {  
  221.                     $imagefunc($dst_photo$targetfile$this->watermarkquality);  
  222.                 }  
  223.                 else 
  224.                 {  
  225.                     $imagefunc($dst_photo$targetfile);  
  226.                 }  
  227.                 $this->attach['size'] = filesize($this->targetfile);  
  228.             }  
  229.         }  
  230.     }  
  231. }  
  232. ?>  

 


标签(Tag):
------分隔线----------------------------
推荐内容
  • dedecms图片类

    dedecms图片类 ?php class image { var $attachinfo ; var $targetfile ; //图片路...

  • php上传图片

    php上传图片 php上传代码 是一个很好的案例。一看就会用。...

  • php生成缩略图类

    php生成缩略图类,支持自定义高和宽。还支持按高和宽截图。...

热点内容