php 上传文件并生成缩略图

<?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;
	}



}

 目前不支持目录的自定义。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_最初の心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值