unisharp/laravel-fileapi 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

unisharp/laravel-fileapi

Composer 安装命令:

composer require unisharp/laravel-fileapi

包简介

Laravel File API - Handle Files with Laravel Storage

README 文档

README

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

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

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

    php artisan vendor:publish --tag=fileapi_config

Config

in config/fileapi.php

  1. fill in the storage path, which make routes for you.

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

    it will generate routes like below :

    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)

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

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

    'enable_api_upload' => false,

    and upload to url by below

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

    '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

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

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

Thumbnail functions

  • Set custom thumb sizes

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

    $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

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

Work with Laravel Storage

  • Get file content

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

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

    \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

  1. Image

    • head

         POST /api/v1/images/{target}/{param?}
      
    • body

         image={file multipart body}
      
  2. Video

    • head

        /api/v1/videos/{target}/{param?}
      
    • body

        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

Step

  1. Write listener under App\Listeners

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

         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

unisharp/laravel-fileapi 适用场景与选型建议

unisharp/laravel-fileapi 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 45.47k 次下载、GitHub Stars 达 52, 最近一次更新时间为 2015 年 08 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「file」 「api」 「storage」 「laravel」 「fileapi」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 unisharp/laravel-fileapi 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 unisharp/laravel-fileapi 我们能提供哪些服务?
定制开发 / 二次开发

基于 unisharp/laravel-fileapi 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 45.47k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 53
  • 点击次数: 13
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 52
  • Watchers: 9
  • Forks: 14
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-08-10