为了账号安全,请及时绑定邮箱和手机立即绑定

求实现图片上传后自动生成多张不同尺寸的图片的实现思路谢谢了

求实现图片上传后自动生成多张不同尺寸的图片的实现思路谢谢了

PHP
create_time 2015-07-10 13:13:54
<?php namespace common\uploads;/** * 构建上传文件信息 * @return unknown */class Upload{public static function getFiles(){ $i=0;    $files = []; foreach($_FILES as $file){ if(is_string($file['name'])){ $files[$i]=$file; $i++; }elseif(is_array($file['name'])){ foreach($file['name'] as $key=>$val){ $files[$i]['name']=$file['name'][$key]; $files[$i]['type']=$file['type'][$key]; $files[$i]['tmp_name']=$file['tmp_name'][$key]; $files[$i]['error']=$file['error'][$key]; $files[$i]['size']=$file['size'][$key]; $i++; } } } return $files; }    public static function getExt($filename){   return strtolower(pathinfo($filename,PATHINFO_EXTENSION));}/** * 产生唯一字符串 * @return string */public static function getUniName(){ return md5(uniqid(microtime(true),true));}/** * 针对于单文件、多个单文件、多文件的上传 * @param array $fileInfo * @param string $path * @param string $flag * @param number $maxSize * @param array $allowExt * @return string */public static function uploadFile($fileInfo,$path='../web/uploads',$flag=true,$maxSize=1048576,$allowExt=array('jpeg','jpg','png','gif')){ //$flag=true; //$allowExt=array('jpeg','jpg','gif','png'); //$maxSize=1048576;//1M //判断错误号     if($fileInfo['error']===UPLOAD_ERR_OK){ //检测上传得到小 if($fileInfo['size']>$maxSize){ $res['mes']=$fileInfo['name'].'上传文件过大'; } $ext=  self::getExt($fileInfo['name']); //检测上传文件的文件类型 if(!in_array($ext,$allowExt)){ $res['mes']=$fileInfo['name'].'非法文件类型'; } //检测是否是真实的图片类型 if($flag){ if(!getimagesize($fileInfo['tmp_name'])){ $res['mes']=$fileInfo['name'].'不是真实图片类型'; } } //检测文件是否是通过HTTP POST上传上来的 if(!is_uploaded_file($fileInfo['tmp_name'])){ $res['mes']=$fileInfo['name'].'文件不是通过HTTP POST方式上传上来的'; }// if($res) return $res; //$path='./uploads'; if(!file_exists($path)){ mkdir($path,0777,true); chmod($path,0777); } $uniName = self::getUniName(); $destination=$path.'/'.$uniName.'.'.$ext; if(!move_uploaded_file($fileInfo['tmp_name'],$destination)){ $res['mes']=$fileInfo['name'].'文件移动失败'; }         $res['mes']=$fileInfo['name'].'上传成功'; $res['dest']=$destination; return $res; }else{ //匹配错误信息 switch ($fileInfo ['error']) { case 1 : $res['mes'] = '上传文件超过了PHP配置文件中upload_max_filesize选项的值'; break; case 2 : $res['mes'] = '超过了表单MAX_FILE_SIZE限制的大小'; break; case 3 : $res['mes'] = '文件部分被上传'; break; case 4 : $res['mes'] = '没有选择上传文件'; break; case 6 : $res['mes'] = '没有找到临时目录'; break; case 7 : case 8 : $res['mes'] = '系统错误'; break; } return $res; }}}这是源码不知道要在那里改= =
查看完整描述

1 回答

?
牵猪的仓鼠

TA贡献3条经验 获得超0个赞

给你提供一种思路,在访问时根据不同的参数来生成图片,此处我给你一个简单的demo

class Bo_Thumb{	private function __construct(){		$this->_enkey = 'aAi{537hs%^$&((^$)' . __FILE__;	}	/**	 * 单例对象	 * 	 * @return Bo_Thumb	 */	static function instance()	{		static $var = null;		if (empty($var))		{			require_once G_ROOT  . '/image.php';			$var = new self();		}		return $var;	}	function url($fileid, $size_w, $size_h)	{		$params = array(				'fileid' => $fileid,				'w'	=> $size_w,				'h'	=> $size_h,				ROUTE_DO_KEY => 'i',			);		$params['token'] = G::identify(json_encode($params) . $this->_enkey. G::fast_uuid(1));		$params[ROUTE_DO_KEY] = 'i' . G::identify(md5($params['token']. $this->_enkey));				return url(ROUTE_DO_APP, url_sfparams('index','thumb',$params));	}	function file($fileid, $size_w, $size_h, $token, $secret)	{		$secret_token = 'i' . G::identify(md5($token. $this->_enkey));				if ( $secret_token != $secret )		{			Bo_Throw::error(Bo_Throw::Invalid_Param, "token无效.");		}		$extname = strtolower( pathinfo($fileid, PATHINFO_EXTENSION) );		if ( !in_array($extname, array('jpg','png','gif','jpeg')) )		{			Bo_Throw::error(Bo_Throw::Invalid_Param, "fileid类型不匹配");		}		$fileid_new = rtrim($fileid, ".{$extname}") . "-{$size_w}x{$size_h}xc.{$extname}";		$file_dest = Bo_Fileupload::instance()->filepath($fileid_new);		if ( is_file($file_dest) )		{			return Bo_Fileupload::instance()->fileurl($fileid_new);		}		$file_src = Bo_Fileupload::instance()->filepath($fileid);		if ( !is_file($file_src) )		{			Bo_Throw::error(Bo_Throw::Invalid_Param, "fileid源不存在");		}		$image = Image::createFromFile($file_src,$extname);		$image->crop($size_w, $size_h);		switch ($extname) {			case 'jpg':			case 'jpeg':				$image->saveAsJpeg($file_dest);				break;			case 'png':				$image->saveAsPng($file_dest);				break;			case 'gif':				$image->saveAsGif($file_dest);				break;		}		$image->destroy();		return Bo_Fileupload::instance()->fileurl($fileid_new);	}}


查看完整回答
反对 回复 2015-08-07
  • 1 回答
  • 1 关注
  • 1537 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信