2017 © Pedro Peláez
 

library laravel-fileapi

Laravel File API - Handle Files with Laravel Storage

image

unisharp/laravel-fileapi

Laravel File API - Handle Files with Laravel Storage

  • Thursday, June 21, 2018
  • by storyn26383
  • Repository
  • 8 Watchers
  • 32 Stars
  • 4,404 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 6 Forks
  • 2 Open issues
  • 6 Versions
  • 17 % Grown

The README.md

Laravel File API

Features

  • Handle files with Laravel Storage.
  • Load files through Laravel routing instead of public path.
  • Save images with thumbs, compressed image, and sizes are customisable.

Installation

  1. Install File API, (*1)

    composer require unisharp/laravel-fileapi
    
  2. Set service provider in config/app.php, (*2)

    Unisharp\FileApi\FileApiServiceProvider::class,
    
  3. publish config file, (*3)

    php artisan vendor:publish --tag=fileapi_config
    

Config

in config/fileapi.php, (*4)

  1. fill in the storage path, which make routes for you., (*5)

    'path' => ['/images/event/', '/images/article/'],
    

    it will generate routes like below :, (*6)

    Route::get('/images/event/{filename}', function ($filename) {
        $entry = new \Unisharp\FileApi\FileApi('/images/event/');
        return $entry->getResponse($filename);
    });
    
    Route::get('/images/article/{filename}', function ($filename) {
        $entry = new \Unisharp\FileApi\FileApi('/images/article/');
        return $entry->getResponse($filename);
    });
    
  2. set default thumb sizes(by key and value), (*7)

    'default_thumbs' => ['S' => '96x96', 'M' => '256x256', 'L' => '480x480'],
    
  3. set default image compress quality, (*8)

    'compress_quality' => 90,
    
  4. choose whether you want to enable upload directly by url(api), (*9)

    'enable_api_upload' => false,
    

    and upload to url by below, (*10)

    POST /upload/images/event/the-file-name
    
  5. and you might also want to set some middlewares to protect the upload route, (*11)

    'middlewares' => [],
    

Usage

Initialize File API

use \Unisharp\FileApi\FileApi;

$fa = new FileApi(); # use default path (as '/images/')
$fa_event = new FileApi('/images/event/'); # initialize it by giving a base path
$fa_article = new FileApi('/images/article/'); # initiate another instance

Save By Giving Uploaded File

  • Default Usage : get unique filename, (*12)

    $file = $fa->save(\Input::file('main_image')); // => wfj412.jpg
    
  • Custimize your upload file name, (*13)

    $file = $fa->save(\Input::file('main_image'), 'custom-file-name'); // => custom-file-name.jpg
    
  • By default will set three thumbs(equal scaling), (*14)

Thumbnail functions

  • Set custom thumb sizes, (*15)

    $file = $fa
        ->thumbs([
            'S' => '150x100',
            'M' => '300x200',
            'L' => '450x300'
            ])
        ->save(\Input::file('main_image'));
    
  • make cropped thumbs, (*16)

    $file = $fa->crop()->save(\Input::file('main_image'));
    

Get image url

// large size
$fa->get('wfj412.jpg');
$fa->get('wfj412.jpg', 'L');
$fa->get('wfj412.jpg', FileApi::SIZE_LARGE);

// medium size
$fa->get('wfj412.jpg', 'M');
$fa->get('wfj412.jpg', FileApi::SIZE_MEDIUM);

// full size
$fa->get('wfj412.jpg', 'full');
$fa->get('wfj412.jpg', FileApi::SIZE_ORIGINAL);

// comporssed
$fa->get('wfj412.jpg', 'CP'); // => get image url of compressed one

Delete image and thumbs

$fa->drop('wfj412.jpg');

Get file fullpath (abstract path from Laravel Storage)

$fa->getPath('wfj412.jpg'); // => '/images/event/wfj412.jpg'

Parse File Path to URL

if you store your file into cloud storage and you want to get url cloud site, you can use url() method to get it, (*17)

echo $fa->getUrl('wfjsdf.jpg'); // => "https://s3-ap-northeast-1.amazonaws.com/xxx/xxx/55c1e027caa62L.png"

Work with Laravel Storage

  • Get file content, (*18)

    \Storage::get($fa->getPath('wfj412.jpg'));
    
  • Write files, (*19)

    \Storage::put($fa->getPath('wfj412.jpg'));
    
  • Get Mime Type, (*20)

    \Storage::mimeType($fa->getPath('wfj412.jpg'));
    

Auto Upload

if enable_api_upload=true in config/fileapi.php, you can upload file to these two path, (*21)

  1. Image, (*22)

    • head, (*23)

       POST /api/v1/images/{target}/{param?}
    • body, (*24)

       image={file multipart body}
  2. Video, (*25)

    • head, (*26)

      /api/v1/videos/{target}/{param?}
    • body, (*27)

      video={file multipar body}

After uploaded

you add event listener to finish up after file uploaded, file api will fire image.{target}.created and video.{target}.created, (*28)

Step

  1. Write listener under App\Listeners, (*29)

    <?php
    
    namespace App\Listeners;
    
    class ArticleImageListener
    {
    
         public function handle($param, $filename, $path)
        {
            ... do something ...
        }
    }
  2. Write event mapping in Providers\EvnetService\Providers, (*30)

        protected $listen = [
            'image.article.created' => [
                'App\Listeners\ArticleImageListener'
            ],
        ];

Configurations

'enable_api_upload' => false, // auto upload api
'api_prefix' => '/api/v1',    // upload api url prefix
'middlewares' => [],          // middlewares that wrap the api upload route

The Versions

21/06 2018

dev-master

9999999-dev

Laravel File API - Handle Files with Laravel Storage

  Sources   Download

MIT

The Requires

 

laravel api file storage fileapi

14/11 2017

v1.0.2

1.0.2.0

Laravel File API - Handle Files with Laravel Storage

  Sources   Download

MIT

The Requires

 

laravel api file storage fileapi

13/11 2017

dev-develop

dev-develop

Laravel File API - Handle Files with Laravel Storage

  Sources   Download

MIT

The Requires

 

laravel api file storage fileapi

20/03 2017

dev-newdev

dev-newdev

Laravel File API - Handle Files with Laravel Storage

  Sources   Download

MIT

The Requires

 

laravel api file storage fileapi

13/10 2016

1.0.1

1.0.1.0

Laravel File API - Handle Files with Laravel Storage

  Sources   Download

MIT

The Requires

 

laravel api file storage fileapi

14/10 2015

1.0.0

1.0.0.0

Laravel File API - Handle Files with Laravel Storage

  Sources   Download

MIT

The Requires

 

laravel api file storage fileapi