php版阿里云OSS图片上传类详解(2)

/** * 上传一个文件 * @access public * @param mixed $name 数据 * @param string $value 数据表名 * @return string */ private function save($file) { $filename = $file['savepath'].$file['savename']; if(!$this->uploadReplace && $this->doesObjectExist($filename)) { // 不覆盖同名文件 $this->error = '文件已经存在!'.$filename; return false; } // 如果是图像文件 检测文件格式 if( in_array(strtolower($file['extension']),array('gif','jpg','jpeg','bmp','png','swf'))) { $info = getimagesize($file['tmp_name']); if(false === $info || ('gif' == strtolower($file['extension']) && empty($info['bits']))){ $this->error = '非法图像文件'; return false; } } if(!$this->putObject($file['tmp_name'], $this->autoCharset($filename,'utf-8','gbk'))) { $this->error = '文件上传保存错误!'; return false; } if($this->thumb && in_array(strtolower($file['extension']),array('gif','jpg','jpeg','bmp','png'))) { $image = getimagesize(C('OSS_IMG_URL').'https://www.jb51.net/'.$filename); if(false !== $image) { //是图像文件生成缩略图 $thumbWidth = explode(',',$this->thumbMaxWidth); $thumbHeight = explode(',',$this->thumbMaxHeight); $thumbPrefix = explode(',',$this->thumbPrefix); $thumbSuffix = explode(',',$this->thumbSuffix); $thumbFile = explode(',',$this->thumbFile); $thumbPath = $this->thumbPath?$this->thumbPath:dirname($filename).'https://www.jb51.net/'; $thumbExt = $this->thumbExt ? $this->thumbExt : $file['extension']; //自定义缩略图扩展名 // 生成图像缩略图 import($this->imageClassPath); for($i=0,$len=count($thumbWidth); $i<$len; $i++) { if(!empty($thumbFile[$i])) { $thumbname = $thumbFile[$i]; }else{ $prefix = isset($thumbPrefix[$i])?$thumbPrefix[$i]:$thumbPrefix[0]; $suffix = isset($thumbSuffix[$i])?$thumbSuffix[$i]:$thumbSuffix[0]; $thumbname = $prefix.basename($filename,'.'.$file['extension']).$suffix; } $this->thumb(C('OSS_IMG_URL').'https://www.jb51.net/'.$filename,$thumbPath.$thumbname.'.'.$thumbExt,'',$thumbWidth[$i],$thumbHeight[$i],true); } if($this->thumbRemoveOrigin) { // 生成缩略图之后删除原图 $this->deleteObject($filename); } } } if($this->zipImags) { // TODO 对图片压缩包在线解压 } return true; } /** * 生成缩略图 * @static * @access public * @param string $image 原图 * @param string $type 图像格式 * @param string $thumbname 缩略图文件名 * @param string $maxWidth 宽度 * @param string $maxHeight 高度 * @param string $position 缩略图保存目录 * @param boolean $interlace 启用隔行扫描 * @return void */ public function thumb($image, $thumbname, $type='', $maxWidth=200, $maxHeight=50, $interlace=true) { // 获取原图信息 $info = Image::getImageInfo($image); if ($info !== false) { $srcWidth = $info['width']; $srcHeight = $info['height']; $type = empty($type) ? $info['type'] : $type; $type = strtolower($type); $interlace = $interlace ? 1 : 0; unset($info); $scale = min($maxWidth / $srcWidth, $maxHeight / $srcHeight); // 计算缩放比例 if ($scale >= 1) { // 超过原图大小不再缩略 $width = $srcWidth; $height = $srcHeight; } else { // 缩略图尺寸 $width = (int) ($srcWidth * $scale); $height = (int) ($srcHeight * $scale); } // 载入原图 $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type); if(!function_exists($createFun)) { return false; } $srcImg = $createFun($image); //创建缩略图 if ($type != 'gif' && function_exists('imagecreatetruecolor')) $thumbImg = imagecreatetruecolor($width, $height); else $thumbImg = imagecreate($width, $height); //png和gif的透明处理 by luofei614 if('png'==$type){ imagealphablending($thumbImg, false);//取消默认的混色模式(为解决阴影为绿色的问题) imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息(为解决阴影为绿色的问题) }elseif('gif'==$type){ $trnprt_indx = imagecolortransparent($srcImg); if ($trnprt_indx >= 0) { //its transparent $trnprt_color = imagecolorsforindex($srcImg , $trnprt_indx); $trnprt_indx = imagecolorallocate($thumbImg, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); imagefill($thumbImg, 0, 0, $trnprt_indx); imagecolortransparent($thumbImg, $trnprt_indx); } } // 复制图片 if (function_exists("ImageCopyResampled")) imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight); else imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight); // 对jpeg图形设置隔行扫描 if ('jpg' == $type || 'jpeg' == $type) imageinterlace($thumbImg, $interlace); imagePNG($thumbImg,'Uploads/file.png'); // 中转站 // 生成图片 $this->putObject('Uploads/file.png',$thumbname); imagedestroy($thumbImg); imagedestroy($srcImg); return $thumbname; } return false; }

5.辅助函数

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/92f0cec5860eba30efb47b9c27180a7b.html