一个简单好用的缩略图类
这个缩略图生成类是根据从 PHPCMS 挖出来的相关函数简单修改而来的。自觉使用起来挺方便的,呵呵。
用法也简单,范例如下:
$thumb = new thumb(str_replace(“\\”,”/”,dirname(__FILE__)).”/”); \\定义根物理路径
$thumb->get(‘images/107.jpg’); \\输入图片路径生成缩略图并返回缩略图路径
以上写法也可以改成
$thumb = new thumb();
$thumb->get(‘E:/htdocs/magki/include/images/107.jpg’);
不过前一种返回的是缩略图的相对路径,而后一种是返回缩略图的绝对物理路径。
缩略图的命名只定义了三个标签如下:
%maxwidth% get 方法中所定义的缩略图的最大宽度,默认 100
%maxheight% get 方法中所定义的缩略图的最大高度,默认 100
%basename% 所要生成缩略图的原图文件名
类中默认使用的缩略图命名是 thumb_%maxwidth%_%maxheight%_%basename% 像上面范例中所生成的缩略图命名就是根据默认来生成的,所得到的缩略图文件名为 thumb_100_100_107.jpg。有需要的可以自己扩展更多的缩略图命名标签。
另外,这个类的 get 方法还有一个参数是 autocut,如果赋值为 1 (默认值)则如果原图高宽比例和所指定的缩略图高宽比例不一致则会进行裁剪而非变形拉伸。
class thumb{
var $basepath;
var $namerule;
var $defaultpic;
var $rules;/**
* 兼容 PHP4 的类构造函数
*
* @param string $basepath
* @param string $namerule
* @param string $defaultpic
*/
function thumb($basepath = ”, $namerule = ”, $defaultpic = ‘images/defaultpic.gif’)
{
return $this->__construct($basepath, $namerule, $defaultpic);
}/**
* 类构造函数
*
* @param string $basepath
* @param string $namerule
* @param string $defaultpic
*/
function __construct($basepath = ”, $namerule = ”, $defaultpic = ‘images/defaultpic.gif’)
{
$this->basepath = $basepath;
$this->namerule = ($namerule) ? $namerule : ‘thumb_%maxwidth%_%maxheight%_%basename%’ ;
$this->defaultpic = ($defaultpic) ? $defaultpic : ‘images/defaultpic.gif’ ;
preg_match_all(“/%([a-zA-Z0-9_]+)%/”,$this->namerule,$matches);
$this->rules = $matches[1];
}/**
* 缩略图输出函数
*
* @param string $imgpath 图片路径
* @param integer $maxwidth 缩略图最大宽度
* @param integer $maxheight 缩略图最大高度
* @param boolean $autocut 是否自动裁剪
* @param boolean $interlace 是否启用交叉模式
* @return string
*/
function get($imgpath, $maxwidth = 100, $maxheight = 100, $autocut = 1, $interlace = 0)
{
if(empty($imgpath)) return $this->defaultpic;
if(!file_exists($this->basepath.$imgpath)) return $this->defaultpic;
if(!extension_loaded(‘gd’) || strpos($imgpath, ‘://’)) return $imgpath;
list($width_t, $height_t, $type, $attr) = getimagesize($this->basepath.$imgpath);
if($maxwidth>=$width_t || $maxheight>=$height_t) return $imgpath;$basename = basename($imgpath);
$thumbname = $this->namerule;
foreach($this->rules as $rule){
$thumbname = str_replace(‘%’.$rule.’%’,$$rule,$thumbname);
}
$thumbpath = dirname($imgpath).’/’.$thumbname;
if(file_exists($this->basepath.$thumbpath)) return $thumbpath;
$image = $this->basepath.$imgpath;
$info = $this->info($image);
if($info === false) return false;
$srcwidth = $info[‘width’];
$srcheight = $info[‘height’];
$pathinfo = pathinfo($image);
$type = $pathinfo[‘extension’];
if(!$type) $type = strtolower($info[‘type’]);
unset($info);
$scale = min($maxwidth/$srcwidth, $maxheight/$srcheight);
$createwidth = $width = (int)($srcwidth*$scale);
$createheight = $height = (int)($srcheight*$scale);
if($maxwidth >= $srcwidth) $createwidth = $width = $srcwidth;
if($maxheight >= $srcheight) $createheight = $height = $srcheight;
$psrc_x = $psrc_y = 0;
if($autocut)
{
if($maxwidth/$maxheight<$srcwidth/$srcheight && $maxheight>=$height)
{
$width = $maxheight/$height*$width;
$height = $maxheight;
}
elseif($maxwidth/$maxheight>$srcwidth/$srcheight && $maxwidth>=$width)
{
$height = $maxwidth/$width*$height;
$width = $maxwidth;
}
$createwidth = $maxwidth;
$createheight = $maxheight;
}
$createfun = ‘imagecreatefrom’.($type==’jpg’ ? ‘jpeg’ : $type);
$srcimg = $createfun($image);
if($type != ‘gif’ && function_exists(‘imagecreatetruecolor’))
$thumbimg = imagecreatetruecolor($createwidth, $createheight);
else
$thumbimg = imagecreate($width, $height);if(function_exists(‘imagecopyresampled’))
imagecopyresampled($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);
else
imagecopyresized($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);
if($type==’gif’ || $type==’png’)
{
$background_color = imagecolorallocate($thumbimg, 0, 255, 0);
imagecolortransparent($thumbimg, $background_color);
}
if($type==’jpg’ || $type==’jpeg’) imageinterlace($thumbimg, $interlace);
$imagefun = ‘image’.($type==’jpg’ ? ‘jpeg’ : $type);
if(empty($thumbpath)) $thumbpath = substr($image, 0, strrpos($image, ‘.’)).$suffix.’.’.$type;
$imagefun($thumbimg, $thumbpath);
imagedestroy($thumbimg);
imagedestroy($srcimg);
return $thumbpath;
}/**
* 根据传递进来图片物理路径返回基本的图片属性
*
* @param string $img
* @return array
*/
function info($img)
{
$imageinfo = getimagesize($img);
if($imageinfo === false) return false;
$imagetype = strtolower(substr(image_type_to_extension($imageinfo[2]),1));
$imagesize = filesize($img);
return $info = array(
‘width’=>$imageinfo[0],
‘height’=>$imageinfo[1],
‘type’=>$imagetype,
‘size’=>$imagesize,
‘mime’=>$imageinfo[‘mime’]
);
}
}