|
...
|
...
|
@@ -7,6 +7,7 @@ use App\Models\Image as ImageModel; |
|
|
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
use Intervention\Image\Facades\Image;
|
|
|
|
|
|
|
|
class ImageController
|
|
...
|
...
|
@@ -48,7 +49,7 @@ class ImageController |
|
|
|
$imageModel = new ImageModel();
|
|
|
|
$info = $imageModel->read(['hash'=>$hash]);
|
|
|
|
if ($info === false) {
|
|
|
|
$this->response('指定图片不存在!', 404);
|
|
|
|
$this->response('指定图片不存在!', Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
//查看缩略图是否存在
|
|
|
|
$filename = './../uploads/images/cache_'. $info['hash'] . $w . '_' . $h;
|
|
...
|
...
|
@@ -58,23 +59,23 @@ class ImageController |
|
|
|
[$last_modified_time, $hash . ':' . $w . '_' . $h . '_' . 2, $last_modified_time], $this->upload_img['header']);
|
|
|
|
$content = file_get_contents($filename);
|
|
|
|
$header['Content-Length'] = $info['size'];
|
|
|
|
return response($content, 200, $header);
|
|
|
|
}
|
|
|
|
$path = './../'.$info['path'];
|
|
|
|
if (!is_file($path)) {
|
|
|
|
$this->response('指定图片已被系统删除!', 404);
|
|
|
|
}
|
|
|
|
$content = '';
|
|
|
|
$last_modified_time = gmdate(time() + ((30 * 60 * 60 * 24))) . " GMT";
|
|
|
|
$header = str_replace(['%Expires%', "%etag%", '%Last-Modified%'],
|
|
|
|
[$last_modified_time, $hash . ':' . $w . '_' . $h . '_' . 2, $last_modified_time], $this->upload_img['header']);
|
|
|
|
if ($w > 0 && $h > 0) {
|
|
|
|
$path = $this->cacheImage($info, $w, $h);
|
|
|
|
$content = file_get_contents($path);
|
|
|
|
$header['Content-Length'] = strlen($content);
|
|
|
|
} else {
|
|
|
|
$content = file_get_contents($path);
|
|
|
|
$header['Content-Length'] = $info['size'];
|
|
|
|
}else{
|
|
|
|
$path = $info['path'];
|
|
|
|
if (!is_file($path)) {
|
|
|
|
$this->response('指定图片已被系统删除!', Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
$content = '';
|
|
|
|
$last_modified_time = gmdate(time() + ((30 * 60 * 60 * 24))) . " GMT";
|
|
|
|
$header = str_replace(['%Expires%', "%etag%", '%Last-Modified%'],
|
|
|
|
[$last_modified_time, $hash . ':' . $w . '_' . $h . '_' . 2, $last_modified_time], $this->upload_img['header']);
|
|
|
|
if ($w > 0 && $h > 0) {
|
|
|
|
$path = $this->cacheImage($info, $w, $h);
|
|
|
|
$content = file_get_contents($path);
|
|
|
|
$header['Content-Length'] = strlen($content);
|
|
|
|
} else {
|
|
|
|
$content = file_get_contents($path);
|
|
|
|
$header['Content-Length'] = $info['size'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$img_type = $info['type'];
|
|
|
|
$content = base64_encode($content);
|
|
...
|
...
|
@@ -117,12 +118,14 @@ class ImageController |
|
|
|
if ($res === false) {
|
|
|
|
return $this->fail($files->getError(), 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'path' => $url.$filename,
|
|
|
|
'created_at' => date('Y-m-d H:i:s',time()),
|
|
|
|
'size' => $res->getSize(),
|
|
|
|
'hash' => $files->hashName(),
|
|
|
|
'mime'=>$files->extension()
|
|
|
|
'hash' => hash_file('md5', $res->getPathname()),
|
|
|
|
'type'=>$files->getClientOriginalExtension(),
|
|
|
|
// 'mime'=>$files->getMimeType()
|
|
|
|
];
|
|
|
|
$imageModel = new ImageModel();
|
|
|
|
$imageModel->add($data);
|
|
...
|
...
|
@@ -136,7 +139,7 @@ class ImageController |
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function cacheImage($info, $w, $h) {
|
|
|
|
$path = './../'.$info['path'];
|
|
|
|
$path = $info['path'];
|
|
|
|
$filename = './../uploads/images/cache_'. $info['hash'] . $w . '_' . $h;
|
|
|
|
Image::make($path)->resize($w, $h)->save($filename);
|
|
|
|
return $filename;
|
|
...
|
...
|
@@ -154,10 +157,9 @@ class ImageController |
|
|
|
$result = [
|
|
|
|
'msg' => $msg == ' ' ? $code->description : $msg,
|
|
|
|
'code' => $code->value,
|
|
|
|
'data' => $this->_extents($data),
|
|
|
|
'data' => $data,
|
|
|
|
];
|
|
|
|
$this->header['Content-Type'] = $type;
|
|
|
|
$this->header['token'] = $this->token;
|
|
|
|
$response = response($result,$result_code,$this->header);;
|
|
|
|
throw new HttpResponseException($response);
|
|
|
|
}
|
...
|
...
|
|