Discuz教程网

PHP图片压缩的类

[复制链接]
authicon dly 发表于 2011-1-7 12:40:11 | 显示全部楼层 |阅读模式
  1. <?php
  2. class ThumbHandler
  3. {
  4.     var $dst_img;// 目标文件
  5.     var $h_src; // 图片资源句柄
  6.     var $h_dst;// 新图句柄
  7.     var $h_mask;// 水印句柄
  8.     var $img_create_quality = 100;// 图片生成质量
  9.     var $img_display_quality = 80;// 图片显示质量,默认为75
  10.     var $img_scale = 0;// 图片缩放比例
  11.     var $src_w = 0;// 原图宽度
  12.     var $src_h = 0;// 原图高度
  13.     var $dst_w = 0;// 新图总宽度
  14.     var $dst_h = 0;// 新图总高度
  15.     var $fill_w;// 填充图形宽
  16.     var $fill_h;// 填充图形高
  17.     var $copy_w;// 拷贝图形宽
  18.     var $copy_h;// 拷贝图形高
  19.     var $src_x = 0;// 原图绘制起始横坐标
  20.     var $src_y = 0;// 原图绘制起始纵坐标
  21.     var $start_x;// 新图绘制起始横坐标
  22.     var $start_y;// 新图绘制起始纵坐标
  23.     var $mask_word;// 水印文字
  24.     var $mask_img;// 水印图片
  25.     var $mask_pos_x = 0;// 水印横坐标
  26.     var $mask_pos_y = 0;// 水印纵坐标
  27.     var $mask_offset_x = 5;// 水印横向偏移
  28.     var $mask_offset_y = 5;// 水印纵向偏移
  29.     var $font_w;// 水印字体宽
  30.     var $font_h;// 水印字体高
  31.     var $mask_w;// 水印宽
  32.     var $mask_h;// 水印高
  33.     var $mask_font_color = "#ffffff";// 水印文字颜色
  34.     var $mask_font = 2;// 水印字体
  35.     var $font_size;// 尺寸
  36.     var $mask_position = 0;// 水印位置
  37.     var $mask_img_pct = 50;// 图片合并程度,值越大,合并程序越低
  38.     var $mask_txt_pct = 50;// 文字合并程度,值越小,合并程序越低
  39.     var $img_border_size = 0;// 图片边框尺寸
  40.     var $img_border_color;// 图片边框颜色
  41.     var $_flip_x=0;// 水平翻转次数
  42.     var $_flip_y=0;// 垂直翻转次数

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


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

  45.     // 文件类型定义,并指出了输出图片的函数
  46.     var $all_type = array(
  47.         "jpg"  => array("output"=>"imagejpeg"),
  48.         "gif"  => array("output"=>"imagegif"),
  49.         "png"  => array("output"=>"imagepng"),
  50.         "wbmp" => array("output"=>"image2wbmp"),
  51.         "jpeg" => array("output"=>"imagejpeg"));

  52.     /**
  53.      * 构造函数
  54.      */
  55.     function ThumbHandler()
  56.     {
  57.         $this->mask_font_color = "#ffffff";
  58.         $this->font = 2;
  59.         $this->font_size = 12;
  60.     }

  61.     /**
  62.      * 取得图片的宽
  63.      */
  64.     function getImgWidth($src)
  65.     {
  66.         return imagesx($src);
  67.     }

  68.     /**
  69.      * 取得图片的高
  70.      */
  71.     function getImgHeight($src)
  72.     {
  73.         return imagesy($src);
  74.     }

  75.     /**
  76.      * 设置图片生成路径
  77.      *
  78.      * @param    string    $src_img   图片生成路径
  79.      */
  80.     function setSrcImg($src_img, $img_type=null)
  81.     {
  82.         if(!file_exists($src_img))
  83.         {
  84.             die("图片不存在");
  85.         }
  86.         
  87.         if(!empty($img_type))
  88.         {
  89.             $this->img_type = $img_type;
  90.         }
  91.         else
  92.         {
  93.             $this->img_type = $this->_getImgType($src_img);
  94.         }
  95.         
  96.         $this->_checkValid($this->img_type);

  97.         // file_get_contents函数要求php版本>4.3.0
  98.         $src = \'\';
  99.         if(function_exists("file_get_contents"))
  100.         {
  101.             $src = file_get_contents($src_img);
  102.         }
  103.         else
  104.         {
  105.             $handle = fopen ($src_img, "r");
  106.             while (!feof ($handle))
  107.             {
  108.                $src .= fgets($fd, 4096);
  109.                               
  110.             }
  111.             fclose ($handle);
  112.         }
  113.         if(empty($src))
  114.         {
  115.             die("图片源为空");
  116.         }
  117.         $this->h_src = @ImageCreateFromString($src);
  118.         $this->src_w = $this->getImgWidth($this->h_src);
  119.         $this->src_h = $this->getImgHeight($this->h_src);
  120.     }

  121.     /**
  122.      * 设置图片生成路径
  123.      *
  124.      * @param    string    $dst_img   图片生成路径
  125.      */
  126.     function setDstImg($dst_img)
  127.     {
  128.         $arr  = explode(\'/\',$dst_img);
  129.         $last = array_pop($arr);
  130.         $path = implode(\'/\',$arr);
  131.         $this->_mkdirs($path);
  132.         $this->dst_img = $dst_img;
  133.     }

  134.     /**
  135.      * 设置图片的显示质量
  136.      *
  137.      * @param    string      $n    质量
  138.      */
  139.     function setImgDisplayQuality($n)
  140.     {
  141.         $this->img_display_quality = (int)$n;
  142.     }

  143.     /**
  144.      * 设置图片的生成质量
  145.      *
  146.      * @param    string      $n    质量
  147.      */
  148.     function setImgCreateQuality($n)
  149.     {
  150.         $this->img_create_quality = (int)$n;
  151.     }

  152.     /**
  153.      * 设置文字水印
  154.      *
  155.      * @param    string     $word    水印文字
  156.      * @param    integer    $font    水印字体
  157.      * @param    string     $color   水印字体颜色
  158.      */
  159.     function setMaskWord($word)
  160.     {
  161.         $this->mask_word .= $word;
  162.     }

  163.     /**
  164.      * 设置字体颜色
  165.      *
  166.      * @param    string     $color    字体颜色
  167.      */
  168.     function setMaskFontColor($color="#ffffff")
  169.     {
  170.         $this->mask_font_color = $color;
  171.     }

  172.     /**
  173.      * 设置水印字体
  174.      *
  175.      * @param    string|integer    $font    字体
  176.      */
  177.     function setMaskFont($font=2)
  178.     {
  179.         if(!is_numeric($font) && !file_exists($font))
  180.         {
  181.             die("字体文件不存在");
  182.         }
  183.         $this->font = $font;
  184.     }

  185.     /**
  186.      * 设置文字字体大小,仅对truetype字体有效
  187.      */
  188.     function setMaskFontSize($size = "12")
  189.     {
  190.         $this->font_size = $size;
  191.     }

  192.     /**
  193.      * 设置图片水印
  194.      *
  195.      * @param    string    $img     水印图片源
  196.      */
  197.     function setMaskImg($img)
  198.     {
  199.         $this->mask_img = $img;
  200.     }

  201.     /**
  202.      * 设置水印横向偏移
  203.      *
  204.      * @param    integer     $x    横向偏移量
  205.      */
  206.     function setMaskOffsetX($x)
  207.     {
  208.         $this->mask_offset_x = (int)$x;
  209.     }

  210.     /**
  211.      * 设置水印纵向偏移
  212.      *
  213.      * @param    integer     $y    纵向偏移量
  214.      */
  215.     function setMaskOffsetY($y)
  216.     {
  217.         $this->mask_offset_y = (int)$y;
  218.     }

  219.     /**
  220.      * 指定水印位置
  221.      *
  222.      * @param    integer     $position    位置,1:左上,2:左下,3:右上,0/4:右下
  223.      */
  224.     function setMaskPosition($position=0)
  225.     {
  226.         $this->mask_position = (int)$position;
  227.     }

  228.     /**
  229.      * 设置图片合并程度
  230.      *
  231.      * @param    integer     $n    合并程度
  232.      */
  233.     function setMaskImgPct($n)
  234.     {
  235.         $this->mask_img_pct = (int)$n;
  236.     }

  237.     /**
  238.      * 设置文字合并程度
  239.      *
  240.      * @param    integer     $n    合并程度
  241.      */
  242.     function setMaskTxtPct($n)
  243.     {
  244.         $this->mask_txt_pct = (int)$n;
  245.     }

  246.     /**
  247.      * 设置缩略图边框
  248.      *
  249.      * @param    (类型)     (参数名)    (描述)
  250.      */
  251.     function setDstImgBorder($size=1, $color="#000000")
  252.     {
  253.         $this->img_border_size  = (int)$size;
  254.         $this->img_border_color = $color;
  255.     }

  256.     /**
  257.      * 水平翻转
  258.      */
  259.     function flipH()
  260.     {
  261.         $this->_flip_x++;
  262.     }

  263.     /**
  264.      * 垂直翻转
  265.      */
  266.     function flipV()
  267.     {
  268.         $this->_flip_y++;
  269.     }

  270.     /**
  271.      * 设置剪切类型
  272.      *
  273.      * @param    (类型)     (参数名)    (描述)
  274.      */
  275.     function setCutType($type)
  276.     {
  277.         $this->cut_type = (int)$type;
  278.     }

  279.     /**
  280.      * 设置图片剪切
  281.      *
  282.      * @param    integer     $width    矩形剪切
  283.      */
  284.     function setRectangleCut($width, $height)
  285.     {
  286.         $this->fill_w = (int)$width;
  287.         $this->fill_h = (int)$height;
  288.     }

  289.     /**
  290.      * 设置源图剪切起始坐标点
  291.      *
  292.      * @param    (类型)     (参数名)    (描述)
  293.      */
  294.     function setSrcCutPosition($x, $y)
  295.     {
  296.         $this->src_x  = (int)$x;
  297.         $this->src_y  = (int)$y;
  298.     }

  299.     /**
  300.      * 创建图片,主函数
  301.      * @param    integer    $a     当缺少第二个参数时,此参数将用作百分比,
  302.      *                             否则作为宽度值
  303.      * @param    integer    $b     图片缩放后的高度
  304.      */
  305.     function createImg($a, $b=null)
  306.     {
  307.         $num = func_num_args();
  308.         if(1 == $num)
  309.         {
  310.             $r = (int)$a;
  311.             if($r < 1)
  312.             {
  313.                 die("图片缩放比例不得小于1");
  314.             }
  315.             $this->img_scale = $r;
  316.             $this->_setNewImgSize($r);
  317.         }

  318.         if(2 == $num)
  319.         {
  320.             $w = (int)$a;
  321.             $h = (int)$b;
  322.             if(0 == $w)
  323.             {
  324.                 die("目标宽度不能为0");
  325.             }
  326.             if(0 == $h)
  327.             {
  328.                 die("目标高度不能为0");
  329.             }
  330.             $this->_setNewImgSize($w, $h);
  331.         }

  332.         if($this->_flip_x%2!=0)
  333.         {
  334.             $this->_flipH($this->h_src);
  335.         }

  336.         if($this->_flip_y%2!=0)
  337.         {
  338.             $this->_flipV($this->h_src);
  339.         }
  340.         $this->_createMask();
  341.         $this->_output();

  342.         // 释放
  343.         if(imagedestroy($this->h_src) && imagedestroy($this->h_dst))
  344.         {
  345.             Return true;
  346.         }
  347.         else
  348.         {
  349.             Return false;
  350.         }
  351.     }

  352.     /**
  353.      * 生成水印,调用了生成水印文字和水印图片两个方法
  354.      */
  355.     function _createMask()
  356.     {
  357.         if($this->mask_word)
  358.         {
  359.             // 获取字体信息
  360.             $this->_setFontInfo();

  361.             if($this->_isFull())
  362.             {
  363.                 die("水印文字过大");
  364.             }
  365.             else
  366.             {
  367.                 $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);
  368.                 $white = ImageColorAllocate($this->h_dst,255,255,255);
  369.                 imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色
  370.                 $this->_drawBorder();
  371.                 imagecopyresampled( $this->h_dst, $this->h_src,
  372.                                     $this->start_x, $this->start_y,
  373.                                     $this->src_x, $this->src_y,
  374.                                     $this->fill_w, $this->fill_h,
  375.                                     $this->copy_w, $this->copy_h);
  376.                 $this->_createMaskWord($this->h_dst);
  377.             }
  378.         }

  379.         if($this->mask_img)
  380.         {
  381.             $this->_loadMaskImg();//加载时,取得宽高

  382.             if($this->_isFull())
  383.             {
  384.                 // 将水印生成在原图上再拷
  385.                 $this->_createMaskImg($this->h_src);
  386.                 $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);
  387.                 $white = ImageColorAllocate($this->h_dst,255,255,255);
  388.                 imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色
  389.                 $this->_drawBorder();
  390.                 imagecopyresampled( $this->h_dst, $this->h_src,
  391.                                     $this->start_x, $this->start_y,
  392.                                     $this->src_x, $this->src_y,
  393.                                     $this->fill_w, $this->start_y,
  394.                                     $this->copy_w, $this->copy_h);
  395.             }
  396.             else
  397.             {
  398.                 // 创建新图并拷贝
  399.                 $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);
  400.                 $white = ImageColorAllocate($this->h_dst,255,255,255);
  401.                 imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色
  402.                 $this->_drawBorder();
  403.                 imagecopyresampled( $this->h_dst, $this->h_src,
  404.                                     $this->start_x, $this->start_y,
  405.                                     $this->src_x, $this->src_y,
  406.                                     $this->fill_w, $this->fill_h,
  407.                                     $this->copy_w, $this->copy_h);
  408.                 $this->_createMaskImg($this->h_dst);
  409.             }
  410.         }

  411.         if(empty($this->mask_word) && empty($this->mask_img))
  412.         {
  413.             $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);
  414.             $white = ImageColorAllocate($this->h_dst,255,255,255);
  415.             imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色
  416.             $this->_drawBorder();

  417.             imagecopyresampled( $this->h_dst, $this->h_src,
  418.                         $this->start_x, $this->start_y,
  419.                         $this->src_x, $this->src_y,
  420.                         $this->fill_w, $this->fill_h,
  421.                         $this->copy_w, $this->copy_h);

  422.         }
  423.     }

  424.     /**
  425.      * 画边框
  426.      */
  427.     function _drawBorder()
  428.     {
  429.         if(!empty($this->img_border_size))
  430.         {
  431.             $c = $this->_parseColor($this->img_border_color);
  432.             $color = ImageColorAllocate($this->h_src,$c[0], $c[1], $c[2]);
  433.             imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$color);// 填充背景色
  434.         }
  435.     }

  436.     /**
  437.      * 生成水印文字
  438.      */
  439.     function _createMaskWord($src)
  440.     {
  441.         $this->_countMaskPos();
  442.         $this->_checkMaskValid();

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

  445.         if(is_numeric($this->font))
  446.         {
  447.             imagestring($src,
  448.                         $this->font,
  449.                         $this->mask_pos_x, $this->mask_pos_y,
  450.                         $this->mask_word,
  451.                         $color);
  452.         }
  453.         else
  454.         {
  455.             imagettftext($src,
  456.                         $this->font_size, 0,
  457.                         $this->mask_pos_x, $this->mask_pos_y,
  458.                         $color,
  459.                         $this->font,
  460.                         $this->mask_word);
  461.         }
  462.     }

  463.     /**
  464.      * 生成水印图
  465.      */
  466.     function _createMaskImg($src)
  467.     {
  468.         $this->_countMaskPos();
  469.         $this->_checkMaskValid();
  470.         imagecopymerge($src,
  471.                         $this->h_mask,
  472.                         $this->mask_pos_x ,$this->mask_pos_y,
  473.                         0, 0,
  474.                         $this->mask_w, $this->mask_h,
  475.                         $this->mask_img_pct);

  476.         imagedestroy($this->h_mask);
  477.     }

  478.     /**
  479.      * 加载水印图
  480.      */
  481.     function _loadMaskImg()
  482.     {
  483.         $mask_type = $this->_getImgType($this->mask_img);
  484.         $this->_checkValid($mask_type);

  485.         // file_get_contents函数要求php版本>4.3.0
  486.         $src = \'\';
  487.         if(function_exists("file_get_contents"))
  488.         {
  489.             $src = file_get_contents($this->mask_img);
  490.         }
  491.         else
  492.         {
  493.             $handle = fopen ($this->mask_img, "r");
  494.             while (!feof ($handle))
  495.             {
  496.                 $src .= fgets($fd, 4096);
  497.             }
  498.             fclose ($handle);
  499.         }
  500.         if(empty($this->mask_img))
  501.         {
  502.             die("水印图片为空");
  503.         }
  504.         $this->h_mask = ImageCreateFromString($src);
  505.         $this->mask_w = $this->getImgWidth($this->h_mask);
  506.         $this->mask_h = $this->getImgHeight($this->h_mask);
  507.     }

  508.     /**
  509.      * 图片输出
  510.      */
  511.     function _output()
  512.     {
  513.         $img_type  = $this->img_type;
  514.         $func_name = $this->all_type[$img_type][\'output\'];
  515.         if(function_exists($func_name))
  516.         {
  517.             // 判断浏览器,若是IE就不发送头
  518.             if(isset($_SERVER[\'HTTP_USER_AGENT\']))
  519.             {
  520.                 $ua = strtoupper($_SERVER[\'HTTP_USER_AGENT\']);
  521.                 if(!preg_match(\'/^.*MSIE.*\\)$/i\',$ua))
  522.                 {
  523.                     header("Content-typeimg_type");
  524.                 }
  525.             }
  526.             $func_name($this->h_dst, $this->dst_img, $this->img_display_quality);
  527.         }
  528.         else
  529.         {
  530.             Return false;
  531.         }
  532.     }

  533.     /**
  534.      * 分析颜色
  535.      *
  536.      * @param    string     $color    十六进制颜色
  537.      */
  538.     function _parseColor($color)
  539.     {
  540.         $arr = array();
  541.         for($ii=1; $ii<strlen($color); $ii++)
  542.         {
  543.             $arr[] = hexdec(substr($color,$ii,2));
  544.             $ii++;
  545.         }

  546.         Return $arr;
  547.     }

  548.     /**
  549.      * 计算出位置坐标
  550.      */
  551.     function _countMaskPos()
  552.     {
  553.         if($this->_isFull())
  554.         {
  555.             switch($this->mask_position)
  556.             {
  557.                 case 1:
  558.                     // 左上
  559.                     $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
  560.                     $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
  561.                     break;

  562.                 case 2:
  563.                     // 左下
  564.                     $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
  565.                     $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;
  566.                     break;

  567.                 case 3:
  568.                     // 右上
  569.                     $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;
  570.                     $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
  571.                     break;

  572.                 case 4:
  573.                     // 右下
  574.                     $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;
  575.                     $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;
  576.                     break;

  577.                 default:
  578.                     // 默认将水印放到右下,偏移指定像素
  579.                     $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;
  580.                     $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;
  581.                     break;
  582.             }
  583.         }
  584.         else
  585.         {
  586.             switch($this->mask_position)
  587.             {
  588.                 case 1:
  589.                     // 左上
  590.                     $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
  591.                     $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
  592.                     break;

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

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

  603.                 case 4:
  604.                     // 右下
  605.                     $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size;
  606.                     $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size;
  607.                     break;

  608.                 default:
  609.                     // 默认将水印放到右下,偏移指定像素
  610.                     $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size;
  611.                     $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size;
  612.                     break;
  613.             }
  614.         }
  615.     }

  616.     /**
  617.      * 设置字体信息
  618.      */
  619.     function _setFontInfo()
  620.     {
  621.         if(is_numeric($this->font))
  622.         {
  623.             $this->font_w  = imagefontwidth($this->font);
  624.             $this->font_h  = imagefontheight($this->font);

  625.             // 计算水印字体所占宽高
  626.             $word_length   = strlen($this->mask_word);
  627.             $this->mask_w  = $this->font_w*$word_length;
  628.             $this->mask_h  = $this->font_h;
  629.         }
  630.         else
  631.         {
  632.             $arr = imagettfbbox ($this->font_size,0, $this->font,$this->mask_word);
  633.             $this->mask_w  = abs($arr[0] - $arr[2]);
  634.             $this->mask_h  = abs($arr[7] - $arr[1]);
  635.         }
  636.     }

  637.     /**
  638.      * 设置新图尺寸
  639.      *
  640.      * @param    integer     $img_w   目标宽度
  641.      * @param    integer     $img_h   目标高度
  642.      */
  643.     function _setNewImgSize($img_w, $img_h=null)
  644.     {
  645.         $num = func_num_args();
  646.         if(1 == $num)
  647.         {
  648.             $this->img_scale = $img_w;// 宽度作为比例
  649.             $this->fill_w = round($this->src_w * $this->img_scale / 100) - $this->img_border_size*2;
  650.             $this->fill_h = round($this->src_h * $this->img_scale / 100) - $this->img_border_size*2;

  651.             // 源文件起始坐标
  652.             $this->src_x  = 0;
  653.             $this->src_y  = 0;
  654.             $this->copy_w = $this->src_w;
  655.             $this->copy_h = $this->src_h;

  656.             // 目标尺寸
  657.             $this->dst_w   = $this->fill_w + $this->img_border_size*2;
  658.             $this->dst_h   = $this->fill_h + $this->img_border_size*2;
  659.         }

  660.         if(2 == $num)
  661.         {
  662.             $fill_w   = (int)$img_w - $this->img_border_size*2;
  663.             $fill_h   = (int)$img_h - $this->img_border_size*2;
  664.             if($fill_w < 0 || $fill_h < 0)
  665.             {
  666.                 die("图片边框过大,已超过了图片的宽度");
  667.             }
  668.             $rate_w = $this->src_w/$fill_w;
  669.             $rate_h = $this->src_h/$fill_h;

  670.             switch($this->cut_type)
  671.             {
  672.                 case 0:
  673.                     // 如果原图大于缩略图,产生缩小,否则不缩小
  674.                     if($rate_w < 1 && $rate_h < 1)
  675.                     {
  676.                         $this->fill_w = (int)$this->src_w;
  677.                         $this->fill_h = (int)$this->src_h;
  678.                     }
  679.                     else
  680.                     {
  681.                         if($rate_w >= $rate_h)
  682.                         {
  683.                             $this->fill_w = (int)$fill_w;
  684.                             $this->fill_h = round($this->src_h/$rate_w);
  685.                         }
  686.                         else
  687.                         {
  688.                             $this->fill_w = round($this->src_w/$rate_h);
  689.                             $this->fill_h = (int)$fill_h;
  690.                         }
  691.                     }

  692.                     $this->src_x  = 0;
  693.                     $this->src_y  = 0;

  694.                     $this->copy_w = $this->src_w;
  695.                     $this->copy_h = $this->src_h;

  696.                     // 目标尺寸
  697.                     $this->dst_w   = $this->fill_w + $this->img_border_size*2;
  698.                     $this->dst_h   = $this->fill_h + $this->img_border_size*2;
  699.                     break;

  700.                 // 自动裁切
  701.                 case 1:
  702.                     // 如果图片是缩小剪切才进行操作
  703.                     if($rate_w >= 1 && $rate_h >=1)
  704.                     {
  705.                         if($this->src_w > $this->src_h)
  706.                         {
  707.                             $src_x = round($this->src_w-$this->src_h)/2;
  708.                             $this->setSrcCutPosition($src_x, 0);
  709.                             $this->setRectangleCut($fill_h, $fill_h);

  710.                             $this->copy_w = $this->src_h;
  711.                             $this->copy_h = $this->src_h;
  712.                            
  713.                         }
  714.                         elseif($this->src_w < $this->src_h)
  715.                         {
  716.                             $src_y = round($this->src_h-$this->src_w)/2;
  717.                             $this->setSrcCutPosition(0, $src_y);
  718.                             $this->setRectangleCut($fill_w, $fill_h);

  719.                             $this->copy_w = $this->src_w;
  720.                             $this->copy_h = $this->src_w;
  721.                         }
  722.                         else
  723.                         {
  724.                             $this->setSrcCutPosition(0, 0);
  725.                             $this->copy_w = $this->src_w;
  726.                             $this->copy_h = $this->src_w;
  727.                             $this->setRectangleCut($fill_w, $fill_h);
  728.                         }
  729.                     }
  730.                     else
  731.                     {
  732.                         $this->setSrcCutPosition(0, 0);
  733.                         $this->setRectangleCut($this->src_w, $this->src_h);

  734.                         $this->copy_w = $this->src_w;
  735.                         $this->copy_h = $this->src_h;
  736.                     }

  737.                     // 目标尺寸
  738.                     $this->dst_w   = $this->fill_w + $this->img_border_size*2;
  739.                     $this->dst_h   = $this->fill_h + $this->img_border_size*2;
  740.                     
  741.                     break;

  742.                 // 手工裁切
  743.                 case 2:
  744.                     $this->copy_w = $this->fill_w;
  745.                     $this->copy_h = $this->fill_h;

  746.                     // 目标尺寸
  747.                     $this->dst_w   = $this->fill_w + $this->img_border_size*2;
  748.                     $this->dst_h   = $this->fill_h + $this->img_border_size*2;               
  749.                     
  750.                     break;
  751.                 default:
  752.                     break;

  753.             }
  754.         }

  755.         // 目标文件起始坐标
  756.         $this->start_x = $this->img_border_size;
  757.         $this->start_y = $this->img_border_size;
  758.     }

  759.     /**
  760.      * 检查水印图是否大于生成后的图片宽高
  761.      */
  762.     function _isFull()
  763.     {
  764.         Return (   $this->mask_w + $this->mask_offset_x > $this->fill_w
  765.                 || $this->mask_h + $this->mask_offset_y > $this->fill_h)
  766.                    ?true:false;
  767.     }

  768.     /**
  769.      * 检查水印图是否超过原图
  770.      */
  771.     function _checkMaskValid()
  772.     {
  773.         if(    $this->mask_w + $this->mask_offset_x > $this->src_w
  774.             || $this->mask_h + $this->mask_offset_y > $this->src_h)
  775.         {
  776.             die("水印图片尺寸大于原图,请缩小水印图");
  777.         }
  778.     }

  779.     /**
  780.      * 取得图片类型
  781.      *
  782.      * @param    string     $file_path    文件路径
  783.      */
  784.     function _getImgType($file_path)
  785.     {
  786.         $type_list = array("1"=>"gif","2"=>"jpg","3"=>"png","4"=>"swf","5" => "psd","6"=>"bmp","15"=>"wbmp");
  787.         if(file_exists($file_path))
  788.         {
  789.             $img_info = @getimagesize ($file_path);
  790.             if(isset($type_list[$img_info[2]]))
  791.             {
  792.                 Return $type_list[$img_info[2]];
  793.             }
  794.         }
  795.         else
  796.         {
  797.             die("文件不存在,不能取得文件类型!");
  798.         }
  799.     }

  800.     /**
  801.      * 检查图片类型是否合法,调用了array_key_exists函数,此函数要求
  802.      * php版本大于4.1.0
  803.      *
  804.      * @param    string     $img_type    文件类型
  805.      */
  806.     function _checkValid($img_type)
  807.     {
  808.         if(!array_key_exists($img_type, $this->all_type))
  809.         {
  810.             Return false;
  811.         }
  812.     }

  813.     /**
  814.      * 按指定路径生成目录
  815.      *
  816.      * @param    string     $path    路径
  817.      */
  818.     function _mkdirs($path)
  819.     {
  820.         $adir = explode(\'/\',$path);
  821.         $dirlist = \'\';
  822.         $rootdir = array_shift($adir);
  823.         if(($rootdir!=\'.\'||$rootdir!=\'..\')&&!file_exists($rootdir))
  824.         {
  825.             @mkdir($rootdir);
  826.         }
  827.         foreach($adir as $key=>$val)
  828.         {
  829.             if($val!=\'.\'&&$val!=\'..\')
  830.             {
  831.                 $dirlist .= "/".$val;
  832.                 $dirpath = $rootdir.$dirlist;
  833.                 if(!file_exists($dirpath))
  834.                 {
  835.                     @mkdir($dirpath);
  836.                     @chmod($dirpath,0777);
  837.                 }
  838.             }
  839.         }
  840.     }

  841.     /**
  842.      * 垂直翻转
  843.      *
  844.      * @param    string     $src    图片源
  845.      */
  846.     function _flipV($src)
  847.     {
  848.         $src_x = $this->getImgWidth($src);
  849.         $src_y = $this->getImgHeight($src);

  850.         $new_im = imagecreatetruecolor($src_x, $src_y);
  851.         for ($y = 0; $y < $src_y; $y++)
  852.         {
  853.             imagecopy($new_im, $src, 0, $src_y - $y - 1, 0, $y, $src_x, 1);
  854.         }
  855.         $this->h_src = $new_im;
  856.     }

  857.     /**
  858.      * 水平翻转
  859.      *
  860.      * @param    string     $src    图片源
  861.      */
  862.     function _flipH($src)
  863.     {
  864.         $src_x = $this->getImgWidth($src);
  865.         $src_y = $this->getImgHeight($src);

  866.         $new_im = imagecreatetruecolor($src_x, $src_y);
  867.         for ($x = 0; $x < $src_x; $x++)
  868.         {
  869.             imagecopy($new_im, $src, $src_x - $x - 1, 0, $x, 0, 1, $src_y);
  870.         }
  871.         $this->h_src = $new_im;
  872.     }
  873. }
  874. ?>
复制代码

使用方法
  1. <?php

  2. $img_data = $_FILES[\'image\'][\'tmp_name\'];

  3. $img_name = $_FILES[\'image\'][\'name\']; //图片名

  4. $imgurl="image/$img_name"; //图片路径



  5. $t=new ThumbHandler();      

  6. $t->setSrcImg($img_data);

  7. $t->setCutType(1);//这一句就OK了

  8. $t->setDstImg($imgurl);

  9. $t->createImg(330,296);

  10. ?>
复制代码






上一篇:php四种常用排序方法的基本思想和
下一篇:身份证位数转换及验证
authicon 21585151 发表于 2011-6-16 16:59:42 | 显示全部楼层
偶的天啊!爱死你了
authicon 纤陌陌 发表于 2011-6-21 07:59:36 | 显示全部楼层
楼主威武
authicon 咫尺天 发表于 2011-6-22 14:00:06 | 显示全部楼层
强人,佩服死了。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-3 03:36

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表