{"id":19706703,"url":"https://github.com/unisharp/laravel-fileapi","last_synced_at":"2025-04-29T16:33:35.832Z","repository":{"id":35705349,"uuid":"39982883","full_name":"UniSharp/laravel-fileapi","owner":"UniSharp","description":"Laravel File API with image thumbnail support","archived":false,"fork":false,"pushed_at":"2022-01-03T03:41:58.000Z","size":70,"stargazers_count":52,"open_issues_count":5,"forks_count":16,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-04-25T17:59:51.956Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/UniSharp.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-07-31T02:38:38.000Z","updated_at":"2024-11-14T15:03:13.000Z","dependencies_parsed_at":"2022-08-18T01:21:15.735Z","dependency_job_id":null,"html_url":"https://github.com/UniSharp/laravel-fileapi","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UniSharp%2Flaravel-fileapi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UniSharp%2Flaravel-fileapi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UniSharp%2Flaravel-fileapi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UniSharp%2Flaravel-fileapi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/UniSharp","download_url":"https://codeload.github.com/UniSharp/laravel-fileapi/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251540539,"owners_count":21605927,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T21:36:39.117Z","updated_at":"2025-04-29T16:33:30.816Z","avatar_url":"https://github.com/UniSharp.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel File API\n\n## Features\n\n * Handle files with Laravel Storage.\n * Load files through Laravel routing instead of public path.\n * Save images with thumbs, compressed image, and sizes are customisable.\n\n## Installation\n\n1. Install File API\n\n    ```php\n    composer require unisharp/laravel-fileapi\n    ```\n\n1. Set service provider in `config/app.php`\n\n    ```php\n    Unisharp\\FileApi\\FileApiServiceProvider::class,\n    ```\n\n1. publish config file\n\n    ```php\n    php artisan vendor:publish --tag=fileapi_config\n    ```\n\n## Config\n\nin `config/fileapi.php`\n\n1. fill in the storage path, which make routes for you.\n\n    ```php\n    'path' =\u003e ['/images/event/', '/images/article/'],\n    ```\n\n    it will generate routes like below :\n\n    ```php\n    Route::get('/images/event/{filename}', function ($filename) {\n        $entry = new \\Unisharp\\FileApi\\FileApi('/images/event/');\n        return $entry-\u003egetResponse($filename);\n    });\n\n    Route::get('/images/article/{filename}', function ($filename) {\n        $entry = new \\Unisharp\\FileApi\\FileApi('/images/article/');\n        return $entry-\u003egetResponse($filename);\n    });\n    ```\n\n1. set default thumb sizes(by key and value)\n\n    ```php\n    'default_thumbs' =\u003e ['S' =\u003e '96x96', 'M' =\u003e '256x256', 'L' =\u003e '480x480'],\n    ```\n\n1. set default image compress quality\n\n    ```php\n    'compress_quality' =\u003e 90,\n    ```\n\n1. choose whether you want to enable upload directly by url(api)\n\n    ```php\n    'enable_api_upload' =\u003e false,\n    ```\n\n    and upload to url by below\n\n    ```\n    POST /upload/images/event/the-file-name\n    ```\n\n1. and you might also want to set some middlewares to protect the upload route\n\n    ```php\n    'middlewares' =\u003e [],\n    ```\n    \n## Usage\n\n### Initialize File API\n\n```php\nuse \\Unisharp\\FileApi\\FileApi;\n    \n$fa = new FileApi(); # use default path (as '/images/')\n$fa_event = new FileApi('/images/event/'); # initialize it by giving a base path\n$fa_article = new FileApi('/images/article/'); # initiate another instance\n```\n\n### Save By Giving Uploaded File\n\n* Default Usage : get unique filename\n\n    ```php\n    $file = $fa-\u003esave(\\Input::file('main_image')); // =\u003e wfj412.jpg\n    ```\n    \n* Custimize your upload file name\n\n    ```php\n    $file = $fa-\u003esave(\\Input::file('main_image'), 'custom-file-name'); // =\u003e custom-file-name.jpg\n    ```\n    \n* By default will set three thumbs(equal scaling)\n\n### Thumbnail functions\n\n* Set custom thumb sizes\n\n    ```php\n    $file = $fa\n        -\u003ethumbs([\n            'S' =\u003e '150x100',\n            'M' =\u003e '300x200',\n            'L' =\u003e '450x300'\n            ])\n        -\u003esave(\\Input::file('main_image'));\n    ```\n\n* make cropped thumbs\n        \n    ```php\n    $file = $fa-\u003ecrop()-\u003esave(\\Input::file('main_image'));\n    ```\n\n### Get image url\n\n```php\n// large size\n$fa-\u003eget('wfj412.jpg');\n$fa-\u003eget('wfj412.jpg', 'L');\n$fa-\u003eget('wfj412.jpg', FileApi::SIZE_LARGE);\n\n// medium size\n$fa-\u003eget('wfj412.jpg', 'M');\n$fa-\u003eget('wfj412.jpg', FileApi::SIZE_MEDIUM);\n\n// full size\n$fa-\u003eget('wfj412.jpg', 'full');\n$fa-\u003eget('wfj412.jpg', FileApi::SIZE_ORIGINAL);\n\n// comporssed\n$fa-\u003eget('wfj412.jpg', 'CP'); // =\u003e get image url of compressed one\n```\n    \n### Delete image and thumbs\n\n```php\n$fa-\u003edrop('wfj412.jpg');\n```\n\n### Get file fullpath (abstract path from Laravel Storage)\n\n```php\n$fa-\u003egetPath('wfj412.jpg'); // =\u003e '/images/event/wfj412.jpg'\n```  \n    \n### Parse File Path to URL\nif you store your file into cloud storage and you want to get url cloud site, you can use url() method to get it\n\n```php\necho $fa-\u003egetUrl('wfjsdf.jpg'); // =\u003e \"https://s3-ap-northeast-1.amazonaws.com/xxx/xxx/55c1e027caa62L.png\"\n```\n    \n### Work with Laravel Storage\n\n* Get file content\n\n    ```php\n    \\Storage::get($fa-\u003egetPath('wfj412.jpg'));\n    ```\n        \n* Write files\n\n    ```php\n    \\Storage::put($fa-\u003egetPath('wfj412.jpg'));\n    ```\n        \n* Get Mime Type\n\n    ```php\n    \\Storage::mimeType($fa-\u003egetPath('wfj412.jpg'));\n    ```\n## Auto Upload\n\nif `enable_api_upload=true` in `config/fileapi.php`, you can upload file to these two path\n\n1. Image\n\n    * head\n \n             POST /api/v1/images/{target}/{param?}\n    * body\n\n             image={file multipart body}\n  \n2. Video\n\n    * head\n\n            /api/v1/videos/{target}/{param?}\n\n    * body\n\n            video={file multipar body}\n  \n\n### After uploaded\n\nyou add event listener to finish up after file uploaded, file api will fire `image.{target}.created` and\n`video.{target}.created`\n\n\n#### Step\n\n1. Write listener under `App\\Listeners`\n\n        \u003c?php\n\n        namespace App\\Listeners;\n\n        class ArticleImageListener\n        {\n\n             public function handle($param, $filename, $path)\n            {\n                ... do something ...\n            }\n        }\n        \n2. Write event mapping in `Providers\\EvnetService\\Providers`\n\n            protected $listen = [\n                'image.article.created' =\u003e [\n                    'App\\Listeners\\ArticleImageListener'\n                ],\n            ];\n\n### Configurations\n\n    'enable_api_upload' =\u003e false, // auto upload api\n    'api_prefix' =\u003e '/api/v1',    // upload api url prefix\n    'middlewares' =\u003e [],          // middlewares that wrap the api upload route\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Funisharp%2Flaravel-fileapi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Funisharp%2Flaravel-fileapi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Funisharp%2Flaravel-fileapi/lists"}