<?php
/**
* 图片处理类
* @author Devil
* @version v_0.0.1
*/
class Images
{
private $path;
private $zoom;
/**
* [__construct 构造方法]
* @param [string] $path [图片存储路径]
* @param [int] $zoom [压缩程度]
*/
public function __construct($path = '', $zoom = 60)
{
// 参数校验
if(empty($path)) exit('param path no');
// 属性赋值
$this->path = $path;
$this->zoom = $zoom;
}
/**
* [ImagesReturn 数据返回]
* @param [mixed] $msg [错误信息/数据]
* @param [int] $code [状态]
* @return[array] [返回数据]
*/
private function ImagesReturn($msg = '', $code = 0)
{
return array('msg'=>$msg, 'code'=>$code);
}
/**
* [FormSave form图片资源存储]
* @param [string] $resources [图片临时资源]
* @param [string] $name [图片名称(可空)]
* @param [string] $path [自定义图片路径(可空)]
* @return[array] [图片名称]
*/
public function FormSave($resources, $name = '', $path = '')
{
// 参数校验
if(empty($resources['tmp_name']) || empty($resources['type'])) return $this->ImagesReturn('参数错误', -1);
// 文件名生成
$file_name = empty($name) ? $this->FileNewName() : $name;
// 图片后缀名
$suffix = $this->GetSuffix($resources['type']);
if(empty($suffix)) $this->ImagesReturn('图片后缀名有误', -2);
// 图片存储
$file_name = $file_name.'.'.$suffix;
$path = empty($path) ? $this->path : $path;
if(move_uploaded_file($resources['tmp_name'], $path.$file_name)) return $this->ImagesReturn($file_name);
// 返回错误
return $this->ImagesReturn('图片存储失败', -100);
}
/**
* [GetSuffix 获取图片的后缀名]
* @param [string] $type [图片资源类型]
* @return[string] [后缀名]
*/
private function GetSuffix($type)
{
$img_all = array(
'jpg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif');
$key = array_search($type, $img_all);
if($key === false) return '';
return $key;
}
/**
* [FileNewName 生成文件名]
*/
private function FileNewName()
{
$name = date('YmdHis');
for($i=0; $i<6; $i++) $name .= rand(0, 9);
return $name;
}
/**
* [Compression 图片缩放存储]
* @param [string] $images [图片资源]
* @param [string] $name [图片名称(可空)]
* @param [string] $path [自定义图片路径(可空)]
* @param [int] $w [宽度(可空)]
* @param [int] $h [高度(默认原始尺寸或以宽度自动计算)]
* @param [int] $zoom [压缩值(默认60)]
* @return[array] [图片信息]
*/
public function Compression($images, $name = '', $path = '', $w = 0, $h = 0, $zoom = 0)
{
// 参数校验
if(empty($images)) return $this->ImagesReturn('参数错误', -1);
// 获取图片信息
$info = @getimagesize($images);
if($info == false) return $this->ImagesReturn('图片有误', -2);
// 文件名生成
$file_name = empty($name) ? $this->FileNewName() : $name;
// 图片后缀名
//$suffix = $this->GetSuffix($info['mime']);
$suffix = strrchr($images, '.');
if(empty($suffix)) return $this->ImagesReturn('图片后缀名有误', -3);
// 取得尺寸的比例值
$proportion = empty($w) ? 0 : $w/$info[0];
// 新的宽度
$new_width = empty($w) ? $info[0] : $w;
// 如果没有自定义高度则根据宽度计算高度
$new_height = empty($h) ? (($w < $info[0] && !empty($proportion)) ? intval($proportion*$info[1]) : (($w > $info[0] && !empty($proportion)) ? intval($proportion*$info[1]) : $info[1])) : $h;
// 新建一个彩色图像
$new_img = imagecreatetruecolor($new_width, $new_height);
// 图片资源
$img = $this->ImageFrom($images, $new_img, $info['mime']);
if(!$img) return $this->ImagesReturn('图片资源获取失败', -4);
// 缩放图片
imagecopyresampled($new_img, $img, 0, 0, 0, 0, $new_width, $new_height, $info[0], $info[1]);
// 缩放程度
$zoom = empty($zoom) ? (empty($this->zoom) ? 100 : $this->zoom) : $zoom;
// 存储图片
$file_name = $file_name.$suffix;
$path = empty($path) ? $this->path : $path;
switch($info['mime'])
{
case 'image/png':
$state = imagepng($new_img, $path.$file_name);
break;
case 'image/gif':
$state = imagegif($new_img, $path.$file_name);
break;
default:
$state = imagejpeg($new_img, $path.$file_name, $zoom);
}
// 释放内容
imagedestroy($img);
imagedestroy($new_img);
// 返回
if($state) return $this->ImagesReturn($file_name);
return $this->ImagesReturn($file_name);
$this->ImagesReturn('文件储存失败', -100);
}
/**
* [ImageFrom 图片资源获取]
* @param [string] $images [原图片资源]
* @param [string] $new_img[新的图片资源]
* @param [string] $type [图片类型]
* @return[mixed] [成功返回图象资源,失败返回 false]
*/
private function ImageFrom($images, $new_img, $type)
{
// 参数校验
if(empty($images) || empty($type)) return false;
// 图片资源获取
switch($type)
{
case 'image/png':
$img = imagecreatefrompng($images);
// png保留透明背景
imagealphablending($new_img, false);
imagesavealpha($new_img, true);
imagesavealpha($img, true);
break;
case 'image/gif':
$img = imagecreatefromgif($images);
break;
default:
case 'image/jpeg':
$img = imagecreatefromjpeg($images);
}
return $img;
}
}
?>