|
...
|
...
|
@@ -6,6 +6,7 @@ use App\Enums\Common\Code; |
|
|
|
use App\Http\Controllers\type;
|
|
|
|
use App\Http\Controllers\统一返回参数;
|
|
|
|
use App\Models\File\Image as ImageModel;
|
|
|
|
use App\Models\User\User as UserModel;
|
|
|
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
...
|
...
|
@@ -24,6 +25,8 @@ class ImageController |
|
|
|
'Content-Description' => 'File Transfer',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
const TYPE = 1;
|
|
|
|
|
|
|
|
public $path = '';
|
|
|
|
|
|
|
|
public $config = '';
|
|
...
|
...
|
@@ -36,10 +39,11 @@ class ImageController |
|
|
|
{
|
|
|
|
$this->request = request();
|
|
|
|
$this->config = config('filesystems.disks.upload');
|
|
|
|
$this->path = $this->config['root'];
|
|
|
|
$this->uploads = config('upload.default_image');
|
|
|
|
$this->path = $this->config['root'].$this->uploads['path'].'/';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function index($hash = '', $w = 0 ,$h = 0){
|
|
|
|
public function index($hash = '',$type = self::TYPE, $w = 0 ,$h = 0 ){
|
|
|
|
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) || isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
|
|
|
|
header("HTTP/1.1 304 Not Modified");
|
|
|
|
exit;
|
|
...
|
...
|
@@ -50,6 +54,7 @@ class ImageController |
|
|
|
$this->response('指定图片不存在!', Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
//查看缩略图是否存在
|
|
|
|
// $header['Content-Type'] = 'image/'.$info['type'];
|
|
|
|
$filename = $this->path . $info['hash'] . $w . '_' . $h;
|
|
|
|
if(is_file($filename)){
|
|
|
|
$last_modified_time = gmdate(time() + ((30 * 60 * 60 * 24))) . " GMT";
|
|
...
|
...
|
@@ -78,7 +83,12 @@ class ImageController |
|
|
|
$img_type = $info['type'];
|
|
|
|
$content = base64_encode($content);
|
|
|
|
$img_base64 = 'data:image/' . $img_type . ';base64,' . $content;
|
|
|
|
return response($img_base64, 200, $header);
|
|
|
|
if($type != self::TYPE){
|
|
|
|
header('Content-Type: image/' . $img_type);
|
|
|
|
echo base64_decode($content);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
return response($img_base64,200,$header);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
...
|
...
|
@@ -114,15 +124,16 @@ class ImageController |
|
|
|
$imageModel = new ImageModel();
|
|
|
|
$image_hash = $imageModel->read(['hash'=>$hash]);
|
|
|
|
if($image_hash !== false){
|
|
|
|
return $hash;
|
|
|
|
return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]);
|
|
|
|
}
|
|
|
|
$url = $this->path;
|
|
|
|
$res = $this->request->file('image')->move($url);
|
|
|
|
$fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension();
|
|
|
|
$res = $files->move($url,$fileName);
|
|
|
|
if ($res === false) {
|
|
|
|
return $this->response($files->getError(), Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
$data = [
|
|
|
|
'path' => $url.'/'.$res->getClientOriginalName(),
|
|
|
|
'path' => $url.$fileName,
|
|
|
|
'created_at' => date('Y-m-d H:i:s',time()),
|
|
|
|
'size' => $res->getSize(),
|
|
|
|
'hash' => $hash,
|
|
...
|
...
|
@@ -132,7 +143,7 @@ class ImageController |
|
|
|
if ($rs === false) {
|
|
|
|
return $this->response('添加失败', Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
return $hash;
|
|
|
|
return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 生成缩略图缓存
|
|
...
|
...
|
@@ -154,6 +165,7 @@ class ImageController |
|
|
|
* @return type
|
|
|
|
*/
|
|
|
|
private function multi($files) {
|
|
|
|
|
|
|
|
if (!is_array($files)) {
|
|
|
|
$files = [$files];
|
|
|
|
}
|
|
...
|
...
|
@@ -168,21 +180,23 @@ class ImageController |
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$url = $this->path;
|
|
|
|
$res = $file->move($url);
|
|
|
|
$fileName = uniqid().rand(10000,99999).'.'.$file->getClientOriginalExtension();
|
|
|
|
$res = $file->move($url,$fileName);
|
|
|
|
if ($res === false) {
|
|
|
|
return $this->response($file->getError(), Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
$save_data[] = [
|
|
|
|
'path' => $url.'/'.$res->getClientOriginalName(),
|
|
|
|
'path' => $url.$fileName,
|
|
|
|
'created_at' => date('Y-m-d H:i:s',time()),
|
|
|
|
'updated_at'=>date('Y-m-d H:i:s',time()),
|
|
|
|
'size' => $res->getSize(),
|
|
|
|
'hash' => $hash,
|
|
|
|
'type'=>$files->getClientOriginalExtension(),
|
|
|
|
'type'=>$file->getClientOriginalExtension(),
|
|
|
|
];
|
|
|
|
$data[] = $hash;
|
|
|
|
}
|
|
|
|
$imageModel->insert($save_data);
|
|
|
|
return $data;
|
|
|
|
return $this->response('图片资源',Code::SUCCESS,['image'=>$data]);
|
|
|
|
}
|
|
|
|
|
|
|
|
//下载
|
|
...
|
...
|
@@ -202,10 +216,50 @@ class ImageController |
|
|
|
$result = [
|
|
|
|
'msg' => $msg == ' ' ? $code->description : $msg,
|
|
|
|
'code' => $code->value,
|
|
|
|
'data' => $data,
|
|
|
|
'data' => $this->_extents($data),
|
|
|
|
];
|
|
|
|
$this->header['Content-Type'] = $type;
|
|
|
|
$response = response($result,$result_code,$this->header);
|
|
|
|
throw new HttpResponseException($response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $data
|
|
|
|
* @name :返回参数处理
|
|
|
|
* @return array|string
|
|
|
|
* @author :liyuhang
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
protected function _extents($data) {
|
|
|
|
|
|
|
|
if (empty($data) || !is_array($data)) {
|
|
|
|
return empty($data) ? is_array($data) ? [] : '' : $data;
|
|
|
|
}
|
|
|
|
foreach ($data as $k => $v) {
|
|
|
|
if (is_array($v)) {
|
|
|
|
$data[$k] = $this->_extents($v);
|
|
|
|
} else {
|
|
|
|
if (is_null($v)) {
|
|
|
|
$data[$k] = '';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
//获取操作人
|
|
|
|
switch ((string) $k) {
|
|
|
|
case 'image':
|
|
|
|
$data['image_link'] = url('/b/image/' . $v);
|
|
|
|
break;
|
|
|
|
case 'images':
|
|
|
|
$v = explode(',',$v);
|
|
|
|
foreach ($v as $k1=>$v1){
|
|
|
|
$data['images_link'][$k1] = url('/b/image/' . $v1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'file':
|
|
|
|
$data['file_link'] = url('/b/file_hash/' . $v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|