2010年2月9日星期二

很强的PHP图片处理类(转)

摘自:http://club.topsage.com/viewthread.php?tid=491690

  1. <?php

  2. /**
  3. *  基本图片处理,用于完成图片缩入,水印添加
  4. *  当水印图超过目标图片尺寸时,水印图能自动适应目标图片而缩小
  5. *  水印图可以设置跟背景的合并度
  6. *
  7. *  Copyright(c) 2005 by ustb99. All rights reserved
  8. *
  9. *  To contact the author write to {@link mailto:ustb80@163.com}
  10. *
  11. * @author 偶然
  12. * @version $Id: thumb.class.php,v 1.9 2006/09/30 09:31:56 zengjian Exp $
  13. * @package system
  14. */

  15. /**
  16. * ThumbHandler
  17. * @access public
  18. */

  19. /*
  20. 使用方法:
  21.     自动裁切:
  22.     程序会按照图片的尺寸从中部裁切最大的正方形,并按目标尺寸进行缩略

  23.     $t->setSrcImg("img/test.jpg");
  24.     $t->setCutType(1);//这一句就OK了
  25.     $t->setDstImg("tmp/new_test.jpg");
  26.     $t->createImg(60,60);

  27.     手工裁切:
  28.     程序会按照指定的位置从源图上取图

  29.     $t->setSrcImg("img/test.jpg");
  30.     $t->setCutType(2);//指明为手工裁切
  31.     $t->setSrcCutPosition(100, 100);// 源图起点坐标
  32.     $t->setRectangleCut(300, 200);// 裁切尺寸
  33.     $t->setDstImg("tmp/new_test.jpg");
  34.     $t->createImg(300,200);
  35. */
  36. class ThumbHandler
  37. {
  38.     var $dst_img;// 目标文件
  39.     var $h_src; // 图片资源句柄
  40.     var $h_dst;// 新图句柄
  41.     var $h_mask;// 水印句柄
  42.     var $img_create_quality = 100;// 图片生成质量
  43.     var $img_display_quality = 80;// 图片显示质量,默认为75
  44.     var $img_scale = 0;// 图片缩放比例
  45.     var $src_w = 0;// 原图宽度
  46.     var $src_h = 0;// 原图高度
  47.     var $dst_w = 0;// 新图总宽度
  48.     var $dst_h = 0;// 新图总高度
  49.     var $fill_w;// 填充图形宽
  50.     var $fill_h;// 填充图形高
  51.     var $copy_w;// 拷贝图形宽
  52.     var $copy_h;// 拷贝图形高
  53.     var $src_x = 0;// 原图绘制起始横坐标
  54.     var $src_y = 0;// 原图绘制起始纵坐标
  55.     var $start_x;// 新图绘制起始横坐标
  56.     var $start_y;// 新图绘制起始纵坐标
  57.     var $mask_word;// 水印文字
  58.     var $mask_img;// 水印图片
  59.     var $mask_pos_x = 0;// 水印横坐标
  60.     var $mask_pos_y = 0;// 水印纵坐标
  61.     var $mask_offset_x = 5;// 水印横向偏移
  62.     var $mask_offset_y = 5;// 水印纵向偏移
  63.     var $font_w;// 水印字体宽
  64.     var $font_h;// 水印字体高
  65.     var $mask_w;// 水印宽
  66.     var $mask_h;// 水印高
  67.     var $mask_font_color = "#ffffff";// 水印文字颜色
  68.     var $mask_font = 2;// 水印字体
  69.     var $font_size;// 尺寸
  70.     var $mask_position = 0;// 水印位置
  71.     var $mask_img_pct = 50;// 图片合并程度,值越大,合并程序越低
  72.     var $mask_txt_pct = 50;// 文字合并程度,值越小,合并程序越低
  73.     var $img_border_size = 0;// 图片边框尺寸
  74.     var $img_border_color;// 图片边框颜色
  75.     var $_flip_x=0;// 水平翻转次数
  76.     var $_flip_y=0;// 垂直翻转次数

  77.     var $cut_type=0;// 剪切类型


  78.     var $img_type;// 文件类型

  79.     // 文件类型定义,并指出了输出图片的函数
  80.     var $all_type = array(
  81.         "jpg"  => array("output"=>"imagejpeg"),
  82.         "gif"  => array("output"=>"imagegif"),
  83.         "png"  => array("output"=>"imagepng"),
  84.         "wbmp" => array("output"=>"image2wbmp"),
  85.         "jpeg" => array("output"=>"imagejpeg"));

  86.     /**
  87.      * 构造函数
  88.      */
  89.     function ThumbHandler()
  90.     {
  91.         $this->mask_font_color = "#ffffff";
  92.         $this->font = 2;
  93.         $this->font_size = 12;
  94.     }

  95.     /**
  96.      * 取得图片的宽
  97.      */
  98.     function getImgWidth($src)
  99.     {
  100.         return imagesx($src);
  101.     }

  102.     /**
  103.      * 取得图片的高
  104.      */
  105.     function getImgHeight($src)
  106.     {
  107.         return imagesy($src);
  108.     }

  109.     /**
  110.      * 设置图片生成路径
  111.      *
  112.      * @param    string    $src_img   图片生成路径
  113.      */
  114.     function setSrcImg($src_img, $img_type=null)
  115.     {
  116.         if(!file_exists($src_img))
  117.         {
  118.             die("图片不存在");
  119.         }
  120.         
  121.         if(!empty($img_type))
  122.         {
  123.             $this->img_type = $img_type;
  124.         }
  125.         else
  126.         {
  127.             $this->img_type = $this->_getImgType($src_img);
  128.         }
  129.         
  130.         $this->_checkValid($this->img_type);

  131.         // file_get_contents函数要求php版本>4.3.0
  132.         $src = '';
  133.         if(function_exists("file_get_contents"))
  134.         {
  135.             $src = file_get_contents($src_img);
  136.         }
  137.         else
  138.         {
  139.             $handle = fopen ($src_img, "r");
  140.             while (!feof ($handle))
  141.             {
  142.                 $src .= fgets($fd, 4096);
  143.             }
  144.             fclose ($handle);
  145.         }
  146.         if(empty($src))
  147.         {
  148.             die("图片源为空");
  149.         }
  150.         $this->h_src = @ImageCreateFromString($src);
  151.         $this->src_w = $this->getImgWidth($this->h_src);
  152.         $this->src_h = $this->getImgHeight($this->h_src);
  153.     }

  154.     /**
  155.      * 设置图片生成路径
  156.      *
  157.      * @param    string    $dst_img   图片生成路径
  158.      */
  159.     function setDstImg($dst_img)
  160.     {
  161.         $arr  = explode('/',$dst_img);
  162.         $last = array_pop($arr);
  163.         $path = implode('/',$arr);
  164.         $this->_mkdirs($path);
  165.         $this->dst_img = $dst_img;
  166.     }

  167.     /**
  168.      * 设置图片的显示质量
  169.      *
  170.      * @param    string      $n    质量
  171.      */
  172.     function setImgDisplayQuality($n)
  173.     {
  174.         $this->img_display_quality = (int)$n;
  175.     }

  176.     /**
  177.      * 设置图片的生成质量
  178.      *
  179.      * @param    string      $n    质量
  180.      */
  181.     function setImgCreateQuality($n)
  182.     {
  183.         $this->img_create_quality = (int)$n;
  184.     }

  185.     /**
  186.      * 设置文字水印
  187.      *
  188.      * @param    string     $word    水印文字
  189.      * @param    integer    $font    水印字体
  190.      * @param    string     $color   水印字体颜色
  191.      */
  192.     function setMaskWord($word)
  193.     {
  194.         $this->mask_word .= $word;
  195.     }

  196.     /**
  197.      * 设置字体颜色
  198.      *
  199.      * @param    string     $color    字体颜色
  200.      */
  201.     function setMaskFontColor($color="#ffffff")
  202.     {
  203.         $this->mask_font_color = $color;
  204.     }

  205.     /**
  206.      * 设置水印字体
  207.      *
  208.      * @param    string|integer    $font    字体
  209.      */
  210.     function setMaskFont($font=2)
  211.     {
  212.         if(!is_numeric($font) && !file_exists($font))
  213.         {
  214.             die("字体文件不存在");
  215.         }
  216.         $this->font = $font;
  217.     }

  218.     /**
  219.      * 设置文字字体大小,仅对truetype字体有效
  220.      */
  221.     function setMaskFontSize($size = "12")
  222.     {
  223.         $this->font_size = $size;
  224.     }

  225.     /**
  226.      * 设置图片水印
  227.      *
  228.      * @param    string    $img     水印图片源
  229.      */
  230.     function setMaskImg($img)
  231.     {
  232.         $this->mask_img = $img;
  233.     }

  234.     /**
  235.      * 设置水印横向偏移
  236.      *
  237.      * @param    integer     $x    横向偏移量
  238.      */
  239.     function setMaskOffsetX($x)
  240.     {
  241.         $this->mask_offset_x = (int)$x;
  242.     }

  243.     /**
  244.      * 设置水印纵向偏移
  245.      *
  246.      * @param    integer     $y    纵向偏移量
  247.      */
  248.     function setMaskOffsetY($y)
  249.     {
  250.         $this->mask_offset_y = (int)$y;
  251.     }

  252.     /**
  253.      * 指定水印位置
  254.      *
  255.      * @param    integer     $position    位置,1:左上,2:左下,3:右上,0/4:右下
  256.      */
  257.     function setMaskPosition($position=0)
  258.     {
  259.         $this->mask_position = (int)$position;
  260.     }

  261.     /**
  262.      * 设置图片合并程度
  263.      *
  264.      * @param    integer     $n    合并程度
  265.      */
  266.     function setMaskImgPct($n)
  267.     {
  268.         $this->mask_img_pct = (int)$n;
  269.     }

  270.     /**
  271.      * 设置文字合并程度
  272.      *
  273.      * @param    integer     $n    合并程度
  274.      */
  275.     function setMaskTxtPct($n)
  276.     {
  277.         $this->mask_txt_pct = (int)$n;
  278.     }

  279.     /**
  280.      * 设置缩略图边框
  281.      *
  282.      * @param    (类型)     (参数名)    (描述)
  283.      */
  284.     function setDstImgBorder($size=1, $color="#000000")
  285.     {
  286.         $this->img_border_size  = (int)$size;
  287.         $this->img_border_color = $color;
  288.     }

  289.     /**
  290.      * 水平翻转
  291.      */
  292.     function flipH()
  293.     {
  294.         $this->_flip_x++;
  295.     }

  296.     /**
  297.      * 垂直翻转
  298.      */
  299.     function flipV()
  300.     {
  301.         $this->_flip_y++;
  302.     }

  303.     /**
  304.      * 设置剪切类型
  305.      *
  306.      * @param    (类型)     (参数名)    (描述)
  307.      */
  308.     function setCutType($type)
  309.     {
  310.         $this->cut_type = (int)$type;
  311.     }

  312.     /**
  313.      * 设置图片剪切
  314.      *
  315.      * @param    integer     $width    矩形剪切
  316.      */
  317.     function setRectangleCut($width, $height)
  318.     {
  319.         $this->fill_w = (int)$width;
  320.         $this->fill_h = (int)$height;
  321.     }

  322.     /**
  323.      * 设置源图剪切起始坐标点
  324.      *
  325.      * @param    (类型)     (参数名)    (描述)
  326.      */
  327.     function setSrcCutPosition($x, $y)
  328.     {
  329.         $this->src_x  = (int)$x;
  330.         $this->src_y  = (int)$y;
  331.     }

  332.     /**
  333.      * 创建图片,主函数
  334.      * @param    integer    $a     当缺少第二个参数时,此参数将用作百分比,
  335.      *                             否则作为宽度值
  336.      * @param    integer    $b     图片缩放后的高度
  337.      */
  338.     function createImg($a, $b=null)
  339.     {
  340.         $num = func_num_args();
  341.         if(1 == $num)
  342.         {
  343.             $r = (int)$a;
  344.             if($r < 1)
  345.             {
  346.                 die("图片缩放比例不得小于1");
  347.             }
  348.             $this->img_scale = $r;
  349.             $this->_setNewImgSize($r);
  350.         }

  351.         if(2 == $num)
  352.         {
  353.             $w = (int)$a;
  354.             $h = (int)$b;
  355.             if(0 == $w)
  356.             {
  357.                 die("目标宽度不能为0");
  358.             }
  359.             if(0 == $h)
  360.             {
  361.                 die("目标高度不能为0");
  362.             }
  363.             $this->_setNewImgSize($w, $h);
  364.         }

  365.         if($this->_flip_x%2!=0)
  366.         {
  367.             $this->_flipH($this->h_src);
  368.         }

  369.         if($this->_flip_y%2!=0)
  370.         {
  371.             $this->_flipV($this->h_src);
  372.         }
  373.         $this->_createMask();
  374.         $this->_output();

  375.         // 释放
  376.         if(imagedestroy($this->h_src) && imagedestroy($this->h_dst))
  377.         {
  378.             Return true;
  379.         }
  380.         else
  381.         {
  382.             Return false;
  383.         }
  384.     }

  385.     /**
  386.      * 生成水印,调用了生成水印文字和水印图片两个方法
  387.      */
  388.     function _createMask()
  389.     {
  390.         if($this->mask_word)
  391.         {
  392.             // 获取字体信息
  393.             $this->_setFontInfo();

  394.             if($this->_isFull())
  395.             {
  396.                 die("水印文字过大");
  397.             }
  398.             else
  399.             {
  400.                 $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);
  401.                 $white = ImageColorAllocate($this->h_dst,255,255,255);
  402.                 imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色
  403.                 $this->_drawBorder();
  404.                 imagecopyresampled( $this->h_dst, $this->h_src,
  405.                                     $this->start_x, $this->start_y,
  406.                                     $this->src_x, $this->src_y,
  407.                                     $this->fill_w, $this->fill_h,
  408.                                     $this->copy_w, $this->copy_h);
  409.                 $this->_createMaskWord($this->h_dst);
  410.             }
  411.         }

  412.         if($this->mask_img)
  413.         {
  414.             $this->_loadMaskImg();//加载时,取得宽高

  415.             if($this->_isFull())
  416.             {
  417.                 // 将水印生成在原图上再拷
  418.                 $this->_createMaskImg($this->h_src);
  419.                 $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);
  420.                 $white = ImageColorAllocate($this->h_dst,255,255,255);
  421.                 imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色
  422.                 $this->_drawBorder();
  423.                 imagecopyresampled( $this->h_dst, $this->h_src,
  424.                                     $this->start_x, $this->start_y,
  425.                                     $this->src_x, $this->src_y,
  426.                                     $this->fill_w, $this->start_y,
  427.                                     $this->copy_w, $this->copy_h);
  428.             }
  429.             else
  430.             {
  431.                 // 创建新图并拷贝
  432.                 $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);
  433.                 $white = ImageColorAllocate($this->h_dst,255,255,255);
  434.                 imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色
  435.                 $this->_drawBorder();
  436.                 imagecopyresampled( $this->h_dst, $this->h_src,
  437.                                     $this->start_x, $this->start_y,
  438.                                     $this->src_x, $this->src_y,
  439.                                     $this->fill_w, $this->fill_h,
  440.                                     $this->copy_w, $this->copy_h);
  441.                 $this->_createMaskImg($this->h_dst);
  442.             }
  443.         }

  444.         if(empty($this->mask_word) && empty($this->mask_img))
  445.         {
  446.             $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);
  447.             $white = ImageColorAllocate($this->h_dst,255,255,255);
  448.             imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色
  449.             $this->_drawBorder();

  450.             imagecopyresampled( $this->h_dst, $this->h_src,
  451.                         $this->start_x, $this->start_y,
  452.                         $this->src_x, $this->src_y,
  453.                         $this->fill_w, $this->fill_h,
  454.                         $this->copy_w, $this->copy_h);
  455.         }
  456.     }

  457.     /**
  458.      * 画边框
  459.      */
  460.     function _drawBorder()
  461.     {
  462.         if(!empty($this->img_border_size))
  463.         {
  464.             $c = $this->_parseColor($this->img_border_color);
  465.             $color = ImageColorAllocate($this->h_src,$c[0], $c[1], $c[2]);
  466.             imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$color);// 填充背景色
  467.         }
  468.     }

  469.     /**
  470.      * 生成水印文字
  471.      */
  472.     function _createMaskWord($src)
  473.     {
  474.         $this->_countMaskPos();
  475.         $this->_checkMaskValid();

  476.         $c = $this->_parseColor($this->mask_font_color);
  477.         $color = imagecolorallocatealpha($src, $c[0], $c[1], $c[2], $this->mask_txt_pct);

  478.         if(is_numeric($this->font))
  479.         {
  480.             imagestring($src,
  481.                         $this->font,
  482.                         $this->mask_pos_x, $this->mask_pos_y,
  483.                         $this->mask_word,
  484.                         $color);
  485.         }
  486.         else
  487.         {
  488.             imagettftext($src,
  489.                         $this->font_size, 0,
  490.                         $this->mask_pos_x, $this->mask_pos_y,
  491.                         $color,
  492.                         $this->font,
  493.                         $this->mask_word);
  494.         }
  495.     }

  496.     /**
  497.      * 生成水印图
  498.      */
  499.     function _createMaskImg($src)
  500.     {
  501.         $this->_countMaskPos();
  502.         $this->_checkMaskValid();
  503.         imagecopymerge($src,
  504.                         $this->h_mask,
  505.                         $this->mask_pos_x ,$this->mask_pos_y,
  506.                         0, 0,
  507.                         $this->mask_w, $this->mask_h,
  508.                         $this->mask_img_pct);

  509.         imagedestroy($this->h_mask);
  510.     }

  511.     /**
  512.      * 加载水印图
  513.      */
  514.     function _loadMaskImg()
  515.     {
  516.         $mask_type = $this->_getImgType($this->mask_img);
  517.         $this->_checkValid($mask_type);

  518.         // file_get_contents函数要求php版本>4.3.0
  519.         $src = '';
  520.         if(function_exists("file_get_contents"))
  521.         {
  522.             $src = file_get_contents($this->mask_img);
  523.         }
  524.         else
  525.         {
  526.             $handle = fopen ($this->mask_img, "r");
  527.             while (!feof ($handle))
  528.             {
  529.                 $src .= fgets($fd, 4096);
  530.             }
  531.             fclose ($handle);
  532.         }
  533.         if(empty($this->mask_img))
  534.         {
  535.             die("水印图片为空");
  536.         }
  537.         $this->h_mask = ImageCreateFromString($src);
  538.         $this->mask_w = $this->getImgWidth($this->h_mask);
  539.         $this->mask_h = $this->getImgHeight($this->h_mask);
  540.     }

  541.     /**
  542.      * 图片输出
  543.      */
  544.     function _output()
  545.     {
  546.         $img_type  = $this->img_type;
  547.         $func_name = $this->all_type[$img_type]['output'];
  548.         if(function_exists($func_name))
  549.         {
  550.             // 判断浏览器,若是IE就不发送头
  551.             if(isset($_SERVER['HTTP_USER_AGENT']))
  552.             {
  553.                 $ua = strtoupper($_SERVER['HTTP_USER_AGENT']);
  554.                 if(!preg_match('/^.*MSIE.*\)$/i',$ua))
  555.                 {
  556.                     header("Content-type:$img_type");
  557.                 }
  558.             }
  559.             $func_name($this->h_dst, $this->dst_img, $this->img_display_quality);
  560.         }
  561.         else
  562.         {
  563.             Return false;
  564.         }
  565.     }

  566.     /**
  567.      * 分析颜色
  568.      *
  569.      * @param    string     $color    十六进制颜色
  570.      */
  571.     function _parseColor($color)
  572.     {
  573.         $arr = array();
  574.         for($ii=1; $ii<strlen ($color); $ii++)
  575.         {
  576.             $arr[] = hexdec(substr($color,$ii,2));
  577.             $ii++;
  578.         }

  579.         Return $arr;
  580.     }

  581.     /**
  582.      * 计算出位置坐标
  583.      */
  584.     function _countMaskPos()
  585.     {
  586.         if($this->_isFull())
  587.         {
  588.             switch($this->mask_position)
  589.             {
  590.                 case 1:
  591.                     // 左上
  592.                     $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
  593.                     $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
  594.                     break;

  595.                 case 2:
  596.                     // 左下
  597.                     $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
  598.                     $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;
  599.                     break;

  600.                 case 3:
  601.                     // 右上
  602.                     $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;
  603.                     $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
  604.                     break;

  605.                 case 4:
  606.                     // 右下
  607.                     $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;
  608.                     $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;
  609.                     break;

  610.                 default:
  611.                     // 默认将水印放到右下,偏移指定像素
  612.                     $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;
  613.                     $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;
  614.                     break;
  615.             }
  616.         }
  617.         else
  618.         {
  619.             switch($this->mask_position)
  620.             {
  621.                 case 1:
  622.                     // 左上
  623.                     $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
  624.                     $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
  625.                     break;

  626.                 case 2:
  627.                     // 左下
  628.                     $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
  629.                     $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size;
  630.                     break;

  631.                 case 3:
  632.                     // 右上
  633.                     $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size;
  634.                     $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
  635.                     break;

  636.                 case 4:
  637.                     // 右下
  638.                     $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size;
  639.                     $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size;
  640.                     break;

  641.                 default:
  642.                     // 默认将水印放到右下,偏移指定像素
  643.                     $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size;
  644.                     $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size;
  645.                     break;
  646.             }
  647.         }
  648.     }

  649.     /**
  650.      * 设置字体信息
  651.      */
  652.     function _setFontInfo()
  653.     {
  654.         if(is_numeric($this->font))
  655.         {
  656.             $this->font_w  = imagefontwidth($this->font);
  657.             $this->font_h  = imagefontheight($this->font);

  658.             // 计算水印字体所占宽高
  659.             $word_length   = strlen($this->mask_word);
  660.             $this->mask_w  = $this->font_w*$word_length;
  661.             $this->mask_h  = $this->font_h;
  662.         }
  663.         else
  664.         {
  665.             $arr = imagettfbbox ($this->font_size,0, $this->font,$this->mask_word);
  666.             $this->mask_w  = abs($arr[0] - $arr[2]);
  667.             $this->mask_h  = abs($arr[7] - $arr[1]);
  668.         }
  669.     }

  670.     /**
  671.      * 设置新图尺寸
  672.      *
  673.      * @param    integer     $img_w   目标宽度
  674.      * @param    integer     $img_h   目标高度
  675.      */
  676.     function _setNewImgSize($img_w, $img_h=null)
  677.     {
  678.         $num = func_num_args();
  679.         if(1 == $num)
  680.         {
  681.             $this->img_scale = $img_w;// 宽度作为比例
  682.             $this->fill_w = round($this->src_w * $this->img_scale / 100) - $this->img_border_size*2;
  683.             $this->fill_h = round($this->src_h * $this->img_scale / 100) - $this->img_border_size*2;

  684.             // 源文件起始坐标
  685.             $this->src_x  = 0;
  686.             $this->src_y  = 0;
  687.             $this->copy_w = $this->src_w;
  688.             $this->copy_h = $this->src_h;

  689.             // 目标尺寸
  690.             $this->dst_w   = $this->fill_w + $this->img_border_size*2;
  691.             $this->dst_h   = $this->fill_h + $this->img_border_size*2;
  692.         }

  693.         if(2 == $num)
  694.         {
  695.             $fill_w   = (int)$img_w - $this->img_border_size*2;
  696.             $fill_h   = (int)$img_h - $this->img_border_size*2;
  697.             if($fill_w < 0 || $fill_h < 0)
  698.             {
  699.                 die("图片边框过大,已超过了图片的宽度");
  700.             }
  701.             $rate_w = $this->src_w/$fill_w;
  702.             $rate_h = $this->src_h/$fill_h;

  703.             switch($this->cut_type)
  704.             {
  705.                 case 0:
  706.                     // 如果原图大于缩略图,产生缩小,否则不缩小
  707.                     if($rate_w < 1 && $rate_h < 1)
  708.                     {
  709.                         $this->fill_w = (int)$this->src_w;
  710.                         $this->fill_h = (int)$this->src_h;
  711.                     }
  712.                     else
  713.                     {
  714.                         if($rate_w >= $rate_h)
  715.                         {
  716.                             $this->fill_w = (int)$fill_w;
  717.                             $this->fill_h = round($this->src_h/$rate_w);
  718.                         }
  719.                         else
  720.                         {
  721.                             $this->fill_w = round($this->src_w/$rate_h);
  722.                             $this->fill_h = (int)$fill_h;
  723.                         }
  724.                     }

  725.                     $this->src_x  = 0;
  726.                     $this->src_y  = 0;

  727.                     $this->copy_w = $this->src_w;
  728.                     $this->copy_h = $this->src_h;

  729.                     // 目标尺寸
  730.                     $this->dst_w   = $this->fill_w + $this->img_border_size*2;
  731.                     $this->dst_h   = $this->fill_h + $this->img_border_size*2;
  732.                     break;

  733.                 // 自动裁切
  734.                 case 1:
  735.                     // 如果图片是缩小剪切才进行操作
  736.                     if($rate_w >= 1 && $rate_h >=1)
  737.                     {
  738.                         if($this->src_w > $this->src_h)
  739.                         {
  740.                             $src_x = round($this->src_w-$this->src_h)/2;
  741.                             $this->setSrcCutPosition($src_x, 0);
  742.                             $this->setRectangleCut($fill_h, $fill_h);

  743.                             $this->copy_w = $this->src_h;
  744.                             $this->copy_h = $this->src_h;
  745.                            
  746.                         }
  747.                         elseif($this->src_w < $this->src_h)
  748.                         {
  749.                             $src_y = round($this->src_h-$this->src_w)/2;
  750.                             $this->setSrcCutPosition(0, $src_y);
  751.                             $this->setRectangleCut($fill_w, $fill_h);

  752.                             $this->copy_w = $this->src_w;
  753.                             $this->copy_h = $this->src_w;
  754.                         }
  755.                         else
  756.                         {
  757.                             $this->setSrcCutPosition(0, 0);
  758.                             $this->copy_w = $this->src_w;
  759.                             $this->copy_h = $this->src_w;
  760.                             $this->setRectangleCut($fill_w, $fill_h);
  761.                         }
  762.                     }
  763.                     else
  764.                     {
  765.                         $this->setSrcCutPosition(0, 0);
  766.                         $this->setRectangleCut($this->src_w, $this->src_h);

  767.                         $this->copy_w = $this->src_w;
  768.                         $this->copy_h = $this->src_h;
  769.                     }

  770.                     // 目标尺寸
  771.                     $this->dst_w   = $this->fill_w + $this->img_border_size*2;
  772.                     $this->dst_h   = $this->fill_h + $this->img_border_size*2;
  773.                     
  774.                     break;

  775.                 // 手工裁切
  776.                 case 2:
  777.                     $this->copy_w = $this->fill_w;
  778.                     $this->copy_h = $this->fill_h;

  779.                     // 目标尺寸
  780.                     $this->dst_w   = $this->fill_w + $this->img_border_size*2;
  781.                     $this->dst_h   = $this->fill_h + $this->img_border_size*2;               
  782.                     
  783.                     break;
  784.                 default:
  785.                     break;

  786.             }
  787.         }

  788.         // 目标文件起始坐标
  789.         $this->start_x = $this->img_border_size;
  790.         $this->start_y = $this->img_border_size;
  791.     }

  792.     /**
  793.      * 检查水印图是否大于生成后的图片宽高
  794.      */
  795.     function _isFull()
  796.     {
  797.         Return (   $this->mask_w + $this->mask_offset_x > $this->fill_w
  798.                 || $this->mask_h + $this->mask_offset_y > $this->fill_h)
  799.                    ?true:false;
  800.     }

  801.     /**
  802.      * 检查水印图是否超过原图
  803.      */
  804.     function _checkMaskValid()
  805.     {
  806.         if(    $this->mask_w + $this->mask_offset_x > $this->src_w
  807.             || $this->mask_h + $this->mask_offset_y > $this->src_h)
  808.         {
  809.             die("水印图片尺寸大于原图,请缩小水印图");
  810.         }
  811.     }

  812.     /**
  813.      * 取得图片类型
  814.      *
  815.      * @param    string     $file_path    文件路径
  816.      */
  817.     function _getImgType($file_path)
  818.     {
  819.         $type_list = array("1"=>"gif","2"=>"jpg","3"=>"png","4"=>"swf","5" => "psd","6"=>"bmp","15"=>"wbmp");
  820.         if(file_exists($file_path))
  821.         {
  822.             $img_info = @getimagesize ($file_path);
  823.             if(isset($type_list[$img_info[2]]))
  824.             {
  825.                 Return $type_list[$img_info[2]];
  826.             }
  827.         }
  828.         else
  829.         {
  830.             die("文件不存在,不能取得文件类型!");
  831.         }
  832.     }

  833.     /**
  834.      * 检查图片类型是否合法,调用了array_key_exists函数,此函数要求
  835.      * php版本大于4.1.0
  836.      *
  837.      * @param    string     $img_type    文件类型
  838.      */
  839.     function _checkValid($img_type)
  840.     {
  841.         if(!array_key_exists($img_type, $this->all_type))
  842.         {
  843.             Return false;
  844.         }
  845.     }

  846.     /**
  847.      * 按指定路径生成目录
  848.      *
  849.      * @param    string     $path    路径
  850.      */
  851.     function _mkdirs($path)
  852.     {
  853.         $adir = explode('/',$path);
  854.         $dirlist = '';
  855.         $rootdir = array_shift($adir);
  856.         if(($rootdir!='.'||$rootdir!='..')&&!file_exists($rootdir))
  857.         {
  858.             @mkdir($rootdir);
  859.         }
  860.         foreach($adir as $key=>$val)
  861.         {
  862.             if($val!='.'&&$val!='..')
  863.             {
  864.                 $dirlist .= "/".$val;
  865.                 $dirpath = $rootdir.$dirlist;
  866.                 if(!file_exists($dirpath))
  867.                 {
  868.                     @mkdir($dirpath);
  869.                     @chmod($dirpath,0777);
  870.                 }
  871.             }
  872.         }
  873.     }

  874.     /**
  875.      * 垂直翻转
  876.      *
  877.      * @param    string     $src    图片源
  878.      */
  879.     function _flipV($src)
  880.     {
  881.         $src_x = $this->getImgWidth($src);
  882.         $src_y = $this->getImgHeight($src);

  883.         $new_im = imagecreatetruecolor($src_x, $src_y);
  884.         for ($y = 0; $y < $src_y; $y++)
  885.         {
  886.             imagecopy($new_im, $src, 0, $src_y - $y - 1, 0, $y, $src_x, 1);
  887.         }
  888.         $this->h_src = $new_im;
  889.     }

  890.     /**
  891.      * 水平翻转
  892.      *
  893.      * @param    string     $src    图片源
  894.      */
  895.     function _flipH($src)
  896.     {
  897.         $src_x = $this->getImgWidth($src);
  898.         $src_y = $this->getImgHeight($src);

  899.         $new_im = imagecreatetruecolor($src_x, $src_y);
  900.         for ($x = 0; $x < $src_x; $x++)
  901.         {
  902.             imagecopy($new_im, $src, $src_x - $x - 1, 0, $x, 0, 1, $src_y);
  903.         }
  904.         $this->h_src = $new_im;
  905.     }
  906. }
复制代码

--------------

使用实例:
  1. <?php
  2. require_once('lib/thumb.class.php');

  3. $t = new ThumbHandler();

  4. $t->setSrcImg("img/test.jpg");
  5. $t->setDstImg("tmp/new_test.jpg");
  6. $t->setMaskImg("img/test.gif");
  7. $t->setMaskPosition(1);
  8. $t->setMaskImgPct(80);
  9. $t->setDstImgBorder(4,"#dddddd");

  10. // 指定缩放比例
  11. $t->createImg(300,200);
  12. ?>
复制代码
  1. <?php
  2. require_once('lib/thumb.class.php');

  3. $t = new ThumbHandler();

  4. // 基本使用
  5. $t->setSrcImg("img/test.jpg");
  6. $t->setMaskWord("test");
  7. $t->setDstImgBorder(10,"#dddddd");

  8. // 指定缩放比例
  9. $t->createImg(50);
  10. ?>
复制代码
  1. <?php
  2. equire_once('lib/thumb.class.php');

  3. $t = new ThumbHandler();

  4. // 基本使用
  5. $t->setSrcImg("img/test.jpg");
  6. $t->setMaskWord("test");

  7. // 指定固定宽高
  8. $t->createImg(200,200);
  9. ?>
复制代码
  1. <?php
  2. require_once('lib/thumb.class.php');

  3. $t = new ThumbHandler();

  4. $t->setSrcImg("img/test.jpg");
  5. $t->setDstImg("tmp/new_test.jpg");
  6. $t->setMaskWord("test");

  7. // 指定固定宽高
  8. $t->createImg(200,200);
  9. ?>
复制代码
  1. <?php
  2. require_once('lib/thumb.class.php');

  3. $t = new ThumbHandler();

  4. $t->setSrcImg("img/test.jpg");

  5. // 指定字体文件地址
  6. $t->setMaskFont("c:/winnt/fonts/arial.ttf");
  7. $t->setMaskFontSize(20);
  8. $t->setMaskFontColor("#ffff00");
  9. $t->setMaskWord("test3333333");
  10. $t->setDstImgBorder(99,"#dddddd");
  11. $t->createImg(50);
  12. ?>
复制代码
  1. <?php
  2. require_once('lib/thumb.class.php');

  3. $t = new ThumbHandler();

  4. $t->setSrcImg("img/test.jpg");
  5. $t->setMaskOffsetX(55);
  6. $t->setMaskOffsetY(55);
  7. $t->setMaskPosition(1);
  8. //$t->setMaskPosition(2);
  9. //$t->setMaskPosition(3);
  10. //$t->setMaskPosition(4);
  11. $t->setMaskFontColor("#ffff00");
  12. $t->setMaskWord("test");

  13. // 指定固定宽高
  14. $t->createImg(50);
  15. ?>
复制代码
  1. <?php
  2. require_once('lib/thumb.class.php');

  3. $t = new ThumbHandler();

  4. $t->setSrcImg("img/test.jpg");
  5. $t->setMaskFont("c:/winnt/fonts/simyou.ttf");
  6. $t->setMaskFontSize(20);
  7. $t->setMaskFontColor("#ffffff");
  8. $t->setMaskTxtPct(20);
  9. $t->setDstImgBorder(10,"#dddddd");
  10. $text = "中文";
  11. $str = mb_convert_encoding($text, "UTF-8", "gb2312");
  12. $t->setMaskWord($str);
  13. $t->setMaskWord("test");

  14. // 指定固定宽高
  15. $t->createImg(50);
  16. ?>
  17. </strlen>
  18. ?>
复制代码


没有评论: