namespace Org\Util;
class ThumbImage{
/**
* 图片信息
*/
private $info;
/**
* 内存中的图片
*/
private $image;
public function __construct($url){
$info = getimagesize($url);
$this->info = array(
'width' => $info['0'],
'height'=> $info['1'],
'type' => image_type_to_extension($info['2'],false),
'mime' => $info['mime'],
);
$fun = "imagecreatefrom{$this->info['type']}";
$this ->image = $fun($url);
}
public function ThumbImage($width,$height){
//创建缩略图
$thumbImage = imagecreatetruecolor($width, $height);
//把原图复制到缩略图上面
imagecopyresampled($thumbImage, $this->image, 0, 0, 0, 0, $width, $height, $this->info['width'], $this->info['height']);
//销毁内存中的原图
imagedestroy($this->image);
$this->image = $thumbImage;
}
//在浏览器中显示图片
public function showImage(){
header("content-type:".$this->info['mime']);
$fun = "image{$this->info['type']}";
$fun($this->image);
}
//保存图片到硬盘里
public function saveImage($newName){
$fun = "image{$this->info['type']}";
$fun($this->image,$newName.'.'.$this->info['type']);
}
//销毁图片
public function __destruct(){
imagedestroy($this->image);
}
}