laravel 的图片上传封装到固定控制器(含缩率图)

44 篇文章 2 订阅
使用方法:直接上传图片,调用photo方法,会返回图片的id

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Models\Pic;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Intervention\Image\ImageManagerStatic as Image; //安装方法 composer require intervention/image



class PhotoController extends CommonController
{
    //封面图
    public function photo()
    {

        $file = Input::file('file');
        $mimeType = $file->getMimeType(); //文件类型
        $realPath = $file->getRealPath(); //临时文件的绝对路径

        $entension = $file -> getClientOriginalExtension(); //上传文件的后缀.
        $newName = $this->random_file_name().'.'.$entension;
        $path = $file -> move(base_path().'/public/uploads/pic/'.date("Y").'/'.date("m").'/'.date("d"),$newName);//移动文件
        $filepath = 'uploads/pic/'.date("Y").'/'.date("m").'/'.date("d").'/'.$newName;
        $thumbnailpath = base_path().'/public/uploads/pic/'.date("Y").'/'.date("m").'/'.date("d").'/thumbnail';
        if(!file_exists($thumbnailpath))//文件夹不存在,先生成文件夹
        {
            mkdir($thumbnailpath, 0777, true);
        }

        $img = Image::make($filepath)->resize(400, 400);

        $img->save($thumbnailpath.'/'.$newName);

        $data['title'] = $newName;
        $data['size'] = $this->get_file_size($path);
        $data['type'] = $mimeType;
        $data['url'] = '/'.$filepath;
        $data['delurl'] = 'file='.$newName.'&dt=uploads/pic/'.date("Y").'/'.date("m").'/'.date("d");
        $data['thumb'] = '/uploads/pic/'.date("Y").'/'.date("m").'/'.date("d").'/thumbnail/'.$newName;
        $data['add_time'] = time();

        $uploadData = Pic::create($data);
        if($uploadData){
            $vdata['id'] = $uploadData->id;
            $vdata['thumb'] = $uploadData->thumb;
            $vdata['delurl'] = $uploadData->delurl;
            $vdata['status'] = 1;
        }else{
            $vdata['status'] = -1;
            $vdata['msg'] = '请求失败,请重试';
        }



        return response()->json($vdata);
    }

    //列表图
    public function photoList(Request $request)
    {
        $file = $request->file('file');
        $mimeType = $file->getMimeType(); //文件类型
        $realPath = $file -> getRealPath(); //临时文件的绝对路径

        $entension = $file -> getClientOriginalExtension(); //上传文件的后缀.
        $newName = $this->random_file_name().'.'.$entension;
        $path = $file -> move(base_path().'/public/uploads/pic'.date("Y").'/'.date("m").'/'.date("d"),$newName);//移动文件
        $filepath = 'uploads/pic/'.date("Y").'/'.date("m").'/'.date("d").'/'.$newName;
        $thumbnailpath = base_path().'/public/uploads/pic/'.date("Y").'/'.date("m").'/'.date("d").'/thumbnail';
        if(!file_exists($thumbnailpath))//文件夹不存在,先生成文件夹
        {
            mkdir($thumbnailpath, 0777, true);
        }
        $img = Image::make($filepath)->resize(400, 400)->save($thumbnailpath.'/'.$newName);

        $data['title'] = $newName;
        $data['size'] = $this->get_file_size($path);
        $data['type'] = $mimeType;
        $data['url'] = '/'.$filepath;
        $data['delurl'] = 'file='.$newName.'&dt=uploads/pic/'.date("Y").'/'.date("m").'/'.date("d");
        $data['thumb'] = '/uploads/pic/'.date("Y").'/'.date("m").'/'.date("d").'/thumbnail/'.$newName;
        $data['add_time'] = time();


        $uploadData = Pic::create($data);
        if($uploadData) {
            $vdata['id'] = $uploadData->id;
            $vdata['thumb'] = $uploadData->thumb;
            $vdata['delurl'] = $uploadData->delurl;
            $vdata['status'] = 1;
        }else{
            $vdata['status'] = -1;
            $vdata['msg'] = '请求失败,请重试';
        }
        return response()->json($vdata);
    }


    public function delphoto(){

        if(is_readable(Input::get('dt').'/'.Input::get('file')) && is_readable(Input::get('dt').'/thumbnail/'.Input::get('file'))){
            if(unlink(Input::get('dt').'/'.Input::get('file')) && unlink(Input::get('dt').'/thumbnail/'.Input::get('file'))){
                $re = Pic::where('id',Input::get('delid'))->delete();
                if($re){
                    $vdata['status'] = 1;
                    $vdata['msg'] = '删除成功';
                }else{
                    $vdata['status'] = -1;
                    $vdata['msg'] = '删除失败';
                }
            }else{
                $vdata['status'] = -1;
                $vdata['msg'] = '删除失败';
            }
            return response()->json($vdata);
        }
    }


    // ADD by:era 添加随机命名
    protected function random_file_name() {
        $alphanum = 'abcdefghijklmnopqrstuvwxyz0123456789';
        $len = 5;
        $name= str_replace(".", "", microtime(true));
        for ($i=0; $i<$len; $i++) {
            $name .= $alphanum[rand(0,strlen($alphanum)-1)];
        }
        return strtolower($name);
    }
    //转换字节
    protected function fix_integer_overflow($size) {
        if ($size < 0) {
            $size += 2.0 * (PHP_INT_MAX + 1);
        }
        return $size;
    }

    protected function get_file_size($file_path, $clear_stat_cache = false) {
        if ($clear_stat_cache) {
            clearstatcache(true, $file_path);
        }
        return $this->fix_integer_overflow(filesize($file_path));
    }



}

*********************************************************
数据库字段

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值