很强的PHP图片处理类

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

   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(!empty($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(empty($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(empty($this->mask_word) && empty($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(!empty($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(empty($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 ($color); $ii++)
 629         {
 630             $arr[] = hexdec(substr($color,$ii,2));
 631             $ii++;
 632         }
 633  
 634         Return $arr;
 635     }
 636  
 637     /**
 638      * 计算出位置坐标
 639      */
 640     function _countMaskPos()
 641     {
 642         if($this->_isFull())
 643         {
 644             switch($this->mask_position)
 645             {
 646                 case 1:
 647                     // 左上
 648                     $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
 649                     $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
 650                     break;
 651  
 652                 case 2:
 653                     // 左下
 654                     $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
 655                     $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;
 656                     break;
 657  
 658                 case 3:
 659                     // 右上
 660                     $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;
 661                     $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
 662                     break;
 663  
 664                 case 4:
 665                     // 右下
 666                     $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;
 667                     $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;
 668                     break;
 669  
 670                 default:
 671                     // 默认将水印放到右下,偏移指定像素
 672                     $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;
 673                     $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;
 674                     break;
 675             }
 676         }
 677         else
 678         {
 679             switch($this->mask_position)
 680             {
 681                 case 1:
 682                     // 左上
 683                     $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
 684                     $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
 685                     break;
 686  
 687                 case 2:
 688                     // 左下
 689                     $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
 690                     $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size;
 691                     break;
 692  
 693                 case 3:
 694                     // 右上
 695                     $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size;
 696                     $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
 697                     break;
 698  
 699                 case 4:
 700                     // 右下
 701                     $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size;
 702                     $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size;
 703                     break;
 704  
 705                 default:
 706                     // 默认将水印放到右下,偏移指定像素
 707                     $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size;
 708                     $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size;
 709                     break;
 710             }
 711         }
 712     }
 713  
 714     /**
 715      * 设置字体信息
 716      */
 717     function _setFontInfo()
 718     {
 719         if(is_numeric($this->font))
 720         {
 721             $this->font_w  = imagefontwidth($this->font);
 722             $this->font_h  = imagefontheight($this->font);
 723  
 724             // 计算水印字体所占宽高
 725             $word_length   = strlen($this->mask_word);
 726             $this->mask_w  = $this->font_w*$word_length;
 727             $this->mask_h  = $this->font_h;
 728         }
 729         else
 730         {
 731             $arr = imagettfbbox ($this->font_size,0, $this->font,$this->mask_word);
 732             $this->mask_w  = abs($arr[0] - $arr[2]);
 733             $this->mask_h  = abs($arr[7] - $arr[1]);
 734         }
 735     }
 736  
 737     /**
 738      * 设置新图尺寸
 739      *
 740      * @param    integer     $img_w   目标宽度
 741      * @param    integer     $img_h   目标高度
 742      */
 743     function _setNewImgSize($img_w, $img_h=null)
 744     {
 745         $num = func_num_args();
 746         if(1 == $num)
 747         {
 748             $this->img_scale = $img_w;// 宽度作为比例
 749             $this->fill_w = round($this->src_w * $this->img_scale / 100) - $this->img_border_size*2;
 750             $this->fill_h = round($this->src_h * $this->img_scale / 100) - $this->img_border_size*2;
 751  
 752             // 源文件起始坐标
 753             $this->src_x  = 0;
 754             $this->src_y  = 0;
 755             $this->copy_w = $this->src_w;
 756             $this->copy_h = $this->src_h;
 757  
 758             // 目标尺寸
 759             $this->dst_w   = $this->fill_w + $this->img_border_size*2;
 760             $this->dst_h   = $this->fill_h + $this->img_border_size*2;
 761         }
 762  
 763         if(2 == $num)
 764         {
 765             $fill_w   = (int)$img_w - $this->img_border_size*2;
 766             $fill_h   = (int)$img_h - $this->img_border_size*2;
 767             if($fill_w < 0 || $fill_h < 0)
 768             {
 769                 die("图片边框过大,已超过了图片的宽度");
 770             }
 771             $rate_w = $this->src_w/$fill_w;
 772             $rate_h = $this->src_h/$fill_h;
 773  
 774             switch($this->cut_type)
 775             {
 776                 case 0:
 777                     // 如果原图大于缩略图,产生缩小,否则不缩小
 778                     if($rate_w < 1 && $rate_h < 1)
 779                     {
 780                         $this->fill_w = (int)$this->src_w;
 781                         $this->fill_h = (int)$this->src_h;
 782                     }
 783                     else
 784                     {
 785                         if($rate_w >= $rate_h)
 786                         {
 787                             $this->fill_w = (int)$fill_w;
 788                             $this->fill_h = round($this->src_h/$rate_w);
 789                         }
 790                         else
 791                         {
 792                             $this->fill_w = round($this->src_w/$rate_h);
 793                             $this->fill_h = (int)$fill_h;
 794                         }
 795                     }
 796  
 797                     $this->src_x  = 0;
 798                     $this->src_y  = 0;
 799  
 800                     $this->copy_w = $this->src_w;
 801                     $this->copy_h = $this->src_h;
 802  
 803                     // 目标尺寸
 804                     $this->dst_w   = $this->fill_w + $this->img_border_size*2;
 805                     $this->dst_h   = $this->fill_h + $this->img_border_size*2;
 806                     break;
 807  
 808                 // 自动裁切
 809                 case 1:
 810                     // 如果图片是缩小剪切才进行操作
 811                     if($rate_w >= 1 && $rate_h >=1)
 812                     {
 813                         if($this->src_w > $this->src_h)
 814                         {
 815                             $src_x = round($this->src_w-$this->src_h)/2;
 816                             $this->setSrcCutPosition($src_x, 0);
 817                             $this->setRectangleCut($fill_h, $fill_h);
 818  
 819                             $this->copy_w = $this->src_h;
 820                             $this->copy_h = $this->src_h;
 821                             
 822                         }
 823                         elseif($this->src_w < $this->src_h)
 824                         {
 825                             $src_y = round($this->src_h-$this->src_w)/2;
 826                             $this->setSrcCutPosition(0, $src_y);
 827                             $this->setRectangleCut($fill_w, $fill_h);
 828  
 829                             $this->copy_w = $this->src_w;
 830                             $this->copy_h = $this->src_w;
 831                         }
 832                         else
 833                         {
 834                             $this->setSrcCutPosition(0, 0);
 835                             $this->copy_w = $this->src_w;
 836                             $this->copy_h = $this->src_w;
 837                             $this->setRectangleCut($fill_w, $fill_h);
 838                         }
 839                     }
 840                     else
 841                     {
 842                         $this->setSrcCutPosition(0, 0);
 843                         $this->setRectangleCut($this->src_w, $this->src_h);
 844  
 845                         $this->copy_w = $this->src_w;
 846                         $this->copy_h = $this->src_h;
 847                     }
 848  
 849                     // 目标尺寸
 850                     $this->dst_w   = $this->fill_w + $this->img_border_size*2;
 851                     $this->dst_h   = $this->fill_h + $this->img_border_size*2;
 852                     
 853                     break;
 854  
 855                 // 手工裁切
 856                 case 2:
 857                     $this->copy_w = $this->fill_w;
 858                     $this->copy_h = $this->fill_h;
 859  
 860                     // 目标尺寸
 861                     $this->dst_w   = $this->fill_w + $this->img_border_size*2;
 862                     $this->dst_h   = $this->fill_h + $this->img_border_size*2;                
 863                     
 864                     break;
 865                 default:
 866                     break;
 867  
 868             }
 869         }
 870  
 871         // 目标文件起始坐标
 872         $this->start_x = $this->img_border_size;
 873         $this->start_y = $this->img_border_size;
 874     }
 875  
 876     /**
 877      * 检查水印图是否大于生成后的图片宽高
 878      */
 879     function _isFull()
 880     {
 881         Return (   $this->mask_w + $this->mask_offset_x > $this->fill_w
 882                 || $this->mask_h + $this->mask_offset_y > $this->fill_h)
 883                    ?true:false;
 884     }
 885  
 886     /**
 887      * 检查水印图是否超过原图
 888      */
 889     function _checkMaskValid()
 890     {
 891         if(    $this->mask_w + $this->mask_offset_x > $this->src_w
 892             || $this->mask_h + $this->mask_offset_y > $this->src_h)
 893         {
 894             die("水印图片尺寸大于原图,请缩小水印图");
 895         }
 896     }
 897  
 898     /**
 899      * 取得图片类型
 900      *
 901      * @param    string     $file_path    文件路径
 902      */
 903     function _getImgType($file_path)
 904     {
 905         $type_list = array("1"=>"gif","2"=>"jpg","3"=>"png","4"=>"swf","5" => "psd","6"=>"bmp","15"=>"wbmp");
 906         if(file_exists($file_path))
 907         {
 908             $img_info = @getimagesize ($file_path);
 909             if(isset($type_list[$img_info[2]]))
 910             {
 911                 Return $type_list[$img_info[2]];
 912             }
 913         }
 914         else
 915         {
 916             die("文件不存在,不能取得文件类型!");
 917         }
 918     }
 919  
 920     /**
 921      * 检查图片类型是否合法,调用了array_key_exists函数,此函数要求
 922      * php版本大于4.1.0
 923      *
 924      * @param    string     $img_type    文件类型
 925      */
 926     function _checkValid($img_type)
 927     {
 928         if(!array_key_exists($img_type, $this->all_type))
 929         {
 930             Return false;
 931         }
 932     }
 933  
 934     /**
 935      * 按指定路径生成目录
 936      *
 937      * @param    string     $path    路径
 938      */
 939     function _mkdirs($path)
 940     {
 941         $adir = explode('/',$path);
 942         $dirlist = '';
 943         $rootdir = array_shift($adir);
 944         if(($rootdir!='.'||$rootdir!='..')&&!file_exists($rootdir))
 945         {
 946             @mkdir($rootdir);
 947         }
 948         foreach($adir as $key=>$val)
 949         {
 950             if($val!='.'&&$val!='..')
 951             {
 952                 $dirlist .= "/".$val;
 953                 $dirpath = $rootdir.$dirlist;
 954                 if(!file_exists($dirpath))
 955                 {
 956                     @mkdir($dirpath);
 957                     @chmod($dirpath,0777);
 958                 }
 959             }
 960         }
 961     }
 962  
 963     /**
 964      * 垂直翻转
 965      *
 966      * @param    string     $src    图片源
 967      */
 968     function _flipV($src)
 969     {
 970         $src_x = $this->getImgWidth($src);
 971         $src_y = $this->getImgHeight($src);
 972  
 973         $new_im = imagecreatetruecolor($src_x, $src_y);
 974         for ($y = 0; $y < $src_y; $y++)
 975         {
 976             imagecopy($new_im, $src, 0, $src_y - $y - 1, 0, $y, $src_x, 1);
 977         }
 978         $this->h_src = $new_im;
 979     }
 980  
 981     /**
 982      * 水平翻转
 983      *
 984      * @param    string     $src    图片源
 985      */
 986     function _flipH($src)
 987     {
 988         $src_x = $this->getImgWidth($src);
 989         $src_y = $this->getImgHeight($src);
 990  
 991         $new_im = imagecreatetruecolor($src_x, $src_y);
 992         for ($x = 0; $x < $src_x; $x++)
 993         {
 994             imagecopy($new_im, $src, $src_x - $x - 1, 0, $x, 0, 1, $src_y);
 995         }
 996         $this->h_src = $new_im;
 997     }
 998 }
 999 ?>
1000 使用实例:
1001 
1002 < ?php
1003 require_once('lib/thumb.class.php');
1004  
1005 $t = new ThumbHandler();
1006  
1007 $t->setSrcImg("img/test.jpg");
1008 $t->setDstImg("tmp/new_test.jpg");
1009 $t->setMaskImg("img/test.gif");
1010 $t->setMaskPosition(1);
1011 $t->setMaskImgPct(80);
1012 $t->setDstImgBorder(4,"#dddddd");
1013  
1014 // 指定缩放比例
1015 $t->createImg(300,200);
1016 ?>
1017 < ?php
1018 require_once('lib/thumb.class.php');
1019  
1020 $t = new ThumbHandler();
1021  
1022 // 基本使用
1023 $t->setSrcImg("img/test.jpg");
1024 $t->setMaskWord("test");
1025 $t->setDstImgBorder(10,"#dddddd");
1026  
1027 // 指定缩放比例
1028 $t->createImg(50);
1029 ?>
1030 < ?php
1031 equire_once('lib/thumb.class.php');
1032  
1033 $t = new ThumbHandler();
1034  
1035 // 基本使用
1036 $t->setSrcImg("img/test.jpg");
1037 $t->setMaskWord("test");
1038  
1039 // 指定固定宽高
1040 $t->createImg(200,200);
1041 ?>
1042 < ?php
1043 require_once('lib/thumb.class.php');
1044  
1045 $t = new ThumbHandler();
1046  
1047 $t->setSrcImg("img/test.jpg");
1048 $t->setDstImg("tmp/new_test.jpg");
1049 $t->setMaskWord("test");
1050  
1051 // 指定固定宽高
1052 $t->createImg(200,200);
1053 ?>
1054 < ?php
1055 require_once('lib/thumb.class.php');
1056  
1057 $t = new ThumbHandler();
1058  
1059 $t->setSrcImg("img/test.jpg");
1060  
1061 // 指定字体文件地址
1062 $t->setMaskFont("c:/winnt/fonts/arial.ttf");
1063 $t->setMaskFontSize(20);
1064 $t->setMaskFontColor("#ffff00");
1065 $t->setMaskWord("test3333333");
1066 $t->setDstImgBorder(99,"#dddddd");
1067 $t->createImg(50);
1068  
1069 ?>
1070 < ?php
1071 require_once('lib/thumb.class.php');
1072  
1073 $t = new ThumbHandler();
1074  
1075 $t->setSrcImg("img/test.jpg");
1076 $t->setMaskOffsetX(55);
1077 $t->setMaskOffsetY(55);
1078 $t->setMaskPosition(1);
1079 //$t->setMaskPosition(2);
1080 //$t->setMaskPosition(3);
1081 //$t->setMaskPosition(4);
1082 $t->setMaskFontColor("#ffff00");
1083 $t->setMaskWord("test");
1084  
1085 // 指定固定宽高
1086 $t->createImg(50);
1087 ?>
1088 < ?php
1089 require_once('lib/thumb.class.php');
1090  
1091 $t = new ThumbHandler();
1092  
1093 $t->setSrcImg("img/test.jpg");
1094 $t->setMaskFont("c:/winnt/fonts/simyou.ttf");
1095 $t->setMaskFontSize(20);
1096 $t->setMaskFontColor("#ffffff");
1097 $t->setMaskTxtPct(20);
1098 $t->setDstImgBorder(10,"#dddddd");
1099 $text = "中文";
1100 $str = mb_convert_encoding($text, "UTF-8", "gb2312");
1101 $t->setMaskWord($str);
1102 $t->setMaskWord("test");
1103  
1104 // 指定固定宽高
1105 $t->createImg(50);
1106 ?>
1107 </strlen>
原文地址:https://www.cnblogs.com/CHEUNGKAMING/p/4080623.html