当前位置:首页 > PHP教程 > php图像处理 > 列表

PHP智能把图片生成缩略图类

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-18 15:37:32 浏览: 评论:0 

一个PHP智能把图片生成缩略图类,可自动根据你图片的大小生成等比例的图片,PHP智能把图片生成缩略图类代码如下:

  1. <?php 
  2.  /***************************************  *作者:落梦天蝎(beluckly)  
  3.  *开源代码phpfensi.com 
  4.  *类名:CreatMiniature  
  5.  *功能:生成多种类型的缩略图  
  6.  *基本参数:$srcFile,$echoType  
  7.  *方法用到的参数:  
  8.                  $toFile,生成的文件  
  9.                  $toW,生成的宽  
  10.                  $toH,生成的高  
  11.                  $bk1,背景颜色参数 以255为最高  
  12.                  $bk2,背景颜色参数  
  13.                  $bk3,背景颜色参数  
  14.     
  15.  *例子:  
  16.     
  17.      include("thumb.php");  
  18.      $cm=new CreatMiniature();  
  19.      $cm->SetVar("1.jpg","file");  
  20.      $cm->Distortion("dis_bei.jpg",150,200);  
  21.      $cm->Prorate("pro_bei.jpg",150,200);  
  22.      $cm->Cut("cut_bei.jpg",150,200);  
  23.       $cm->BackFill("fill_bei.jpg",150,200);  
  24.     
  25.  ***************************************/  
  26.     
  27.  class CreatMiniature  
  28.  {  
  29.      //公共变量  
  30.      var $srcFile="";        //原图  
  31.      var $echoType;            //输出图片类型,link--不保存为文件;file--保存为文件  
  32.      var $im="";                //临时变量  
  33.      var $srcW="";            //原图宽  
  34.      var $srcH="";            //原图高  
  35.     
  36.      //设置变量及初始化  
  37.      function SetVar($srcFile,$echoType)  
  38.      {  
  39.          $this->srcFile=$srcFile;  
  40.          $this->echoType=$echoType;  
  41.     
  42.          $info = "";  
  43.          $data = GetImageSize($this->srcFile,$info);  
  44.          switch ($data[2])          {  
  45.           case 1:  
  46.             if(!function_exists("imagecreatefromgif")){  
  47.              echo "你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!<a href='javascript:go(-1);'>返回</a>";  
  48.              exit();  
  49.             }  
  50.             $this->im = ImageCreateFromGIF($this->srcFile);  
  51.             break;  
  52.          case 2:  
  53.            if(!function_exists("imagecreatefromjpeg")){  
  54.             echo "你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!<a href='javascript:go(-1);'>返回</a>";  
  55.             exit();  
  56.            }  
  57.            $this->im = ImageCreateFromJpeg($this->srcFile);      
  58.            break;  
  59.          case 3:  
  60.            $this->im = ImageCreateFromPNG($this->srcFile);      
  61.            break;  
  62.        }  
  63.        $this->srcW=ImageSX($this->im);  
  64.        $this->srcH=ImageSY($this->im);   
  65.      }  
  66.        
  67.      //生成扭曲型缩图  
  68.      function Distortion($toFile,$toW,$toH)  
  69.      {  
  70.          $cImg=$this->CreatImage($this->im,$toW,$toH,0,0,0,0,$this->srcW,$this->srcH);  
  71.          return $this->EchoImage($cImg,$toFile);  
  72.          ImageDestroy($cImg);  
  73.      }  
  74.        
  75.      //生成按比例缩放的缩图  
  76.      function Prorate($toFile,$toW,$toH)  
  77.      {  
  78.          $toWH=$toW/$toH;  
  79.          $srcWH=$this->srcW/$this->srcH;  
  80.          if($toWH< =$srcWH)  
  81.          {  
  82.              $ftoW=$toW;  
  83.              $ftoH=$ftoW*($this->srcH/$this->srcW);  
  84.          }  
  85.       else          {  
  86.                $ftoH=$toH;  
  87.                $ftoW=$ftoH*($this->srcW/$this->srcH);  
  88.          }  
  89.          if($this->srcW>$toW||$this->srcH>$toH)  
  90.          {  
  91.              $cImg=$this->CreatImage($this->im,$ftoW,$ftoH,0,0,0,0,$this->srcW,$this->srcH);  
  92.              return $this->EchoImage($cImg,$toFile);  
  93.              ImageDestroy($cImg);  
  94.          }  
  95.          else  
  96.          {  
  97.              $cImg=$this->CreatImage($this->im,$this->srcW,$this->srcH,0,0,0,0,$this->srcW,$this->srcH);  
  98.              return $this->EchoImage($cImg,$toFile);  
  99.              ImageDestroy($cImg);  
  100.          }  
  101.      }  
  102.        
  103.      //生成最小裁剪后的缩图  
  104.      function Cut($toFile,$toW,$toH)  
  105.      {  
  106.            $toWH=$toW/$toH;  
  107.            $srcWH=$this->srcW/$this->srcH;  
  108.            if($toWH< =$srcWH)  
  109.            {  
  110.                 $ctoH=$toH;  
  111.                 $ctoW=$ctoH*($this->srcW/$this->srcH);  
  112.            }  
  113.            else  
  114.            {  
  115.                $ctoW=$toW;  
  116.                $ctoH=$ctoW*($this->srcH/$this->srcW);  
  117.            }   
  118.          $allImg=$this->CreatImage($this->im,$ctoW,$ctoH,0,0,0,0,$this->srcW,$this->srcH);  
  119.          $cImg=$this->CreatImage($allImg,$toW,$toH,0,0,($ctoW-$toW)/2,($ctoH-$toH)/2,$toW,$toH);  
  120.          return $this->EchoImage($cImg,$toFile);  
  121.          ImageDestroy($cImg);  
  122.          ImageDestroy($allImg);  
  123.      }  
  124.     
  125.      //生成背景填充的缩图  
  126.      function BackFill($toFile,$toW,$toH,$bk1=255,$bk2=255,$bk3=255)  
  127.      {  
  128.          $toWH=$toW/$toH;  
  129.          $srcWH=$this->srcW/$this->srcH;  
  130.          if($toWH< =$srcWH)  
  131.          {  
  132.              $ftoW=$toW;  
  133.              $ftoH=$ftoW*($this->srcH/$this->srcW);  
  134.          }  
  135.          else  
  136.          {  
  137.                $ftoH=$toH;  
  138.                $ftoW=$ftoH*($this->srcW/$this->srcH);  
  139.          }  
  140.          if(function_exists("imagecreatetruecolor"))  
  141.          {  
  142.              @$cImg=ImageCreateTrueColor($toW,$toH);  
  143.              if(!$cImg)  
  144.              {  
  145.                  $cImg=ImageCreate($toW,$toH);  
  146.              }  
  147.          }  
  148.          else  
  149.          {  
  150.              $cImg=ImageCreate($toW,$toH);  
  151.          }  
  152.          $backcolor = imagecolorallocate($cImg$bk1$bk2$bk3);        //填充的背景颜色  
  153.          ImageFilledRectangle($cImg,0,0,$toW,$toH,$backcolor);  
  154.          if($this->srcW>$toW||$this->srcH>$toH)  
  155.          {       
  156.              $proImg=$this->CreatImage($this->im,$ftoW,$ftoH,0,0,0,0,$this->srcW,$this->srcH);  
  157.              /*  
  158.               if($ftoW< $toW)  
  159.               {  
  160.                   ImageCopyMerge($cImg,$proImg,($toW-$ftoW)/2,0,0,0,$ftoW,$ftoH,100);  
  161.               }  
  162.               else if($ftoH<$toH)  
  163.               {  
  164.                   ImageCopyMerge($cImg,$proImg,0,($toH-$ftoH)/2,0,0,$ftoW,$ftoH,100);  
  165.               }  
  166.               */  
  167.              if($ftoW<$toW)  
  168.              {  
  169.                   ImageCopy($cImg,$proImg,($toW-$ftoW)/2,0,0,0,$ftoW,$ftoH);  
  170.              }  
  171.              else if($ftoH<$toH)  
  172.              {  
  173.                   ImageCopy($cImg,$proImg,0,($toH-$ftoH)/2,0,0,$ftoW,$ftoH);  
  174.              }  
  175.              else  
  176.              {  
  177.                   ImageCopy($cImg,$proImg,0,0,0,0,$ftoW,$ftoH);  
  178.              }   
  179.          }  
  180.          else  
  181.          {  
  182.               ImageCopyMerge($cImg,$this->im,($toW-$ftoW)/2,($toH-$ftoH)/2,0,0,$ftoW,$ftoH,100);  
  183.          }  
  184.          return $this->EchoImage($cImg,$toFile);  
  185.          ImageDestroy($cImg);  
  186.      }  
  187.        
  188.     
  189.      function CreatImage($img,$creatW,$creatH,$dstX,$dstY,$srcX,$srcY,$srcImgW,$srcImgH)  
  190.      {  
  191.          if(function_exists("imagecreatetruecolor"))  
  192.          {  
  193.              @$creatImg = ImageCreateTrueColor($creatW,$creatH);              if($creatImg)   
  194.                  ImageCopyResampled($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);  
  195.              else  
  196.              {  
  197.                  $creatImg=ImageCreate($creatW,$creatH);  
  198.                  ImageCopyResized($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);  
  199.              }  
  200.           }  
  201.           else  
  202.           {  
  203.              $creatImg=ImageCreate($creatW,$creatH);  
  204.              ImageCopyResized($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);  
  205.           }  
  206.           return $creatImg;  
  207.      }  
  208.        
  209.      //输出图片,link---只输出,不保存文件。file--保存为文件  
  210.      function EchoImage($img,$to_File)  
  211.      {  
  212.          switch($this->echoType)          {  
  213.              case "link":  
  214.                  if(function_exists('imagejpeg')) return ImageJpeg($img);  
  215.                  else return ImagePNG($img);  
  216.                  break;  
  217.              case "file":  
  218.                  if(function_exists('imagejpeg')) return ImageJpeg($img,$to_File);  
  219.                  else return ImagePNG($img,$to_File);  
  220.                  break;  
  221.          }  
  222.      }  
  223.     
  224.  }  
  225.  ?> 

Tags: PHP智能图片 缩略图类

分享到: