您好,欢迎访问一九零五行业门户网

PHP 生成缩略图的类

php代码
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
{  
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;  
   }  
   }  
}  
?>
其它类似信息

推荐信息