<?php
namespace app\common;
use think\Controller;
use Request,Log,Config;
class Upload extends Controller
{
protected $size;
protected $savePath;
protected $i;
public function upload($file, $num=1){
$path = config('common.app_path');
$config = config('upload.');
$this->size = $config['img_size'];
$this->savePath = $config['save_path'] . DIRECTORY_SEPARATOR . 'images';
$this->i = $config['compression_size'];
if(1 == $num){
$info = $file->validate(['size'=>$this->size])
->move($this->savePath);
if($info){
$url1 = dirname($_SERVER['SCRIPT_FILENAME']) . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $info->getSaveName();
$image = Image::open($url1);
$width = $image->width() * $this->i;
$height = $image->height() * $this->i;
$image->thumb($width, $height, Image::THUMB_CENTER)->save($url1,'jpg',30,true);
$path = $path. DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $info->getSaveName();
$path = str_replace('\\', '/', $path);
return $path;
}else{
return $file->getError();
}
}else{
return $this->manyUpload($file);
}
}
public function manyUpload($files){
$url = config('common.app_path');
$path = null;
foreach($files as $file) {
$info = $file->validate(['size'=>$this->size])
->move($this->savePath);
if ($info) {
$url1 = dirname($_SERVER['SCRIPT_FILENAME']) . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $info->getSaveName();
$image = Image::open($url1);
$width = $image->width() * $this->i;
$height = $image->height() * $this->i;
$image->thumb($width, $height, Image::THUMB_SCALING)->save($url1,'jpg',30,true);
$path .= $url . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $info->getSaveName() . "|";
} else {
return $file->getError();
}
}
$path = str_replace('\\', '/', $path);
return rtrim($path, '|');
}
function Base64Upload($base64) {
$url = config('common.app_path');
$path = null;
$result = array();
foreach ($base64 as $k => $v) {
$path = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR;
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $v, $re)){
$type = $re[2];
$new_file = $path . date('Ymd',time())."/";
if(!file_exists($new_file))
{
mkdir(iconv("UTF-8", "GBK", $new_file),0777,true);
@chmod($new_file, 0777);
}
$file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $type;
$new_file = $new_file . $file_name;
if (file_put_contents($new_file, base64_decode(str_replace($re[1], '', $v)))){
$result[$k] = $url . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . date('Ymd',time()) . DIRECTORY_SEPARATOR . $file_name;
$result[$k] = str_replace('\\', '/', $result[$k]);
}
}
}
return $result;
}
public function uploadFile($file)
{
$path = config('common.app_path');
$config = config('upload.');
$size = $config['file_size'];
$savePath = $config['save_path'] . DIRECTORY_SEPARATOR . 'files';
$info = $file->validate(['size'=>$size])
->move($savePath);
if($info){
$path = $path. DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . $info->getSaveName();
$path = str_replace('\\', '/', $path);
return $path;
}else{
return $file->getError();
}
}
}