Chinaunix首页 | 论坛 | 博客
  • 博客访问: 239871
  • 博文数量: 108
  • 博客积分: 3045
  • 博客等级: 中校
  • 技术积分: 1162
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-19 18:26
文章分类

全部博文(108)

分类: PHP

2013-07-19 17:14:14

 

  1. <?php

  2.   

  3. /**

  4. * 功能:生成多种类型的缩略图
  5. *基本参数:$srcFile,$echoType
  6. *方法用到的参数:
  7.                 $toFile,生成的文件
  8.                 $toW,生成的宽
  9.                 $toH,生成的高
  10.                 $bk1,背景颜色参数 以255为最高
  11.                 $bk2,背景颜色参数
  12.                 $bk3,背景颜色参数
  13.   
  14. *例子:
  15.   
  16.     include("thumb.php");
  17.     $cm=new CreatMiniature();
  18.     $cm->SetVar("1.jpg","file");
  19.     $cm->Distortion("dis_bei.jpg",150,200);
  20.     $cm->Prorate("pro_bei.jpg",150,200);
  21.     $cm->Cut("cut_bei.jpg",150,200);
  22.     $cm->BackFill("fill_bei.jpg",150,200);

  23. */

  24.   

  25. class CreatMiniature

  26. {

  27.     //公共变量


  28.     var $srcFile=""; //原图


  29.     var $echoType; //输出图片类型,link--不保存为文件;file--保存为文件


  30.     var $im=""; //临时变量


  31.     var $srcW=""; //原图宽


  32.     var $srcH=""; //原图高


  33.   

  34.     //设置变量及初始化


  35.     function SetVar($srcFile,$echoType)

  36.     {

  37.         if (!file_exists($srcFile)){

  38.             echo '源图片文件不存在!';

  39.             exit();

  40.         }

  41.         $this->srcFile=$srcFile;

  42.         $this->echoType=$echoType;

  43.   

  44.         $info = "";

  45.         $data = GetImageSize($this->srcFile,$info);

  46.         switch ($data[2])

  47.         {

  48.          case 1:

  49.            if(!function_exists("imagecreatefromgif")){

  50.             echo "你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!返回";

  51.             exit();

  52.            }

  53.            $this->im = ImageCreateFromGIF($this->srcFile);

  54.            break;

  55.         case 2:

  56.           if(!function_exists("imagecreatefromjpeg")){

  57.            echo "你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!返回";

  58.            exit();

  59.           }

  60.           $this->im = ImageCreateFromJpeg($this->srcFile);

  61.           break;

  62.         case 3:

  63.           $this->im = ImageCreateFromPNG($this->srcFile);

  64.           break;

  65.       }

  66.       $this->srcW=ImageSX($this->im);

  67.       $this->srcH=ImageSY($this->im);

  68.     }

  69.        

  70.     //生成扭曲型缩图


  71.     function Distortion($toFile,$toW,$toH)

  72.     {

  73.         $cImg=$this->CreatImage($this->im,$toW,$toH,0,0,0,0,$this->srcW,$this->srcH);

  74.         return $this->EchoImage($cImg,$toFile);

  75.         ImageDestroy($cImg);

  76.     }

  77.        

  78.     //生成按比例缩放的缩图


  79.     function PRorate($toFile,$toW,$toH)

  80.     {

  81.         $toWH=$toW/$toH;

  82.         $srcWH=$this->srcW/$this->srcH;

  83.         if($toWH<=$srcWH)

  84.         {

  85.             $ftoW=$toW;

  86.             $ftoH=$ftoW*($this->srcH/$this->srcW);

  87.         }

  88.         else

  89.         {

  90.               $ftoH=$toH;

  91.               $ftoW=$ftoH*($this->srcW/$this->srcH);

  92.         }

  93.         if($this->srcW>$toW||$this->srcH>$toH)

  94.         {

  95.             $cImg=$this->CreatImage($this->im,$ftoW,$ftoH,0,0,0,0,$this->srcW,$this->srcH);

  96.             return $this->EchoImage($cImg,$toFile);

  97.             ImageDestroy($cImg);

  98.         }

  99.         else

  100.         {

  101.             $cImg=$this->CreatImage($this->im,$this->srcW,$this->srcH,0,0,0,0,$this->srcW,$this->srcH);

  102.             return $this->EchoImage($cImg,$toFile);

  103.             ImageDestroy($cImg);

  104.         }

  105.     }

  106.        

  107.     //生成最小裁剪后的缩图


  108.     function Cut($toFile,$toW,$toH)

  109.     {

  110.           $toWH=$toW/$toH;

  111.           $srcWH=$this->srcW/$this->srcH;

  112.           if($toWH<=$srcWH)

  113.           {

  114.                $ctoH=$toH;

  115.                $ctoW=$ctoH*($this->srcW/$this->srcH);

  116.           }

  117.           else

  118.           {

  119.               $ctoW=$toW;

  120.               $ctoH=$ctoW*($this->srcH/$this->srcW);

  121.           }

  122.         $allImg=$this->CreatImage($this->im,$ctoW,$ctoH,0,0,0,0,$this->srcW,$this->srcH);

  123.         $cImg=$this->CreatImage($allImg,$toW,$toH,0,0,($ctoW-$toW)/2,($ctoH-$toH)/2,$toW,$toH);

  124.         return $this->EchoImage($cImg,$toFile);

  125.         ImageDestroy($cImg);

  126.         ImageDestroy($allImg);

  127.     }

  128.   

  129.     //生成背景填充的缩图


  130.     function BackFill($toFile,$toW,$toH,$bk1=255,$bk2=255,$bk3=255)

  131.     {

  132.         $toWH=$toW/$toH;

  133.         $srcWH=$this->srcW/$this->srcH;

  134.         if($toWH<=$srcWH)

  135.         {

  136.             $ftoW=$toW;

  137.             $ftoH=$ftoW*($this->srcH/$this->srcW);

  138.         }

  139.         else

  140.         {

  141.               $ftoH=$toH;

  142.               $ftoW=$ftoH*($this->srcW/$this->srcH);

  143.         }

  144.         if(function_exists("imagecreatetruecolor"))

  145.         {

  146.             @$cImg=ImageCreateTrueColor($toW,$toH);

  147.             if(!$cImg)

  148.             {

  149.                 $cImg=ImageCreate($toW,$toH);

  150.             }

  151.         }

  152.         else

  153.         {

  154.             $cImg=ImageCreate($toW,$toH);

  155.         }

  156.         $backcolor = imagecolorallocate($cImg, $bk1, $bk2, $bk3); //填充的背景颜色


  157.         ImageFilledRectangle($cImg,0,0,$toW,$toH,$backcolor);

  158.         if($this->srcW>$toW||$this->srcH>$toH)

  159.         {

  160.             $proImg=$this->CreatImage($this->im,$ftoW,$ftoH,0,0,0,0,$this->srcW,$this->srcH);

  161.              if($ftoW<$toW)

  162.              {

  163.                  ImageCopy($cImg,$proImg,($toW-$ftoW)/2,0,0,0,$ftoW,$ftoH);

  164.              }

  165.              else if($ftoH<$toH)

  166.              {

  167.                  ImageCopy($cImg,$proImg,0,($toH-$ftoH)/2,0,0,$ftoW,$ftoH);

  168.              }

  169.              else

  170.              {

  171.                  ImageCopy($cImg,$proImg,0,0,0,0,$ftoW,$ftoH);

  172.              }

  173.         }

  174.         else

  175.         {

  176.              ImageCopyMerge($cImg,$this->im,($toW-$ftoW)/2,($toH-$ftoH)/2,0,0,$ftoW,$ftoH,100);

  177.         }

  178.         return $this->EchoImage($cImg,$toFile);

  179.         ImageDestroy($cImg);

  180.     }

  181.        

  182.   

  183.     function CreatImage($img,$creatW,$creatH,$dstX,$dstY,$srcX,$srcY,$srcImgW,$srcImgH)

  184.     {

  185.         if(function_exists("imagecreatetruecolor"))

  186.         {

  187.             @$creatImg = ImageCreateTrueColor($creatW,$creatH);

  188.             if($creatImg)

  189.                 ImageCopyResampled($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);

  190.             else

  191.             {

  192.                 $creatImg=ImageCreate($creatW,$creatH);

  193.                 ImageCopyResized($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);

  194.             }

  195.          }

  196.          else

  197.          {

  198.             $creatImg=ImageCreate($creatW,$creatH);

  199.             ImageCopyResized($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);

  200.          }

  201.          return $creatImg;

  202.     }

  203.        

  204.     //输出图片,link---只输出,不保存文件。file--保存为文件


  205.     function EchoImage($img,$to_File)

  206.     {

  207.         switch($this->echoType)

  208.         {

  209.             case "link":

  210.                 if(function_exists('imagejpeg')) return ImageJpeg($img);

  211.                 else return ImagePNG($img);

  212.                 break;

  213.             case "file":

  214.                 if(function_exists('imagejpeg')) return @ImageJpeg($img,$to_File);

  215.                 else return ImagePNG($img,$to_File);

  216.                 break;

  217.         }

  218.     }

  219. }

  220. $cm=new CreatMiniature();

  221. $cm->SetVar("1.jpg", "file");
  222. $cm->cut("./small/1_samall.jpg", 200, 133);
  223. //$cm->EchoImage($img,"./small/1_samall.jpg");



  224. ?>

阅读(310) | 评论(0) | 转发(0) |
0

上一篇:PHP_封装性

下一篇:岳阳楼记

给主人留下些什么吧!~~