php专区

 首页 > php专区 > PHP应用 > php类库 > PHP多功能图片处理类 - php类库

PHP多功能图片处理类 - php类库

分享到:
【字体:
导读:
          !--?php ** *基本图片处理,用于完成图片缩入,水印添加 *当水印图超过目标图片尺寸时,水印图能自动适应目标图片而缩小 *水印图可以...

PHP多功能图片处理类

  1. setSrcImg("img/test.jpg");     
  2.     $t->setCutType(1);//这一句就OK了     
  3.     $t->setDstImg("tmp/new_test.jpg");     
  4.     $t->createImg(60,60);     
  5.       
  6.     手工裁切:     
  7.     程序会按照指定的位置从源图上取图     
  8.       
  9.     $t->setSrcImg("img/test.jpg");     
  10.     $t->setCutType(2);//指明为手工裁切     
  11.     $t->setSrcCutPosition(100, 100);// 源图起点坐标     
  12.     $t->setRectangleCut(300, 200);// 裁切尺寸     
  13.     $t->setDstImg("tmp/new_test.jpg");     
  14.     $t->createImg(300,200);     
  15. */      
  16. class ThumbHandler      
  17. {      
  18.     var $dst_img;// 目标文件      
  19.     var $h_src// 图片资源句柄      
  20.     var $h_dst;// 新图句柄      
  21.     var $h_mask;// 水印句柄      
  22.     var $img_create_quality = 100;// 图片生成质量      
  23.     var $img_display_quality = 80;// 图片显示质量,默认为75      
  24.     var $img_scale = 0;// 图片缩放比例      
  25.     var $src_w = 0;// 原图宽度      
  26.     var $src_h = 0;// 原图高度      
  27.     var $dst_w = 0;// 新图总宽度      
  28.     var $dst_h = 0;// 新图总高度      
  29.     var $fill_w;// 填充图形宽      
  30.     var $fill_h;// 填充图形高      
  31.     var $copy_w;// 拷贝图形宽      
  32.     var $copy_h;// 拷贝图形高      
  33.     var $src_x = 0;// 原图绘制起始横坐标      
  34.     var $src_y = 0;// 原图绘制起始纵坐标      
  35.     var $start_x;// 新图绘制起始横坐标      
  36.     var $start_y;// 新图绘制起始纵坐标      
  37.     var $mask_word;// 水印文字      
  38.     var $mask_img;// 水印图片      
  39.     var $mask_pos_x = 0;// 水印横坐标      
  40.     var $mask_pos_y = 0;// 水印纵坐标      
  41.     var $mask_offset_x = 5;// 水印横向偏移      
  42.     var $mask_offset_y = 5;// 水印纵向偏移      
  43.     var $font_w;// 水印字体宽      
  44.     var $font_h;// 水印字体高      
  45.     var $mask_w;// 水印宽      
  46.     var $mask_h;// 水印高      
  47.     var $mask_font_color = "#ffffff";// 水印文字颜色      
  48.     var $mask_font = 2;// 水印字体      
  49.     var $font_size;// 尺寸      
  50.     var $mask_position = 0;// 水印位置      
  51.     var $mask_img_pct = 50;// 图片合并程度,值越大,合并程序越低      
  52.     var $mask_txt_pct = 50;// 文字合并程度,值越小,合并程序越低      
  53.     var $img_border_size = 0;// 图片边框尺寸      
  54.     var $img_border_color;// 图片边框颜色      
  55.     var $_flip_x=0;// 水平翻转次数      
  56.     var $_flip_y=0;// 垂直翻转次数      
  57.        
  58.     var $cut_type=0;// 剪切类型      
  59.        
  60.        
  61.     var $img_type;// 文件类型      
  62.        
  63.     // 文件类型定义,并指出了输出图片的函数      
  64.     var $all_type = array(      
  65.         "jpg"  => array("output"=>"imagejpeg"),      
  66.         "gif"  => array("output"=>"imagegif"),      
  67.         "png"  => array("output"=>"imagepng"),      
  68.         "wbmp" => array("output"=>"image2wbmp"),      
  69.         "jpeg" => array("output"=>"imagejpeg"));      
  70.        
  71.     /**   
  72.      * 构造函数     
  73.      */      
  74.     function ThumbHandler()      
  75.     {      
  76.         $this->mask_font_color = "#ffffff";      
  77.         $this->font = 2;      
  78.         $this->font_size = 12;      
  79.     }      
  80.        
  81.     /**   
  82.      * 取得图片的宽     
  83.      */      
  84.     function getImgWidth($src)      
  85.     {      
  86.         return imagesx($src);      
  87.     }      
  88.        
  89.     /**   
  90.      * 取得图片的高     
  91.      */      
  92.     function getImgHeight($src)      
  93.     {      
  94.         return imagesy($src);      
  95.     }      
  96.        
  97.     /**   
  98.      * 设置图片生成路径     
  99.      *     
  100.      * @param    string    $src_img   图片生成路径     
  101.      */      
  102.     function setSrcImg($src_img$img_type=null)      
  103.     {      
  104.         if(!file_exists($src_img))      
  105.         {      
  106.             die("图片不存在");      
  107.         }      
  108.               
  109.         if(!emptyempty($img_type))      
  110.         {      
  111.             $this->img_type = $img_type;      
  112.         }      
  113.         else      
  114.         {      
  115.             $this->img_type = $this->_getImgType($src_img);      
  116.         }      
  117.               
  118.         $this->_checkValid($this->img_type);      
  119.        
  120.         // file_get_contents函数要求php版本>4.3.0      
  121.         $src = '';      
  122.         if(function_exists("file_get_contents"))      
  123.         {      
  124.             $src = file_get_contents($src_img);      
  125.         }      
  126.         else      
  127.         {      
  128.             $handle = fopen ($src_img"r");      
  129.             while (!feof ($handle))      
  130.             {      
  131.                 $src .= fgets($fd, 4096);      
  132.             }      
  133.             fclose ($handle);      
  134.         }      
  135.         if(emptyempty($src))      
  136.         {      
  137.             die("图片源为空");      
  138.         }      
  139.         $this->h_src = @ImageCreateFromString($src);      
  140.         $this->src_w = $this->getImgWidth($this->h_src);      
  141.         $this->src_h = $this->getImgHeight($this->h_src);      
  142.     }      
  143.        
  144.     /**   
  145.      * 设置图片生成路径     
  146.      *     
  147.      * @param    string    $dst_img   图片生成路径     
  148.      */      
  149.     function setDstImg($dst_img)      
  150.     {      
  151.         $arr  = explode('/',$dst_img);      
  152.         $last = array_pop($arr);      
  153.         $path = implode('/',$arr);      
  154.         $this->_mkdirs($path);      
  155.         $this->dst_img = $dst_img;      
  156.     }      
  157.        
  158.     /**   
  159.      * 设置图片的显示质量     
  160.      *     
  161.      * @param    string      $n    质量     
  162.      */      
  163.     function setImgDisplayQuality($n)      
  164.     {      
  165.         $this->img_display_quality = (int)$n;      
  166.     }      
  167.        
  168.     /**   
  169.      * 设置图片的生成质量     
  170.      *     
  171.      * @param    string      $n    质量     
  172.      */      
  173.     function setImgCreateQuality($n)      
  174.     {      
  175.         $this->img_create_quality = (int)$n;      
  176.     }      
  177.        
  178.     /**   
  179.      * 设置文字水印     
  180.      *     
  181.      * @param    string     $word    水印文字     
  182.      * @param    integer    $font    水印字体     
  183.      * @param    string     $color   水印字体颜色     
  184.      */      
  185.     function setMaskWord($word)      
  186.     {      
  187.         $this->mask_word .= $word;      
  188.     }      
  189.        
  190.     /**   
  191.      * 设置字体颜色     
  192.      *     
  193.      * @param    string     $color    字体颜色     
  194.      */      
  195.     function setMaskFontColor($color="#ffffff")      
  196.     {      
  197.         $this->mask_font_color = $color;      
  198.     }      
  199.        
  200.     /**   
  201.      * 设置水印字体     
  202.      *     
  203.      * @param    string|integer    $font    字体     
  204.      */      
  205.     function setMaskFont($font=2)      
  206.     {      
  207.         if(!is_numeric($font) && !file_exists($font))      
  208.         {      
  209.             die("字体文件不存在");      
  210.         }      
  211.         $this->font = $font;      
  212.     }      
  213.        
  214.     /**   
  215.      * 设置文字字体大小,仅对truetype字体有效     
  216.      */      
  217.     function setMaskFontSize($size = "12")      
  218.     {      
  219.         $this->font_size = $size;      
  220.     }      
  221.        
  222.     /**   
  223.      * 设置图片水印     
  224.      *     
  225.      * @param    string    $img     水印图片源     
  226.      */      
  227.     function setMaskImg($img)      
  228.     {      
  229.         $this->mask_img = $img;      
  230.     }      
  231.        
  232.     /**   
  233.      * 设置水印横向偏移     
  234.      *     
  235.      * @param    integer     $x    横向偏移量     
  236.      */      
  237.     function setMaskOffsetX($x)      
  238.     {      
  239.         $this->mask_offset_x = (int)$x;      
  240.     }      
  241.        
  242.     /**   
  243.      * 设置水印纵向偏移     
  244.      *     
  245.      * @param    integer     $y    纵向偏移量     
  246.      */      
  247.     function setMaskOffsetY($y)      
  248.     {      
  249.         $this->mask_offset_y = (int)$y;      
  250.     }      
  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.      *     
  265.      * @param    integer     $n    合并程度     
  266.      */      
  267.     function setMaskImgPct($n)      
  268.     {      
  269.         $this->mask_img_pct = (int)$n;      
  270.     }      
  271.        
  272.     /**   
  273.      * 设置文字合并程度     
  274.      *     
  275.      * @param    integer     $n    合并程度     
  276.      */      
  277.     function setMaskTxtPct($n)      
  278.     {      
  279.         $this->mask_txt_pct = (int)$n;      
  280.     }      
  281.        
  282.     /**   
  283.      * 设置缩略图边框     
  284.      *     
  285.      * @param    (类型)     (参数名)    (描述)     
  286.      */      
  287.     function setDstImgBorder($size=1, $color="#000000")      
  288.     {      
  289.         $this->img_border_size  = (int)$size;      
  290.         $this->img_border_color = $color;      
  291.     }      
  292.        
  293.     /**   
  294.      * 水平翻转     
  295.      */      
  296.     function flipH()      
  297.     {      
  298.         $this->_flip_x++;      
  299.     }      
  300.        
  301.     /**   
  302.      * 垂直翻转     
  303.      */      
  304.     function flipV()      
  305.     {      
  306.         $this->_flip_y++;      
  307.     }      
  308.        
  309.     /**   
  310.      * 设置剪切类型     
  311.      *     
  312.      * @param    (类型)     (参数名)    (描述)     
  313.      */      
  314.     function setCutType($type)      
  315.     {      
  316.         $this->cut_type = (int)$type;      
  317.     }      
  318.        
  319.     /**   
  320.      * 设置图片剪切     
  321.      *     
  322.      * @param    integer     $width    矩形剪切     
  323.      */      
  324.     function setRectangleCut($width$height)      
  325.     {      
  326.         $this->fill_w = (int)$width;      
  327.         $this->fill_h = (int)$height;      
  328.     }      
  329.        
  330.     /**   
  331.      * 设置源图剪切起始坐标点     
  332.      *     
  333.      * @param    (类型)     (参数名)    (描述)     
  334.      */      
  335.     function setSrcCutPosition($x$y)      
  336.     {      
  337.         $this->src_x  = (int)$x;      
  338.         $this->src_y  = (int)$y;      
  339.     }      
  340.        
  341.     /**   
  342.      * 创建图片,主函数     
  343.      * @param    integer    $a     当缺少第二个参数时,此参数将用作百分比,     
  344.      *                             否则作为宽度值     
  345.      * @param    integer    $b     图片缩放后的高度     
  346.      */      
  347.     function createImg($a$b=null)      
  348.     {      
  349.         $num = func_num_args();      
  350.         if(1 == $num)      
  351.         {      
  352.             $r = (int)$a;      
  353.             if($r < 1)      
  354.             {      
  355.                 die("图片缩放比例不得小于1");      
  356.             }      
  357.             $this->img_scale = $r;      
  358.             $this->_setNewImgSize($r);      
  359.         }      
  360.        
  361.         if(2 == $num)      
  362.         {      
  363.             $w = (int)$a;      
  364.             $h = (int)$b;      
  365.             if(0 == $w)      
  366.             {      
  367.                 die("目标宽度不能为0");      
  368.             }      
  369.             if(0 == $h)      
  370.             {      
  371.                 die("目标高度不能为0");      
  372.             }      
  373.             $this->_setNewImgSize($w$h);      
  374.         }      
  375.        
  376.         if($this->_flip_x%2!=0)      
  377.         {      
  378.             $this->_flipH($this->h_src);      
  379.         }      
  380.        
  381.         if($this->_flip_y%2!=0)      
  382.         {      
  383.             $this->_flipV($this->h_src);      
  384.         }      
  385.         $this->_createMask();      
  386.         $this->_output();      
  387.        
  388.         // 释放      
  389.         if(imagedestroy($this->h_src) && imagedestroy($this->h_dst))      
  390.         {      
  391.             Return true;      
  392.         }      
  393.         else      
  394.         {      
  395.             Return false;      
  396.         }      
  397.     }      
  398.        
  399.     /**   
  400.      * 生成水印,调用了生成水印文字和水印图片两个方法     
  401.      */      
  402.     function _createMask()      
  403.     {      
  404.         if($this->mask_word)      
  405.         {      
  406.             // 获取字体信息      
  407.             $this->_setFontInfo();      
  408.        
  409.             if($this->_isFull())      
  410.             {      
  411.                 die("水印文字过大");      
  412.             }      
  413.             else      
  414.             {      
  415.                 $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);      
  416.                 $white = ImageColorAllocate($this->h_dst,255,255,255);      
  417.                 imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色      
  418.                 $this->_drawBorder();      
  419.                 imagecopyresampled( $this->h_dst, $this->h_src,      
  420.                                     $this->start_x, $this->start_y,      
  421.                                     $this->src_x, $this->src_y,      
  422.                                     $this->fill_w, $this->fill_h,      
  423.                                     $this->copy_w, $this->copy_h);      
  424.                 $this->_createMaskWord($this->h_dst);      
  425.             }      
  426.         }      
  427.        
  428.         if($this->mask_img)      
  429.         {      
  430.             $this->_loadMaskImg();//加载时,取得宽高      
  431.        
  432.             if($this->_isFull())      
  433.             {      
  434.                 // 将水印生成在原图上再拷      
  435.                 $this->_createMaskImg($this->h_src);      
  436.                 $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);      
  437.                 $white = ImageColorAllocate($this->h_dst,255,255,255);      
  438.                 imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色      
  439.                 $this->_drawBorder();      
  440.                 imagecopyresampled( $this->h_dst, $this->h_src,      
  441.                                     $this->start_x, $this->start_y,      
  442.                                     $this->src_x, $this->src_y,      
  443.                                     $this->fill_w, $this->start_y,      
  444.                                     $this->copy_w, $this->copy_h);      
  445.             }      
  446.             else      
  447.             {      
  448.                 // 创建新图并拷贝      
  449.                 $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);      
  450.                 $white = ImageColorAllocate($this->h_dst,255,255,255);      
  451.                 imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色      
  452.                 $this->_drawBorder();      
  453.                 imagecopyresampled( $this->h_dst, $this->h_src,      
  454.                                     $this->start_x, $this->start_y,      
  455.                                     $this->src_x, $this->src_y,      
  456.                                     $this->fill_w, $this->fill_h,      
  457.                                     $this->copy_w, $this->copy_h);      
  458.                 $this->_createMaskImg($this->h_dst);      
  459.             }      
  460.         }      
  461.        
  462.         if(emptyempty($this->mask_word) && emptyempty($this->mask_img))      
  463.         {      
  464.             $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h);      
  465.             $white = ImageColorAllocate($this->h_dst,255,255,255);      
  466.             imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// 填充背景色      
  467.             $this->_drawBorder();      
  468.        
  469.             imagecopyresampled( $this->h_dst, $this->h_src,      
  470.                         $this->start_x, $this->start_y,      
  471.                         $this->src_x, $this->src_y,      
  472.                         $this->fill_w, $this->fill_h,      
  473.                         $this->copy_w, $this->copy_h);      
  474.         }      
  475.     }      
  476.        
  477.     /**   
  478.      * 画边框     
  479.      */      
  480.     function _drawBorder()      
  481.     {      
  482.         if(!emptyempty($this->img_border_size))      
  483.         {      
  484.             $c = $this->_parseColor($this->img_border_color);      
  485.             $color = ImageColorAllocate($this->h_src,$c[0], $c[1], $c[2]);      
  486.             imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$color);// 填充背景色      
  487.         }      
  488.     }      
  489.        
  490.     /**   
  491.      * 生成水印文字     
  492.      */      
  493.     function _createMaskWord($src)      
  494.     {      
  495.         $this->_countMaskPos();      
  496.         $this->_checkMaskValid();      
  497.        
  498.         $c = $this->_parseColor($this->mask_font_color);      
  499.         $color = imagecolorallocatealpha($src$c[0], $c[1], $c[2], $this->mask_txt_pct);      
  500.        
  501.         if(is_numeric($this->font))      
  502.         {      
  503.             imagestring($src,      
  504.                         $this->font,      
  505.                         $this->mask_pos_x, $this->mask_pos_y,      
  506.                         $this->mask_word,      
  507.                         $color);      
  508.         }      
  509.         else      
  510.         {      
  511.             imagettftext($src,      
  512.                         $this->font_size, 0,      
  513.                         $this->mask_pos_x, $this->mask_pos_y,      
  514.                         $color,      
  515.                         $this->font,      
  516.                         $this->mask_word);      
  517.         }      
  518.     }      
  519.        
  520.     /**   
  521.      * 生成水印图     
  522.      */      
  523.     function _createMaskImg($src)      
  524.     {      
  525.         $this->_countMaskPos();      
  526.         $this->_checkMaskValid();      
  527.         imagecopymerge($src,      
  528.                         $this->h_mask,      
  529.                         $this->mask_pos_x ,$this->mask_pos_y,      
  530.                         0, 0,      
  531.                         $this->mask_w, $this->mask_h,      
  532.                         $this->mask_img_pct);      
  533.        
  534.         imagedestroy($this->h_mask);      
  535.     }      
  536.        
  537.     /**   
  538.      * 加载水印图     
  539.      */      
  540.     function _loadMaskImg()      
  541.     {      
  542.         $mask_type = $this->_getImgType($this->mask_img);      
  543.         $this->_checkValid($mask_type);      
  544.        
  545.         // file_get_contents函数要求php版本>4.3.0      
  546.         $src = '';      
  547.         if(function_exists("file_get_contents"))      
  548.         {      
  549.             $src = file_get_contents($this->mask_img);      
  550.         }      
  551.         else      
  552.         {      
  553.             $handle = fopen ($this->mask_img, "r");      
  554.             while (!feof ($handle))      
  555.             {      
  556.                 $src .= fgets($fd, 4096);      
  557.             }      
  558.             fclose ($handle);      
  559.         }      
  560.         if(emptyempty($this->mask_img))      
  561.         {      
  562.             die("水印图片为空");      
  563.         }      
  564.         $this->h_mask = ImageCreateFromString($src);      
  565.         $this->mask_w = $this->getImgWidth($this->h_mask);      
  566.         $this->mask_h = $this->getImgHeight($this->h_mask);      
  567.     }      
  568.        
  569.     /**   
  570.      * 图片输出     
  571.      */      
  572.     function _output()      
  573.     {      
  574.         $img_type  = $this->img_type;      
  575.         $func_name = $this->all_type[$img_type]['output'];      
  576.         if(function_exists($func_name))      
  577.         {      
  578.             // 判断浏览器,若是IE就不发送头      
  579.             if(isset($_SERVER['HTTP_USER_AGENT']))      
  580.             {      
  581.                 $ua = strtoupper($_SERVER['HTTP_USER_AGENT']);      
  582.                 if(!preg_match('/^.*MSIE.*)$/i',$ua))      
  583.                 {      
  584.                     header("Content-type:$img_type");      
  585.                 }      
  586.             }      
  587.             $func_name($this->h_dst, $this->dst_img, $this->img_display_quality);      
  588.         }      
  589.         else      
  590.         {      
  591.             Return false;      
  592.         }      
  593.     }      
  594.        
  595.     /**   
  596.      * 分析颜色     
  597.      *     
  598.      * @param    string     $color    十六进制颜色     
  599.      */      
  600.     function _parseColor($color)      
  601.     {      
  602.         $arr = array();      
  603.         for($ii=1; $ii<strlen function="" return="" this-="">_isFull())      
  604.         {      
  605.             switch($this->mask_position)      
  606.             {      
  607.                 case 1:      
  608.                     // 左上      
  609.                     $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;      
  610.                     $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;      
  611.                     break;      
  612.        
  613.                 case 2:      
  614.                     // 左下      
  615.                     $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;      
  616.                     $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;      
  617.                     break;      
  618.        
  619.                 case 3:      
  620.                     // 右上      
  621.                     $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;      
  622.                     $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;      
  623.                     break;      
  624.        
  625.                 case 4:      
  626.                     // 右下      
  627.                     $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;      
  628.                     $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;      
  629.                     break;      
  630.        
  631.                 default:      
  632.                     // 默认将水印放到右下,偏移指定像素      
  633.                     $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;      
  634.                     $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;      
  635.                     break;      
  636.             }      
  637.         }      
  638.         else      
  639.         {      
  640.             switch($this->mask_position)      
  641.             {      
  642.                 case 1:      
  643.                     // 左上      
  644.                     $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;      
  645.                     $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;      
  646.                     break;      
  647.        
  648.                 case 2:      
  649.                     // 左下      
  650.                     $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;      
  651.                     $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size;      
  652.                     break;      
  653.        
  654.                 case 3:      
  655.                     // 右上      
  656.                     $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size;      
  657.                     $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;      
  658.                     break;      
  659.        
  660.                 case 4:      
  661.                     // 右下      
  662.                     $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size;      
  663.                     $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size;      
  664.                     break;      
  665.        
  666.                 default:      
  667.                     // 默认将水印放到右下,偏移指定像素      
  668.                     $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size;      
  669.                     $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size;      
  670.                     break;      
  671.             }      
  672.         }      
  673.     }      
  674.        
  675.     /**   
  676.      * 设置字体信息     
  677.      */      
  678.     function _setFontInfo()      
  679.     {      
  680.         if(is_numeric($this->font))      
  681.         {      
  682.             $this->font_w  = imagefontwidth($this->font);      
  683.             $this->font_h  = imagefontheight($this->font);      
  684.        
  685.             // 计算水印字体所占宽高      
  686.             $word_length   = strlen($this->mask_word);      
  687.             $this->mask_w  = $this->font_w*$word_length;      
  688.             $this->mask_h  = $this->font_h;      
  689.         }      
  690.         else      
  691.         {      
  692.             $arr = imagettfbbox ($this->font_size,0, $this->font,$this->mask_word);      
  693.             $this->mask_w  = abs($arr[0] - $arr[2]);      
  694.             $this->mask_h  = abs($arr[7] - $arr[1]);      
  695.         }      
  696.     }      
  697.        
  698.     /**   
  699.      * 设置新图尺寸     
  700.      *     
  701.      * @param    integer     $img_w   目标宽度     
  702.      * @param    integer     $img_h   目标高度     
  703.      */      
  704.     function _setNewImgSize($img_w$img_h=null)      
  705.     {      
  706.         $num = func_num_args();      
  707.         if(1 == $num)      
  708.         {      
  709.             $this->img_scale = $img_w;// 宽度作为比例      
  710.             $this->fill_w = round($this->src_w * $this->img_scale / 100) - $this->img_border_size*2;      
  711.             $this->fill_h = round($this->src_h * $this->img_scale / 100) - $this->img_border_size*2;      
  712.        
  713.             // 源文件起始坐标      
  714.             $this->src_x  = 0;      
  715.             $this->src_y  = 0;      
  716.             $this->copy_w = $this->src_w;      
  717.             $this->copy_h = $this->src_h;      
  718.        
  719.             // 目标尺寸      
  720.             $this->dst_w   = $this->fill_w + $this->img_border_size*2;      
  721.             $this->dst_h   = $this->fill_h + $this->img_border_size*2;      
  722.         }      
  723.        
  724.         if(2 == $num)      
  725.         {      
  726.             $fill_w   = (int)$img_w - $this->img_border_size*2;      
  727.             $fill_h   = (int)$img_h - $this->img_border_size*2;      
  728.             if($fill_w < 0 || $fill_h < 0)      
  729.             {      
  730.                 die("图片边框过大,已超过了图片的宽度");      
  731.             }      
  732.             $rate_w = $this->src_w/$fill_w;      
  733.             $rate_h = $this->src_h/$fill_h;      
  734.        
  735.             switch($this->cut_type)      
  736.             {      
  737.                 case 0:      
  738.                     // 如果原图大于缩略图,产生缩小,否则不缩小      
  739.                     if($rate_w < 1 && $rate_h < 1)      
  740.                     {      
  741.                         $this->fill_w = (int)$this->src_w;      
  742.                         $this->fill_h = (int)$this->src_h;      
  743.                     }      
  744.                     else      
  745.                     {      
  746.                         if($rate_w >= $rate_h)      
  747.                         {      
  748.                             $this->fill_w = (int)$fill_w;      
  749.                             $this->fill_h = round($this->src_h/$rate_w);      
  750.                         }      
  751.                         else      
  752.                         {      
  753.                             $this->fill_w = round($this->src_w/$rate_h);      
  754.                             $this->fill_h = (int)$fill_h;      
  755.                         }      
  756.                     }      
  757.        
  758.                     $this->src_x  = 0;      
  759.                     $this->src_y  = 0;      
  760.        
  761.                     $this->copy_w = $this->src_w;      
  762.                     $this->copy_h = $this->src_h;      
  763.        
  764.                     // 目标尺寸      
  765.                     $this->dst_w   = $this->fill_w + $this->img_border_size*2;      
  766.                     $this->dst_h   = $this->fill_h + $this->img_border_size*2;      
  767.                     break;      
  768.        
  769.                 // 自动裁切      
  770.                 case 1:      
  771.                     // 如果图片是缩小剪切才进行操作      
  772.                     if($rate_w >= 1 && $rate_h >=1)      
  773.                     {      
  774.                         if($this->src_w > $this->src_h)      
  775.                         {      
  776.                             $src_x = round($this->src_w-$this->src_h)/2;      
  777.                             $this->setSrcCutPosition($src_x, 0);      
  778.                             $this->setRectangleCut($fill_h$fill_h);      
  779.        
  780.                             $this->copy_w = $this->src_h;      
  781.                             $this->copy_h = $this->src_h;      
  782.                                   
  783.                         }      
  784.                         elseif($this->src_w < $this->src_h)      
  785.                         {      
  786.                             $src_y = round($this->src_h-$this->src_w)/2;      
  787.                             $this->setSrcCutPosition(0, $src_y);      
  788.                             $this->setRectangleCut($fill_w$fill_h);      
  789.        
  790.                             $this->copy_w = $this->src_w;      
  791.                             $this->copy_h = $this->src_w;      
  792.                         }      
  793.                         else      
  794.                         {      
  795.                             $this->setSrcCutPosition(0, 0);      
  796.                             $this->copy_w = $this->src_w;      
  797.                             $this->copy_h = $this->src_w;      
  798.                             $this->setRectangleCut($fill_w$fill_h);      
  799.                         }      
  800.                     }      
  801.                     else      
  802.                     {      
  803.                         $this->setSrcCutPosition(0, 0);      
  804.                         $this->setRectangleCut($this->src_w, $this->src_h);      
  805.        
  806.                         $this->copy_w = $this->src_w;      
  807.                         $this->copy_h = $this->src_h;      
  808.                     }      
  809.        
  810.                     // 目标尺寸      
  811.                     $this->dst_w   = $this->fill_w + $this->img_border_size*2;      
  812.                     $this->dst_h   = $this->fill_h + $this->img_border_size*2;      
  813.                           
  814.                     break;      
  815.        
  816.                 // 手工裁切      
  817.                 case 2:      
  818.                     $this->copy_w = $this->fill_w;      
  819.                     $this->copy_h = $this->fill_h;      
  820.        
  821.                     // 目标尺寸      
  822.                     $this->dst_w   = $this->fill_w + $this->img_border_size*2;      
  823.                     $this->dst_h   = $this->fill_h + $this->img_border_size*2;                     
  824.                           
  825.                     break;      
  826.                 default:      
  827.                     break;      
  828.        
  829.             }      
  830.         }      
  831.        
  832.         // 目标文件起始坐标      
  833.         $this->start_x = $this->img_border_size;      
  834.         $this->start_y = $this->img_border_size;      
  835.     }      
  836.        
  837.     /**   
  838.      * 检查水印图是否大于生成后的图片宽高     
  839.      */      
  840.     function _isFull()      
  841.     {      
  842.         Return (   $this->mask_w + $this->mask_offset_x > $this->fill_w      
  843.                 || $this->mask_h + $this->mask_offset_y > $this->fill_h)      
  844.                    ?true:false;      
  845.     }      
  846.        
  847.     /**   
  848.      * 检查水印图是否超过原图     
  849.      */      
  850.     function _checkMaskValid()      
  851.     {      
  852.         if(    $this->mask_w + $this->mask_offset_x > $this->src_w      
  853.             || $this->mask_h + $this->mask_offset_y > $this->src_h)      
  854.         {      
  855.             die("水印图片尺寸大于原图,请缩小水印图");      
  856.         }      
  857.     }      
  858.        
  859.     /**   
  860.      * 取得图片类型     
  861.      *     
  862.      * @param    string     $file_path    文件路径     
  863.      */      
  864.     function _getImgType($file_path)      
  865.     {      
  866.         $type_list = array("1"=>"gif","2"=>"jpg","3"=>"png","4"=>"swf","5" => "psd","6"=>"bmp","15"=>"wbmp");      
  867.         if(file_exists($file_path))      
  868.         {      
  869.             $img_info = @getimagesize ($file_path);      
  870.             if(isset($type_list[$img_info[2]]))      
  871.             {      
  872.                 Return $type_list[$img_info[2]];      
  873.             }      
  874.         }      
  875.         else      
  876.         {      
  877.             die("文件不存在,不能取得文件类型!");      
  878.         }      
  879.     }      
  880.        
  881.     /**   
  882.      * 检查图片类型是否合法,调用了array_key_exists函数,此函数要求     
  883.      * php版本大于4.1.0     
  884.      *     
  885.      * @param    string     $img_type    文件类型     
  886.      */      
  887.     function _checkValid($img_type)      
  888.     {      
  889.         if(!array_key_exists($img_type$this->all_type))      
  890.         {      
  891.             Return false;      
  892.         }      
  893.     }      
  894.        
  895.     /**   
  896.      * 按指定路径生成目录     
  897.      *     
  898.      * @param    string     $path    路径     
  899.      */      
  900.     function _mkdirs($path)      
  901.     {      
  902.         $adir = explode('/',$path);      
  903.         $dirlist = '';      
  904.         $rootdir = array_shift($adir);      
  905.         if(($rootdir!='.'||$rootdir!='..')&&!file_exists($rootdir))      
  906.         {      
  907.             @mkdir($rootdir);      
  908.         }      
  909.         foreach($adir as $key=>$val)      
  910.         {      
  911.             if($val!='.'&&$val!='..')      
  912.             {      
  913.                 $dirlist .= "/".$val;      
  914.                 $dirpath = $rootdir.$dirlist;      
  915.                 if(!file_exists($dirpath))      
  916.                 {      
  917.                     @mkdir($dirpath);      
  918.                     @chmod($dirpath,0777);      
  919.                 }      
  920.             }      
  921.         }      
  922.     }      
  923.        
  924.     /**   
  925.      * 垂直翻转     
  926.      *     
  927.      * @param    string     $src    图片源     
  928.      */      
  929.     function _flipV($src)      
  930.     {      
  931.         $src_x = $this->getImgWidth($src);      
  932.         $src_y = $this->getImgHeight($src);      
  933.        
  934.         $new_im = imagecreatetruecolor($src_x$src_y);      
  935.         for ($y = 0; $y < $src_y$y++)      
  936.         {      
  937.             imagecopy($new_im$src, 0, $src_y - $y - 1, 0, $y$src_x, 1);      
  938.         }      
  939.         $this->h_src = $new_im;      
  940.     }      
  941.        
  942.     /**   
  943.      * 水平翻转     
  944.      *     
  945.      * @param    string     $src    图片源     
  946.      */      
  947.     function _flipH($src)      
  948.     {      
  949.         $src_x = $this->getImgWidth($src);      
  950.         $src_y = $this->getImgHeight($src);      
  951.        
  952.         $new_im = imagecreatetruecolor($src_x$src_y);      
  953.         for ($x = 0; $x < $src_x$x++)      
  954.         {      
  955.             imagecopy($new_im$src$src_x - $x - 1, 0, $x, 0, 1, $src_y);      
  956.         }      
  957.         $this->h_src = $new_im;      
  958.     }      
  959. }      
  960. ?>  
  961.  
  962. 函数描述及例子  
  963.    
  964. 使用实例:      
  965.        
  966. < ?php      
  967. require_once('lib/thumb.class.php');      
  968.        
  969. $t = new ThumbHandler();      
  970.        
  971. $t->setSrcImg("img/test.jpg");      
  972. $t->setDstImg("tmp/new_test.jpg");      
  973. $t->setMaskImg("img/test.gif");      
  974. $t->setMaskPosition(1);      
  975. $t->setMaskImgPct(80);      
  976. $t->setDstImgBorder(4,"#dddddd");      
  977.        
  978. // 指定缩放比例      
  979. $t->createImg(300,200);      
  980. ?>      
  981. setSrcImg("img/test.jpg");      
  982. $t->setMaskWord("test");      
  983. $t->setDstImgBorder(10,"#dddddd");      
  984.        
  985. // 指定缩放比例      
  986. $t->createImg(50);      
  987. ?>      
  988. setSrcImg("img/test.jpg");      
  989. $t->setMaskWord("test");      
  990.        
  991. // 指定固定宽高      
  992. $t->createImg(200,200);      
  993. ?>      
  994. setSrcImg("img/test.jpg");      
  995. $t->setDstImg("tmp/new_test.jpg");      
  996. $t->setMaskWord("test");      
  997.        
  998. // 指定固定宽高      
  999. $t->createImg(200,200);      
  1000. ?>      
  1001. setSrcImg("img/test.jpg");      
  1002.        
  1003. // 指定字体文件地址      
  1004. $t->setMaskFont("c:/winnt/fonts/arial.ttf");      
  1005. $t->setMaskFontSize(20);      
  1006. $t->setMaskFontColor("#ffff00");      
  1007. $t->setMaskWord("test3333333");      
  1008. $t->setDstImgBorder(99,"#dddddd");      
  1009. $t->createImg(50);      
  1010.        
  1011. ?>      
  1012. setSrcImg("img/test.jpg");      
  1013. $t->setMaskOffsetX(55);      
  1014. $t->setMaskOffsetY(55);      
  1015. $t->setMaskPosition(1);      
  1016. //$t->setMaskPosition(2);      
  1017. //$t->setMaskPosition(3);      
  1018. //$t->setMaskPosition(4);      
  1019. $t->setMaskFontColor("#ffff00");      
  1020. $t->setMaskWord("test");      
  1021.        
  1022. // 指定固定宽高      
  1023. $t->createImg(50);      
  1024. ?>      
  1025. setSrcImg("img/test.jpg");      
  1026. $t->setMaskFont("c:/winnt/fonts/simyou.ttf");      
  1027. $t->setMaskFontSize(20);      
  1028. $t->setMaskFontColor("#ffffff");      
  1029. $t->setMaskTxtPct(20);      
  1030. $t->setDstImgBorder(10,"#dddddd");      
  1031. $text = "中文";      
  1032. $str = mb_convert_encoding($text"UTF-8""gb2312");      
  1033. $t->setMaskWord($str);      
  1034. $t->setMaskWord("test");      
  1035.        
  1036. // 指定固定宽高      
  1037. $t->createImg(50);      
  1038. ?> strlen

 

分享到:
PHP 生成缩略图的类 - php类库
PHP 生成缩略图的类 PHP代码   /** * 功能:生成缩略图 * 作者:phpox * 日期:Thu May 17 09:57:05 CST 2007 */ class CreatMiniature { //公共变量 var $srcFile=""; //原图 var $echoType; //输出图片类型,link--不保存为文件;file--保存为文件 var $im=""; //临时变量 var $...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……