亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 語言 > PHP > 正文

PHP多功能圖片處理類

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

 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产成人avxxxxx在线看| 国产精品va在线| 91国内产香蕉| 国产亚洲欧洲黄色| 日韩av综合网| 最近更新的2019中文字幕| 一区二区三区回区在观看免费视频| 欧美精品久久久久久久久| 全球成人中文在线| 奇米四色中文综合久久| 亚洲四色影视在线观看| 欧洲亚洲在线视频| 亚洲国产美女精品久久久久∴| 欧美区在线播放| 色偷偷噜噜噜亚洲男人的天堂| 欧美激情xxxx性bbbb| 亚洲免费人成在线视频观看| 欧美亚洲在线播放| 精品五月天久久| 亚洲国产婷婷香蕉久久久久久| 精品国产户外野外| 国产成人精品免费视频| 日韩欧美高清在线视频| 2019中文字幕在线观看| 久久精品一区中文字幕| 久久久999精品免费| 亚洲桃花岛网站| 国产视频欧美视频| 国产91精品黑色丝袜高跟鞋| 亚洲精品网址在线观看| 亚洲国产精品热久久| 亚洲欧美日韩中文在线制服| 亚洲福利精品在线| 欧美第一黄网免费网站| 三级精品视频久久久久| 97婷婷大伊香蕉精品视频| 亚洲成人在线视频播放| 精品国产1区2区| 黑人极品videos精品欧美裸| 亚洲毛片在线免费观看| 欧亚精品在线观看| 久久91精品国产91久久久| 91av国产在线| 超碰97人人做人人爱少妇| 亚洲一区二区三| 亚洲人成网站在线播| 九九视频这里只有精品| 欧美精品激情blacked18| 亚洲欧美在线第一页| 久久精品视频播放| 高清日韩电视剧大全免费播放在线观看| 中文字幕久热精品视频在线| 97久久精品国产| 久久99青青精品免费观看| www欧美日韩| 日韩在线视频导航| 国产97在线亚洲| 国产精品一区二区久久| 久久精品视频播放| 亚洲自拍av在线| 欧美日韩国产精品| 91精品国产91| 日韩电影在线观看免费| 日韩中文字幕国产精品| 成人国产精品一区二区| 欧美精品成人91久久久久久久| 国产精品第二页| 欧美韩日一区二区| 亚洲视频免费一区| 中文字幕日韩专区| 日韩三级影视基地| 国产精品户外野外| 97国产一区二区精品久久呦| 欧美中文字幕视频| 一区二区三区天堂av| 成人深夜直播免费观看| 日韩在线免费观看视频| 日韩精品中文字幕视频在线| 91香蕉嫩草神马影院在线观看| 国产精品香蕉在线观看| 欧美性猛交xxxx| 欧美性xxxxxxxxx| 国内精品小视频在线观看| 91精品国产乱码久久久久久久久| 久久99精品久久久久久琪琪| 97久久精品在线| 在线精品高清中文字幕| 欧美成人午夜影院| 亚洲日韩第一页| 国产成人精品在线播放| 亚洲自拍中文字幕| 久久久视频免费观看| 国产手机视频精品| 亚洲人成电影网站色…| 成人免费视频在线观看超级碰| 中文在线资源观看视频网站免费不卡| 国产精品综合网站| 中文字幕欧美专区| 国产欧美最新羞羞视频在线观看| 欧美激情视频一区二区三区不卡| 欧美日韩aaaa| 久久久精品久久久久| 最近2019免费中文字幕视频三| 亚洲免费电影在线观看| 中文字幕亚洲欧美日韩2019| 91色精品视频在线| 日韩一区二区三区国产| 91精品国产91久久久久久不卡| 日韩精品在线免费| 亚洲美女精品久久| 最新中文字幕亚洲| 国产成人精品电影久久久| 日韩**中文字幕毛片| 亚洲人成电影在线观看天堂色| 亚洲第一黄色网| 久久精品国产欧美激情| 日韩电影免费观看中文字幕| 欧美亚洲激情在线| 日韩精品视频免费| 欧美专区中文字幕| 成人有码在线播放| 欧美电影免费在线观看| 欧美最猛黑人xxxx黑人猛叫黄| 亚洲电影免费观看高清完整版| 欧美美女操人视频| 精品中文字幕久久久久久| 亚洲精品电影网| 欧美日韩成人在线视频| 亚洲人成网站999久久久综合| 日本亚洲欧洲色| 久久夜色精品国产| 欧美成人高清视频| 红桃视频成人在线观看| 亚洲国产精品va| 91高清视频免费观看| 中文字幕精品av| 欧美激情在线观看视频| 成人免费视频网| 久久久久久久久久国产精品| 欧美日韩中文字幕日韩欧美| 中文日韩在线观看| 久久久精品视频成人| 在线播放亚洲激情| 精品视频—区二区三区免费| 欧美在线日韩在线| 久热精品视频在线| 欧美亚洲一级片| 欧美成年人视频网站欧美| 欧美激情免费看| 91在线精品播放| 91爱爱小视频k| 日韩av在线免费播放| 欧美黄色性视频| 国外色69视频在线观看| 亚洲第一区中文字幕| 久久五月情影视| 97在线看免费观看视频在线观看| 久久久久久久久久久成人| 欧美中文字幕在线播放| 精品综合久久久久久97| 久久露脸国产精品| 亚洲欧美精品伊人久久| 裸体女人亚洲精品一区| 亚洲网站在线观看|