|
...
|
...
|
@@ -4,12 +4,41 @@ namespace App\Http\Controllers; |
|
|
|
|
|
|
|
use App\Enums\Common\Code;
|
|
|
|
use App\Models\Image as ImageModel;
|
|
|
|
use Faker\Provider\Image;
|
|
|
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Intervention\Image\Facades\Image;
|
|
|
|
|
|
|
|
class ImageController
|
|
|
|
{
|
|
|
|
public $upload_img = [
|
|
|
|
//验证
|
|
|
|
'rule' => [
|
|
|
|
'size' => 3 * 1024 * 1024, //大小限制
|
|
|
|
'ext' => 'jpeg,jpg,png,gif', //文件类型限制
|
|
|
|
],
|
|
|
|
//生成文件规则
|
|
|
|
'savename' => 'uniqid',
|
|
|
|
//设置静态缓存参数(304)
|
|
|
|
'header' => [
|
|
|
|
'Cache-Control' => 'max-age=2592000',
|
|
|
|
'Pragma' => 'cache',
|
|
|
|
'Expires' => "%Expires%", // cache 1 month
|
|
|
|
'etag' => "%etag%",
|
|
|
|
'Last-Modified' => "%Last-Modified%",
|
|
|
|
'Content-Description' => 'File Transfer',
|
|
|
|
],
|
|
|
|
//图片保存根路径
|
|
|
|
'path' => './uploads/images/',
|
|
|
|
//图片上传变量名
|
|
|
|
'name' => 'image',
|
|
|
|
];
|
|
|
|
public $request = '';
|
|
|
|
|
|
|
|
public function __construct(Request $request)
|
|
|
|
{
|
|
|
|
$this->request = $request;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function index($hash = '', $w = 0 ,$h = 0){
|
|
|
|
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) || isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
|
|
...
|
...
|
@@ -21,15 +50,26 @@ class ImageController |
|
|
|
if ($info === false) {
|
|
|
|
$this->response('指定图片不存在!', 404);
|
|
|
|
}
|
|
|
|
//查看缩略图是否存在
|
|
|
|
$filename = './../uploads/images/cache_'. $info['hash'] . $w . '_' . $h;
|
|
|
|
if(is_file($filename)){
|
|
|
|
$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']);
|
|
|
|
$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,$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->config['header_cache']);
|
|
|
|
$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, 2);
|
|
|
|
$path = $this->cacheImage($info, $w, $h);
|
|
|
|
$content = file_get_contents($path);
|
|
|
|
$header['Content-Length'] = strlen($content);
|
|
|
|
} else {
|
|
...
|
...
|
@@ -38,7 +78,51 @@ class ImageController |
|
|
|
}
|
|
|
|
return response($content, 200, $header);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 图片上传
|
|
|
|
*/
|
|
|
|
public function upload() {
|
|
|
|
$files = $this->request->file('image');
|
|
|
|
if (empty($files)) {
|
|
|
|
$this->response('没有上传的文件!', 400);
|
|
|
|
}
|
|
|
|
$type = $this->request->post('type', 'single');
|
|
|
|
if ($type == 'multi') {
|
|
|
|
return $this->multi($files);
|
|
|
|
} else {
|
|
|
|
return $this->single($files);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @name :上传图片
|
|
|
|
* @return void
|
|
|
|
* @author :liyuhang
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
public function single($files){
|
|
|
|
if (is_array($files)) {
|
|
|
|
$file = current($files);
|
|
|
|
}
|
|
|
|
$image = $this->request->file('image');
|
|
|
|
if(empty($image)){
|
|
|
|
return $this->fail('没有上传图片',Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
$url = './../uploads/images/';
|
|
|
|
$filename = date('ymdHis').rand(10000,99999);
|
|
|
|
$res = $this->request->file('image')->move($url,$filename);
|
|
|
|
if ($res === false) {
|
|
|
|
return $this->fail($image->getError(), 400);
|
|
|
|
}
|
|
|
|
$data = [
|
|
|
|
'path' => $url.$filename,
|
|
|
|
'created_at' => date('Y-m-d H:i:s',time()),
|
|
|
|
'size' => $res->getSize(),
|
|
|
|
'hash' => hash(),
|
|
|
|
];
|
|
|
|
$imageModel = new ImageModel();
|
|
|
|
$imageModel->add($data);
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 生成缩略图缓存
|
|
|
|
* @param type $info
|
|
...
|
...
|
@@ -46,16 +130,11 @@ class ImageController |
|
|
|
* @param type $h
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function cacheImage($info, $w, $h, $type = 2) {
|
|
|
|
$thumbnailPath = './../uploads/image/';
|
|
|
|
$thumbnailImage = Image::make($info)
|
|
|
|
->fit($w, $h, function ($constraint) {
|
|
|
|
$constraint->upsize();
|
|
|
|
});
|
|
|
|
|
|
|
|
$thumbnailImage->save($thumbnailPath);
|
|
|
|
return $thumbnailPath;
|
|
|
|
return;
|
|
|
|
private function cacheImage($info, $w, $h) {
|
|
|
|
$path = './../'.$info['path'];
|
|
|
|
$filename = './../uploads/images/cache_'. $info['hash'] . $w . '_' . $h;
|
|
|
|
Image::make($path)->resize($w, $h)->save($filename);
|
|
|
|
return $filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
...
|
...
|
|