增加了裁图工具

This commit is contained in:
Li Jianxuan 2014-01-03 02:22:45 +00:00
parent f6b1acc273
commit e2f2b2ae71
1 changed files with 217 additions and 146 deletions

View File

@ -9,153 +9,224 @@ namespace Files;
*/ */
class Thumbnail { class Thumbnail {
private $maxWidth; private $maxWidth;
private $maxHeight; private $maxHeight;
private $scale; private $scale;
private $inflate; private $inflate;
private $types; private $types;
private $imgLoaders; private $imgLoaders;
private $imgCreators; private $imgCreators;
private $source; private $source;
private $sourceWidth; private $sourceWidth;
private $sourceHeight; private $sourceHeight;
private $sourceMime; private $sourceMime;
private $thumb; private $thumb;
private $thumbWidth; private $thumbWidth;
private $thumbHeight; private $thumbHeight;
public function __construct($maxWidth, $maxHeight, $scale = true, $inflate = false) { public function __construct($maxWidth, $maxHeight, $scale = true, $inflate = false) {
$this->maxWidth = $maxWidth; $this->maxWidth = $maxWidth;
$this->maxHeight = $maxHeight; $this->maxHeight = $maxHeight;
$this->scale = $scale; $this->scale = $scale;
$this->inflate = $inflate; $this->inflate = $inflate;
$this->types = array( $this->types = array(
'image/jpeg', 'image/jpeg',
'image/png', 'image/png',
'image/gif' 'image/gif'
); );
//加载MIME类型图像的函数名称 //加载MIME类型图像的函数名称
$this->imgLoaders = array( $this->imgLoaders = array(
'image/jpeg' => 'imagecreatefromjpeg', 'image/jpeg' => 'imagecreatefromjpeg',
'image/png' => 'imagecreatefrompng', 'image/png' => 'imagecreatefrompng',
'image/gif' => 'imagecreatefromgif' 'image/gif' => 'imagecreatefromgif'
); );
//储存创建MIME类型图片的函数名称 //储存创建MIME类型图片的函数名称
$this->imgCreators = array( $this->imgCreators = array(
'image/jpeg' => 'imagejpeg', 'image/jpeg' => 'imagejpeg',
'image/png' => 'imagepng', 'image/png' => 'imagepng',
'image/gif' => 'imagegif' 'image/gif' => 'imagegif'
); );
} }
/**
* 文件方式加载图片 /**
* @param string $image 源图片 * 文件方式加载图片
* @return bool * @param string $image 源图片
*/ * @return bool
public function loadFile($image){ */
if(!$dims = @getimagesize($image)){ public function loadFile($image){
trigger_error("源图片不存在"); if(!$dims = @getimagesize($image)){
} trigger_error("源图片不存在");
if(in_array($dims['mime'], $this->types)){ }
$loader = $this->imgLoaders[$dims['mime']]; if(in_array($dims['mime'], $this->types)){
$this->source = $loader($image); $loader = $this->imgLoaders[$dims['mime']];
if($dims['mime'] == 'image/png' || $dims['mime'] == 'image/gif'){ $this->source = $loader($image);
imagesavealpha($this->source, true); if($dims['mime'] == 'image/png' || $dims['mime'] == 'image/gif'){
} imagesavealpha($this->source, true);
$this->sourceWidth = $dims[0];
$this->sourceHeight = $dims[1];
$this->sourceMime = $dims['mime'];
$this->initThumb();
return TRUE;
}else{
trigger_error('不支持'.$dims['mime']."图片类型");
}
}
/**
* 字符串方式加载图片
* @param string $image 字符串
* @param string $mime 图片类型
* @return type
*/
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{
trigger_error("不能从字符串加载图片");
}
}else{
trigger_error("不支持".$mime."图片格式");
}
}
/**
* 生成缩略图
* @param string $file 文件名。如果不为空则储存为文件,否则直接输出到浏览器
*/
public function buildThumb($file = NULL){
$creator = $this->imgCreators[$this->sourceMime];
if(isset($file) && $this->thumb !== NULL){
return $creator($this->thumb,$file);
}else{
return false;
}
}
/**
* 处理缩放
*/
public function initThumb(){
if($this->scale){
if($this->sourceWidth > $this->sourceHeight){
$this->thumbWidth = $this->maxWidth;
$this->thumbHeight = floor($this->sourceHeight*($this->maxWidth/$this->sourceWidth));
}elseif($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;
}
}
if($this->sourceWidth <= $this->maxWidth && $this->sourceHeight <= $this->maxHeight && $this->inflate == FALSE){
$this->thumb = NULL;
}else{
$this->thumb = imagecreatetruecolor($this->thumbWidth, $this->thumbHeight);
if($this->sourceMime == 'image/png' || $this->sourceMime == 'image/gif')
{
imagealphablending($this->thumb, true);
imagesavealpha($this->thumb, true);
if($this->sourceMime == 'image/gif')
{
$bgcolor=imagecolorallocate($this->thumb,0,0,0);
$transparent = imagecolortransparent($this->thumb,$bgcolor) ;
}
if($this->sourceMime == 'image/png')
{
$transparent = imagecolorallocatealpha($this->thumb, 0, 0, 0, 127);
}
imagefill($this->thumb, 0, 0, $transparent);
} }
imagecopyresampled($this->thumb, $this->source, 0, 0, 0, 0, $this->thumbWidth, $this->thumbHeight, $this->sourceWidth, $this->sourceHeight); $this->sourceWidth = $dims[0];
} $this->sourceHeight = $dims[1];
} $this->sourceMime = $dims['mime'];
$this->initThumb();
public function getMine(){ return TRUE;
return $this->sourceMime; }else{
} trigger_error('不支持'.$dims['mime']."图片类型");
}
public function getThumbWidth(){ }
return $this->thumbWidth;
} /**
* 字符串方式加载图片
public function getThumbHeight(){ * @param string $image 字符串
return $this->thumbHeight; * @param string $mime 图片类型
} * @return type
*/
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{
trigger_error("不能从字符串加载图片");
}
}else{
trigger_error("不支持".$mime."图片格式");
}
}
/**
* 生成缩略图
* @param string $file 文件名。如果不为空则储存为文件,否则直接输出到浏览器
*/
public function buildThumb($file = NULL){
$creator = $this->imgCreators[$this->sourceMime];
if(isset($file) && $this->thumb !== NULL){
return $creator($this->thumb,$file);
}else{
return false;
}
}
/**
* 处理缩放
*/
public function initThumb(){
if($this->scale){
if($this->sourceWidth > $this->sourceHeight){
$this->thumbWidth = $this->maxWidth;
$this->thumbHeight = floor($this->sourceHeight*($this->maxWidth/$this->sourceWidth));
}elseif($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;
}
}
if($this->sourceWidth <= $this->maxWidth && $this->sourceHeight <= $this->maxHeight && $this->inflate == FALSE){
$this->thumb = NULL;
}else{
$this->thumb = imagecreatetruecolor($this->thumbWidth, $this->thumbHeight);
if($this->sourceMime == 'image/png' || $this->sourceMime == 'image/gif')
{
imagealphablending($this->thumb, true);
imagesavealpha($this->thumb, true);
if($this->sourceMime == 'image/gif')
{
$bgcolor=imagecolorallocate($this->thumb,0,0,0);
$transparent = imagecolortransparent($this->thumb,$bgcolor) ;
}
if($this->sourceMime == 'image/png')
{
$transparent = imagecolorallocatealpha($this->thumb, 0, 0, 0, 127);
}
imagefill($this->thumb, 0, 0, $transparent);
}
imagecopyresampled($this->thumb, $this->source, 0, 0, 0, 0, $this->thumbWidth, $this->thumbHeight, $this->sourceWidth, $this->sourceHeight);
}
}
public function getMine(){
return $this->sourceMime;
}
public function getThumbWidth(){
return $this->thumbWidth;
}
public function getThumbHeight(){
return $this->thumbHeight;
}
static function cut($source_path, $target_width, $target_height){
$source_info = getimagesize($source_path);
$source_width = $source_info[0];
$source_height = $source_info[1];
$source_mime = $source_info['mime'];
$source_ratio = $source_height / $source_width;
$target_ratio = $target_height / $target_width;
// 源图过高
if ($source_ratio > $target_ratio)
{
$cropped_width = $source_width;
$cropped_height = $source_width * $target_ratio;
$source_x = 0;
$source_y = ($source_height - $cropped_height) / 2;
}
// 源图过宽
elseif ($source_ratio < $target_ratio)
{
$cropped_width = $source_height / $target_ratio;
$cropped_height = $source_height;
$source_x = ($source_width - $cropped_width) / 2;
$source_y = 0;
}
// 源图适中
else
{
$cropped_width = $source_width;
$cropped_height = $source_height;
$source_x = 0;
$source_y = 0;
}
switch ($source_mime)
{
case 'image/gif':
$source_image = imagecreatefromgif($source_path);
break;
case 'image/jpeg':
$source_image = imagecreatefromjpeg($source_path);
break;
case 'image/png':
$source_image = imagecreatefrompng($source_path);
break;
default:
return false;
break;
}
$target_image = imagecreatetruecolor($target_width, $target_height);
$cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);
// 裁剪
imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
// 缩放
imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);
header('Content-Type: image/jpeg');
imagejpeg($target_image);
imagedestroy($source_image);
imagedestroy($target_image);
imagedestroy($cropped_image);
}//cute
} }