以下代码摘自《PHP深度分析:101个核心技巧、窍门和问题解决方法》 第八章
Thumbnail.class.php
class ThumbnailException extends Exception
{
public function __construct($message = null, $code = 0)
{
parent::__construct($message, $code);
error_log('Error in '. $this->getFile().
' Line: '.$this->getLine().
' Error: '.$this->getMessage());
}
}
class ThumbnailFileException extends ThumbnailException {}
class ThumbnailNotSupportedException extends ThumbnailException {}
class Thumbnail
{
private $maxWidth;
private $maxHeight;
private $scale;
private $inflate;
private $types;
private $imgLoaders;
private $imgCreators;
private $source;
private $sourceWidth;
private $sourceHeight;
private $sourceMime;
private $thumb;
private $thumbWidth;
private $thumbHeight;
public function __construct($maxWidth, $maxHeight, $scale = true, $inflate = true)
{
$this->maxWidth = $maxWidth;
$this->maxHeight = $maxHeight;
$this->scale = $scale;
$this->inflate = $inflate;
$this->types = array('image/jpeg', 'image/png', 'image/gif');
$this->imgLoaders = array(
'image/jpeg' => 'imagecreatefromjpeg',
'image/png' => 'imagecreatefrompng',
'image/gif' => 'imagecreatefromgif'
);
$this->imgCreators = array(
'image/jpeg' => 'imagejpeg',
'image/png' => 'imagepng',
'image/gif' => 'imagegif'
);
}
// load a image from path
public function loadFile($image)
{
if (!$dims = @getimagesize($image)) {
throw new ThumbnailFileException("Could not find image: ".$image);
}
if (in_array($dims['mime'], $this->types)) {
$loader = $this->imgLoaders[$dims['mime']];
$this->source = $loader($image);
$this->sourceWidth =$dims[0];
$this->sourceHeight = $dims[1];
$this->sourceMime = $dims['mime'];
$this->initThumb();
return true;
}
else {
throw new ThumbnailNotSupportedException(
'Image MIME type ').$dims['mime'].' not supported';
}
}
// Load a image from binary string
public function loadData($image, $mime)
{
if( in_array($mime, $this->types)) {
if($this->source = @imagecreatefromstring($image)) {
$this->sourceWidth = imagesx($this->source);
$this->sourceHeight = imagesy($this->source);
$this->sourceMime = $mime;
$this->initThumb();
return true;
}else {
throw new ThumbnailFileException('Could not load image from string');
}
} else {
throw new ThumbnailNotSupportedException(
'Image MIME type '.$mime.' not supported');
}
}
// Create the thumb and save to the file or return
public function buildThumb($file = null)
{
$creator = $this->imgCreators[$this->sourceMime];
if (isset($file)) {
return $creator($this->thumb, $file);
} else {
return $creator($this->thumb);
}
}
public function getMime()
{
return $this->sourceMime;
}
public function getThumbWidth()
{
return $this->thumbWidth;
}
public function getThumbHeight()
{
return $this->thumbHeight;
}
private function initThumb()
{
if ($this->scale)//等比例缩放图片
{
if ($this->sourceWidth > $this->sourceHeight)
{
$this->thumbWidth = $this->maxWidth;
$this->thumbHeight = floor($this->sourceHeight * ($this->maxWidth / $this->sourceWidth));
}
else if ($this->sourceWidth < $this->sourceHeight) {
$this->thumbHeight = $this->maxHeight;
$this->thumbWidth = floor($this->sourceWidth * ($this->maxHeight / $this->sourceHeight));
} else {
$this->thumbWidth = $this->maxWidth;
$this->thumbHeight = $this->maxHeight;
}
$this->thumb = imagecreatetruecolor($this->thumbWidth, $this->thumbHeight);
}
if($this->sourceWidth <= $this->maxWidth && $this->sourceHeight <= $this->maxHeight
&& $this->inflate == false) //是否按需放大图片
{
$this->thumb = $this->source;
}else {
imagecopyresampled($this->thumb, $this->source, 0, 0, 0, 0,
$this->thumbWidth, $this->thumbHeight, $this->sourceWidth, $this->sourceHeight);
}
}
}
?>
说明:
从构造函数__construct($maxWidth, $maxHeight, $scale = true, $inflate = true)得知,至少需要传入两个参数$maxWidth 和 $maxHeight, 分别代表缩略图的宽高,
$scale代表是否按照原图片等比例缩放,默认为true, 实际生成的缩略图的宽高不一定全部等于$maxWidth和$maxHeight,可能只有一个达到,而另一个小于。
$inflate代表当原图过小而要生成的缩略图大的时候是否要进行填充放大,默认为true。 当为false的时候,意味着缩略图和原图一样大小。
使用如下:
1. 获取本地或远程图片路径,创建200x200像素的图片,并输出到网页上:
require_once('Thumbnail.class.php');
$tn = new Thumbnail(200, 200);
// $tn ->loadFile('test.jpg');//本地图片
$tn ->loadFile('http://7.su.bdimg.com/skin/65.jpg');//网络图片
header('Content-Type: '.$tn->getMime());//输出图片到页面
$tn->buildThumb();
?>2.获取图片内容(本地、网络、或者数据库),创建缩略图并保存到本地图片,然后在页面上以标签来引用显示:
require_once('Thumbnail.class.php');
$tn = new Thumbnail(200, 200);
$image = file_get_contents('test.jpg');
$tn->loadData($image, 'image/jpeg');
$tn->buildThumb('test_200.jpg');
echo "";
?>