ImageController.php 11.7 KB
<?php

namespace App\Http\Controllers\File;

use App\Enums\Common\Code;
use App\Http\Controllers\Controller;
use App\Http\Controllers\type;
use App\Models\File\Image as ImageModel;
use App\Models\Project\Project;
use App\Services\CosService;
use App\Services\TencentCosService;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;

class ImageController extends Controller
{
    public $upload_img = [
            //设置静态缓存参数(304)
            'header' => [
                'Cache-Control' => 'max-age=2592000',
                'Pragma' => 'cache',
                'Expires' => "%Expires%", // cache 1 month
                'etag' => "%etag%",
                'Last-Modified' => "%Last-Modified%",
                'Content-Description' => 'File Transfer',
            ],
    ];
    public $path = '';//路径

    public $config = '';//存储默认配置

    public $request = '';//request

    public $param = '';//参数

    public $token = '';//token

    public $cache = '';//缓存数据

    public $upload_location = 0;//是否同步到cos
    //上传图片类型
    public $image_type = [
        1 => 'image_product',
        2 => 'image_news',
        3 => 'image_blog',
        4 => 'image_product_icon',
        5 => 'image_product_cate',
        0 => 'image_other',
    ];

    public function __construct()
    {
        $this->request = request();
        $this->token = $this->request->header('token');
        $this->cache = Cache::get($this->token);
        $this->param = $this->request->all();
        $this->config = config('filesystems.disks.upload');
        $this->uploads = config('upload.default_image');
    }

    /**
     * @param $hash
     * @param $w
     * @param $h
     * @name   :index
     * @author :lyh
     * @method :post
     * @time   :2023/5/11 17:19
     */
    public function index($hash = '', $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;
        }
        $imageModel = new ImageModel();
        $info = $imageModel->read(['hash'=>$hash]);
        if ($info === false) {
            $this->response('指定图片不存在!', Code::USER_ERROR);
        }
        //查看缩略图是否存在
        $filename = $this->config['root'] . '/' .$info['path'] . '_' . $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 . '_' . 1, $last_modified_time], $this->upload_img['header']);
            $content = file_get_contents($filename);
            $header['Content-Length'] = strlen($content);
        }else{
            $path = $this->config['root'].'/'.$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 . '_' . 1, $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'] = strlen($content);
            }
        }
        $header['Content-Type'] = 'image/'.$info['type'];
        return response($content,200,$header);
    }


    /**
     * @name   :(图片上传)upload
     * @author :lyh
     * @method :post
     * @time   :2023/6/17 16:28
     */
    public function upload() {
        $this->request->validate([
            'image'=>['required'],
        ],[
            'image.required'=>'图片必须填写',
        ]);
        $files = $this->request->file('image');
        if (empty($files)) {
            $this->response('没有上传的文件!', Code::USER_ERROR);
        }
        $type = $this->request->post('type','single');
        $this->setUrl();
        if ($type == 'multi') {
            return $this->multi($files);
        }else{
            $size = $files->getSize();
            $image_type = $files->getClientOriginalExtension();
            return $this->single($files,$size,$image_type);
        }
    }

    /**
     * @param $files
     * @remark :单图片上传
     * @name   :single
     * @author :lyh
     * @method :post
     * @time   :2023/6/17 16:30
     */
    public function single(&$files,$size,$image_type){
        $hash = hash_file('md5', $files->getPathname());
        //查看文件是否存在
        $imageModel = new ImageModel();
        $image_hash = $imageModel->read(['hash'=>$hash]);
        if($image_hash !== false){
            return $this->response('图片资源',Code::SUCCESS,['image'=>$hash,'image_link'=>$this->getImageUrl($hash)]);
        }
        $url = $this->config['root'].$this->path;
        $fileName = uniqid().rand(10000,99999).'.'.$image_type;
        //上传到cos
        if($this->upload_location == 1){
            $cosService = new CosService();
            $cosService->uploadFile($files,$this->path,$fileName);
        }else{
            $res =  $files->move($url,$fileName);
            if ($res === false) {
                return $this->response($files->getError(), Code::USER_ERROR);
            }
        }
        $this->saveMysql($imageModel,$size,$image_type,$fileName,$hash,$this->upload_location);
        return $this->response('图片资源',Code::SUCCESS,['image'=>$hash,'image_link'=>$this->getImageUrl($hash)]);
    }

    /**
     * @remark :保存数据库
     * @name   :saveMysql
     * @author :lyh
     * @method :post
     * @time   :2023/7/19 16:38
     */
    public function saveMysql(&$imageModel,$size,$image_type,$fileName,$hash,$is_cos = 0){
        $data = [
            'path' => $this->path.'/'.$fileName,
            'created_at' => date('Y-m-d H:i:s',time()),
            'size' => $size,
            'hash' => $hash,
            'type'=>$image_type,
            'refer'=>$this->param['refer'] ?? 1,
            'is_cos'=>$is_cos
        ];
        $rs = $imageModel->add($data);
        if ($rs === false) {
            return $this->response('添加失败', Code::USER_ERROR);
        }
        return true;
    }
    /**
     * @param $info
     * @param $w
     * @param $h
     * @remark :生成缩略图
     * @name   :cacheImage
     * @author :lyh
     * @method :post
     * @time   :2023/6/17 16:31
     */
    private function cacheImage($info, $w, $h) {
        $path = $info['path'];
        $filename = $this->config['root'] . '/' . $info['path'] . '_' . $w . '_' . $h;
        Image::make($path)->resize($w, $h)->save($filename);
        return $filename;
    }

    /**
     * @param $files
     * @remark :多图片上传
     * @name   :multi
     * @author :lyh
     * @method :post
     * @time   :2023/6/17 16:31
     */
    private function multi(&$files) {
        $data = [];
        foreach ($files as $file) {
            $size = $file->getSize();
            $image_type = $file->getClientOriginalExtension();
            $imageModel = new ImageModel();
            $hash = hash_file('md5', $file->getPathname());
            $image_hash = $imageModel->read(['hash'=>$hash]);
            if($image_hash !== false){
                $data[] = ['image'=>$hash,'image_link'=>$this->getImageUrl($hash)];
                continue;
            }
            $url = $this->config['root'].$this->path;
            $fileName = uniqid().rand(10000,99999).'.'.$file->getClientOriginalExtension();
            //同步数据到cos
            if($this->upload_location == 1){
                $cosService = new CosService();
                $cosService->uploadFile($file,$this->path,$fileName);
            }else{
                $res = $file->move($url,$fileName);
                if ($res === false) {
                    return $this->response($file->getError(), Code::USER_ERROR);
                }
            }
            //批量存储
            $this->saveMysql($imageModel,$size,$image_type,$fileName,$hash,$this->upload_location);
            $data[] = ['image'=>$hash,'image_link'=>$this->getImageUrl($hash)];
        }
        return $this->response('图片资源',Code::SUCCESS,$data);
    }

    /**
     * @param $filename
     * @remark :下载
     * @name   :download
     * @author :lyh
     * @method :post
     * @time   :2023/7/19 17:59
     */
    public function download($filename){
        $path = Storage::path($filename);
        return response()->download($path,time().rand(1,100000));
    }

    /**
     * @param $msg
     * @param string $code
     * @param $data
     * @param $result_code
     * @param $type
     * @remark :统一返回
     * @name   :response
     * @author :lyh
     * @method :post
     * @time   :2023/7/19 17:59
     */
    public function response($msg = null,string $code = Code::SUCCESS,$data = [],$result_code = 200,$type = 'application/json'): JsonResponse
    {
        $code = Code::fromValue($code);
        $result = [
            'msg' => $msg == ' ' ? $code->description : $msg,
            'code' => $code->value,
            'data' => $data,
        ];
        $this->header['Content-Type'] = $type;
        $response =  response($result,$result_code,$this->header);
        throw new HttpResponseException($response);
    }


    /**
     * @remark :获取所有图片
     * @name   :getImageList
     * @author :lyh
     * @method :post
     * @time   :2023/6/29 11:48
     */
    public function getImageList(){
        $imageModel = new ImageModel();
        $lists = $imageModel->list($this->param,$order = 'id');
        foreach ($lists as $k => $v){
            $v['image_link'] = $this->getImageUrl($v['hash']);
            $lists[$k] = $v;
        }
        $this->response('success',Code::SUCCESS,$lists);
    }

    /**
     * @remark :保存路径
     * @name   :setUrl
     * @author :lyh
     * @method :post
     * @time   :2023/7/18 15:36
     */
    public function setUrl(){
        //A端上传
        if(isset($this->param['refer_type']) && $this->param['refer_type'] == 1){
            $this->path = $this->uploads['path_a'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m');
        }else{
            //B端上传
            if(isset($this->param['upload_method']) && $this->param['upload_method'] == 1){
                //强制上传本地配置
                $this->upload_location = 0;
            }else{
                //根据项目上传标识区分上传到cos/本地
                $projectModel = new Project();
                $project_info = $projectModel->read(['id'=>$this->cache['project_id']],['upload_location']);
                $this->upload_location = $project_info['upload_location'];
            }
            $this->path = $this->uploads['path_b'].'/'.$this->cache['project_id'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m');
        }
    }

    /**
     * @remark :获取图片链接
     * @name   :getImageUrl
     * @author :lyh
     * @method :post
     * @time   :2023/7/20 16:46
     */
    public function getImageUrl($hash){
        $imageModel = new ImageModel();
        $info = $imageModel->read(['hash'=>$hash]);
        if($info['is_cos'] == 1){
            $cos = new CosService();
            $url = $cos->getImageUrl($info['path']);
        }else{
            $url = url('a/image/'.$info['hash']);
        }
        return $url;
    }
}