您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP智能把图片生成缩略图类_PHP教程

2024/4/15 14:53:25发布18次查看
一个php智能把图片生成缩略图类,可自动根据你图片的大小生成等比例的图片哦,有需要的朋友可参考参考。
 代码如下 复制代码
/***************************************  *作者:落梦天蝎(beluckly)
 *完成时间:2006-12-18
 *类名:creatminiature
 *功能:生成多种类型的缩略图
 *基本参数:$srcfile,$echotype
 *方法用到的参数:
                 $tofile,生成的文件
                 $tow,生成的宽
                 $toh,生成的高
                 $bk1,背景颜色参数 以255为最高
                 $bk2,背景颜色参数
                 $bk3,背景颜色参数
*例子:
include(thumb.php);
     $cm=new creatminiature();
     $cm->setvar(1.jpg,file);
     $cm->distortion(dis_bei.jpg,150,200);
     $cm->prorate(pro_bei.jpg,150,200);
     $cm->cut(cut_bei.jpg,150,200);
      $cm->backfill(fill_bei.jpg,150,200);
***************************************/
class creatminiature
 {
     //公共变量
     var $srcfile=;        //原图
     var $echotype;            //输出图片类型,link--不保存为文件;file--保存为文件
     var $im=;                //临时变量
     var $srcw=;            //原图宽
     var $srch=;            //原图高
//设置变量及初始化
     function setvar($srcfile,$echotype)
     {
         $this->srcfile=$srcfile;
         $this->echotype=$echotype;
$info = ;
         $data = getimagesize($this->srcfile,$info);
         switch ($data[2])          {
          case 1:
            if(!function_exists(imagecreatefromgif)){
             echo 你的gd库不能使用gif格式的图片,请使用jpeg或png格式!返回;
             exit();
            }
            $this->im = imagecreatefromgif($this->srcfile);
            break;
         case 2:
           if(!function_exists(imagecreatefromjpeg)){
            echo 你的gd库不能使用jpeg格式的图片,请使用其它格式的图片!返回;
            exit();
           }
           $this->im = imagecreatefromjpeg($this->srcfile);    
           break;
         case 3:
           $this->im = imagecreatefrompng($this->srcfile);    
           break;
       }
       $this->srcw=imagesx($this->im);
       $this->srch=imagesy($this->im); 
     }
//生成扭曲型缩图
     function distortion($tofile,$tow,$toh)
     {
         $cimg=$this->creatimage($this->im,$tow,$toh,0,0,0,0,$this->srcw,$this->srch);
         return $this->echoimage($cimg,$tofile);
         imagedestroy($cimg);
     }
//生成按比例缩放的缩图
     function prorate($tofile,$tow,$toh)
     {
         $towh=$tow/$toh;
         $srcwh=$this->srcw/$this->srch;
         if($towh         {
             $ftow=$tow;
             $ftoh=$ftow*($this->srch/$this->srcw);
         }
      else          {
               $ftoh=$toh;
               $ftow=$ftoh*($this->srcw/$this->srch);
         }
         if($this->srcw>$tow||$this->srch>$toh)
         {
             $cimg=$this->creatimage($this->im,$ftow,$ftoh,0,0,0,0,$this->srcw,$this->srch);
             return $this->echoimage($cimg,$tofile);
             imagedestroy($cimg);
         }
         else
         {
             $cimg=$this->creatimage($this->im,$this->srcw,$this->srch,0,0,0,0,$this->srcw,$this->srch);
             return $this->echoimage($cimg,$tofile);
             imagedestroy($cimg);
         }
     }
//生成最小裁剪后的缩图
     function cut($tofile,$tow,$toh)
     {
           $towh=$tow/$toh;
           $srcwh=$this->srcw/$this->srch;
           if($towh           {
                $ctoh=$toh;
                $ctow=$ctoh*($this->srcw/$this->srch);
           }
           else
           {
               $ctow=$tow;
               $ctoh=$ctow*($this->srch/$this->srcw);
           } 
         $allimg=$this->creatimage($this->im,$ctow,$ctoh,0,0,0,0,$this->srcw,$this->srch);
         $cimg=$this->creatimage($allimg,$tow,$toh,0,0,($ctow-$tow)/2,($ctoh-$toh)/2,$tow,$toh);
         return $this->echoimage($cimg,$tofile);
         imagedestroy($cimg);
         imagedestroy($allimg);
     }
//生成背景填充的缩图
     function backfill($tofile,$tow,$toh,$bk1=255,$bk2=255,$bk3=255)
     {
         $towh=$tow/$toh;
         $srcwh=$this->srcw/$this->srch;
         if($towh         {
             $ftow=$tow;
             $ftoh=$ftow*($this->srch/$this->srcw);
         }
         else
         {
               $ftoh=$toh;
               $ftow=$ftoh*($this->srcw/$this->srch);
         }
         if(function_exists(imagecreatetruecolor))
         {
             @$cimg=imagecreatetruecolor($tow,$toh);
             if(!$cimg)
             {
                 $cimg=imagecreate($tow,$toh);
             }
         }
         else
         {
             $cimg=imagecreate($tow,$toh);
         }
         $backcolor = imagecolorallocate($cimg, $bk1, $bk2, $bk3);        //填充的背景颜色
         imagefilledrectangle($cimg,0,0,$tow,$toh,$backcolor);
         if($this->srcw>$tow||$this->srch>$toh)
         {     
             $proimg=$this->creatimage($this->im,$ftow,$ftoh,0,0,0,0,$this->srcw,$this->srch);
             /*
              if($ftow              {
                  imagecopymerge($cimg,$proimg,($tow-$ftow)/2,0,0,0,$ftow,$ftoh,100);
              }
              else if($ftoh              {
                  imagecopymerge($cimg,$proimg,0,($toh-$ftoh)/2,0,0,$ftow,$ftoh,100);
              }
              */
             if($ftow             {
                  imagecopy($cimg,$proimg,($tow-$ftow)/2,0,0,0,$ftow,$ftoh);
             }
             else if($ftoh             {
                  imagecopy($cimg,$proimg,0,($toh-$ftoh)/2,0,0,$ftow,$ftoh);
             }
             else
             {
                  imagecopy($cimg,$proimg,0,0,0,0,$ftow,$ftoh);
             } 
         }
         else
         {
              imagecopymerge($cimg,$this->im,($tow-$ftow)/2,($toh-$ftoh)/2,0,0,$ftow,$ftoh,100);
         }
         return $this->echoimage($cimg,$tofile);
         imagedestroy($cimg);
     }
function creatimage($img,$creatw,$creath,$dstx,$dsty,$srcx,$srcy,$srcimgw,$srcimgh)
     {
         if(function_exists(imagecreatetruecolor))
         {
             @$creatimg = imagecreatetruecolor($creatw,$creath);              if($creatimg) 
                 imagecopyresampled($creatimg,$img,$dstx,$dsty,$srcx,$srcy,$creatw,$creath,$srcimgw,$srcimgh);
             else
             {
                 $creatimg=imagecreate($creatw,$creath);
                 imagecopyresized($creatimg,$img,$dstx,$dsty,$srcx,$srcy,$creatw,$creath,$srcimgw,$srcimgh);
             }
          }
          else
          {
             $creatimg=imagecreate($creatw,$creath);
             imagecopyresized($creatimg,$img,$dstx,$dsty,$srcx,$srcy,$creatw,$creath,$srcimgw,$srcimgh);
          }
          return $creatimg;
     }
//输出图片,link---只输出,不保存文件。file--保存为文件
     function echoimage($img,$to_file)
     {
         switch($this->echotype)          {
             case link:
                 if(function_exists('imagejpeg')) return imagejpeg($img);
                 else return imagepng($img);
                 break;
             case file:
                 if(function_exists('imagejpeg')) return imagejpeg($img,$to_file);
                 else return imagepng($img,$to_file);
                 break;
         }
     }
}
 ?>
http://www.bkjia.com/phpjc/632961.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/632961.htmltecharticle一个php智能把图片生成缩略图类,可自动根据你图片的大小生成等比例的图片哦,有需要的朋友可参考参考。 代码如下 复制代码 ?php /******...
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product