laravel 的圖片上傳封裝到固定控制器(含縮率圖)

使用方法:直接上傳圖片,調用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));
    }



}

*********************************************************
數據庫字段

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章