<?php
// 图片上传类
class UploadAction
{
//上传目录
private $dir;
// 主图的宽度
private $upImageWidth;
// 主图的高度
private $upImageHeight;
// 缩略图的宽度
private $thumWidth;
// 缩略图的高度
private $thumHeight;
// 缩略图保存地址
private $fileNamePathThum;
// 文件类型
private $upImageType;
/**
* @param isCreateThum 是否上传缩略图
* @param width 缩略图的宽度
* @param height 缩略图的高度 (auto宽度等比例)
* */
public function make($isCreateThum,$width,$height='auto')
{
// 处理图片资源为数组
$imgArr = $this->uploadImageArray();
if(!$imgArr){
echo '没有图片上传';die;
}
//生成目录
if(!$this->makeDir()){
echo '目录创建失败';die;
}
// 开始上传
foreach ($imgArr as $key => $value)
{
if(is_uploaded_file($value["tmp_name"])){
// 图片保存路径
$fileNamePath = $this->dir.'/'.time().mt_rand(1,999).'.'.pathinfo($value['name'])['extension'];
//图片信息
$ipImgInfo = getimagesize($value['tmp_name']);
//主图的宽、 高、 文件类型
$this->upImageWidth = $ipImgInfo[0];
$this->upImageheight = $ipImgInfo[1];
$this->upImageType = $ipImgInfo[2];
if($isCreateThum)
{
// 设置缩略的宽、高、保存目录
$this->thumWidth = $width;
if($height === 'auto'){
$this->thumHeight = ($width/$this->upImageWidth)*$this->upImageheight;
}else{
$this->thumHeight = $height;
}
// 设置缩略图的保存地址
$this->fileNamePathThum = $this->dir.'/thum_'.time().mt_rand(1,999).'.'.pathinfo($value['name'])['extension'];
// 生成缩略图
$thumStatus = $this->createThumImage($value['tmp_name']);
if(!$thumStatus){
echo '缩略图生成失败!';
}
}
// 处理上传
if(move_uploaded_file($value['tmp_name'], $fileNamePath)){
// 上传成功
echo '上传成功';
}
}
}
}
/**
* 创建画布,复制主图资源到画布。生成缩略资源
* @param upImageId 主图的资源
* @return thumImageID 缩略图资源
*
* */
private function setThumImage($upImageId)
{
//创建缩略图画布
$thumImageID = imagecreatetruecolor($this->thumWidth, $this->thumHeight);
// 主图复制到缩略图
$status = imagecopyresampled($thumImageID, $upImageId, 0, 0, 0, 0, $this->thumWidth, $this->thumHeight, $this->upImageWidth , $this->upImageheight );
if(!$status){
return false;
}
return $thumImageID;
}
/**
* 生成并保存缩略图
* */
private function createThumImage($upImage)
{
$status = false;
switch ($this->upImageType) {
case IMAGETYPE_JPEG:
// 获取资源 jpg
$upImageId = imagecreatefromjpeg($upImage);
//复制主图,创建资源
$thumImageID = $this->setThumImage($upImageId);
// 保存缩略图
$status = imagejpeg($thumImageID,$this->fileNamePathThum);
break;
case IMAGETYPE_PNG:
// 获取资源 png
$upImageId = imagecreatefrompng($upImage);
//复制主图,创建资源
$thumImageID = $this->setThumImage($upImageId);
// 保存缩略图
$status = imagepng($thumImageID,$this->fileNamePathThum);
break;
case IMAGETYPE_GIF:
// 获取资源 gif
$upImageId = imagecreatefromgif($upImage);
//复制主图,创建资源
$thumImageID = $this->setThumImage($upImageId);
// 保存缩略图
$status = imagegif($thumImageID,$this->fileNamePathThum);
break;
}
if(!$status){
return false;
}
return $status;
}
/**
* 创建上传目录
* */
public function makeDir():bool
{
$filePath = 'upload/'.date('Y').'/'.date('m');
$this->dir = $filePath;
return is_dir($filePath) or mkdir($filePath,0755,true);
}
/***
*
* 整合上传的图片
*
* */
private function uploadImageArray():array
{
$upIma = [];
foreach ($_FILES as $key => $value) {
if(is_array($value['name'])){
foreach($value['name'] as $k=>$v){
if($value['error'][$k] === 0){
// 整合上传的图片
$upIma[] = [
'name'=>$v,
'type'=>$value['type'][$k],
'tmp_name'=>$value['tmp_name'][$k],
'error'=>$value['error'][$k],
'size'=>$value['size'][$k],
];
}
}
}else{
// 整合上传的图片
if($value['error'] === 0){
$upIma[] = $value;
}
}
}
return $upIma;
}
}
目前不支持目录的自定义。