class image{
private $ext = array('jpg','png'); //支持的扩展名
public $imgSrc = ''; //原图片地址
public $thumbWidth = 200;//缩略为最大宽度为200的图片
public $thumbHeight = '';//缩略图的高度
public $width = '';//原图宽度
public $height = '';//原图高度
public $bili = '';//缩放比例
public $dstImgSrc = 'thumb/';//复制目标图片地址
public $currentExt = '';//当前图片扩展名
/**
* 判断文件是否符合上传类型,是否存在此文件,并且初始换文件信息
* @param string $path 图片的路径
*/
public function __construct($path){
if(is_file($path)){
$this->currentExt = substr($path,strpos($path,'.')+1);
if(!in_array($this->currentExt,$this->ext)){
die('不支持此扩展名!');
}
$this->imgSrc = $path;
$this->init();
} else {
die('不是一个文件!');
}
}
/**
* 初始换文件信息,获取文件宽高,生成的文件名,要缩放的比例
* return void
*/
public function init(){
$info = getimagesize($this->imgSrc);
$this->width = $info[0];
$this->height = $info[1];
$this->dstImgSrc .= $this->_getRoundName();
if($this->width <= $this->thumbWidth){
copy($this->imgSrc,$this->dstImgSrc);
return;
}
$this->bili = round($this->thumbWidth/$this->width,2);
$this->thumbHeight = $this->height * $this->bili;
}
/**
* 创建并生成图片
* return void
*/
public function createImg(){
$canvas = imagecreatetruecolor($this->thumbWidth,$this->thumbHeight);
$image = imagecreatefromjpeg($this->imgSrc);
imagecopyresampled($canvas, $image, 0, 0, 0, 0, $this->thumbWidth,$this->thumbHeight, $this->width, $this->height);
header('Content-Type: image/jpeg');
ImageJpeg ($canvas,$this->dstImgSrc);
imagedestroy($canvas);
}
/**
* 随机生成文件名,并判断是否存在此目录
* return string 文件名称
*/
private function _getRoundName(){
$dir = date('Ym');
$name = $dir.'/'.md5(time()).'.'.$this->currentExt;
if(!is_dir($this->dstImgSrc.$dir)){
mkdir($this->dstImgSrc.$dir,0777,true);
}
if(!is_file($this->dstImgSrc.$name)){
return $name;
} else {
$this->_getRoundName();
}
}
}
$image = new image('abc.jpg');
$isTrue = $image->createImg();
private $ext = array('jpg','png'); //支持的扩展名
public $imgSrc = ''; //原图片地址
public $thumbWidth = 200;//缩略为最大宽度为200的图片
public $thumbHeight = '';//缩略图的高度
public $width = '';//原图宽度
public $height = '';//原图高度
public $bili = '';//缩放比例
public $dstImgSrc = 'thumb/';//复制目标图片地址
public $currentExt = '';//当前图片扩展名
/**
* 判断文件是否符合上传类型,是否存在此文件,并且初始换文件信息
* @param string $path 图片的路径
*/
public function __construct($path){
if(is_file($path)){
$this->currentExt = substr($path,strpos($path,'.')+1);
if(!in_array($this->currentExt,$this->ext)){
die('不支持此扩展名!');
}
$this->imgSrc = $path;
$this->init();
} else {
die('不是一个文件!');
}
}
/**
* 初始换文件信息,获取文件宽高,生成的文件名,要缩放的比例
* return void
*/
public function init(){
$info = getimagesize($this->imgSrc);
$this->width = $info[0];
$this->height = $info[1];
$this->dstImgSrc .= $this->_getRoundName();
if($this->width <= $this->thumbWidth){
copy($this->imgSrc,$this->dstImgSrc);
return;
}
$this->bili = round($this->thumbWidth/$this->width,2);
$this->thumbHeight = $this->height * $this->bili;
}
/**
* 创建并生成图片
* return void
*/
public function createImg(){
$canvas = imagecreatetruecolor($this->thumbWidth,$this->thumbHeight);
$image = imagecreatefromjpeg($this->imgSrc);
imagecopyresampled($canvas, $image, 0, 0, 0, 0, $this->thumbWidth,$this->thumbHeight, $this->width, $this->height);
header('Content-Type: image/jpeg');
ImageJpeg ($canvas,$this->dstImgSrc);
imagedestroy($canvas);
}
/**
* 随机生成文件名,并判断是否存在此目录
* return string 文件名称
*/
private function _getRoundName(){
$dir = date('Ym');
$name = $dir.'/'.md5(time()).'.'.$this->currentExt;
if(!is_dir($this->dstImgSrc.$dir)){
mkdir($this->dstImgSrc.$dir,0777,true);
}
if(!is_file($this->dstImgSrc.$name)){
return $name;
} else {
$this->_getRoundName();
}
}
}
$image = new image('abc.jpg');
$isTrue = $image->createImg();