欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php生成缩略图的类代码

程序员文章站 2022-03-08 14:11:39
<?php

/**
* 功能:生成缩略图
* 作者:phpox
* 日期:thu may 17 09:57:05 cst 2007
*/

class creatminiature
{
//公共变量
var $srcfile=""; //原图
var $echotype; //输出图片类型,link--不保存为文件;file--保存为文件
var $im=""; //临时变量
var $srcw=""; //原图宽
var $srch=""; //原图高

//设置变量及初始化
function setvar($srcfile,$echotype)
{
if (!file_exists($srcfile)){
echo '源图片文件不存在!';
exit();
}
$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格式!<a href='javascript:go(-1);'>返回</a>";
exit();
}
$this->im = imagecreatefromgif($this->srcfile);
break;
case 2:
if(!function_exists("imagecreatefromjpeg")){
echo "你的gd库不能使用jpeg格式的图片,请使用其它格式的图片!<a href='javascript:go(-1);'>返回</a>";
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<=$srcwh)
{
$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<=$srcwh)
{
$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<=$srcwh)
{
$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<$tow)
{
imagecopy($cimg,$proimg,($tow-$ftow)/2,0,0,0,$ftow,$ftoh);
}
else if($ftoh<$toh)
{
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;
}
}
}
?>