{"id":13452953,"url":"https://github.com/qcod/laravel-imageup","last_synced_at":"2026-03-17T20:03:50.231Z","repository":{"id":33013034,"uuid":"149756198","full_name":"qcod/laravel-imageup","owner":"qcod","description":"Auto Image \u0026 file upload, resize and crop for Laravel eloquent model using Intervention image","archived":false,"fork":false,"pushed_at":"2025-05-15T03:56:48.000Z","size":118,"stargazers_count":773,"open_issues_count":7,"forks_count":62,"subscribers_count":18,"default_branch":"master","last_synced_at":"2025-12-14T23:10:48.102Z","etag":null,"topics":["eloquent","image","image-crop","image-resize","laravel","php","trait","upload"],"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/qcod.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2018-09-21T11:49:28.000Z","updated_at":"2025-12-07T01:20:23.000Z","dependencies_parsed_at":"2025-12-13T12:05:56.806Z","dependency_job_id":null,"html_url":"https://github.com/qcod/laravel-imageup","commit_stats":{"total_commits":63,"total_committers":10,"mean_commits":6.3,"dds":0.2857142857142857,"last_synced_commit":"2810400b154e24dcc74834a4ac7a3d201bf87dc8"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"purl":"pkg:github/qcod/laravel-imageup","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qcod%2Flaravel-imageup","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qcod%2Flaravel-imageup/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qcod%2Flaravel-imageup/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qcod%2Flaravel-imageup/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/qcod","download_url":"https://codeload.github.com/qcod/laravel-imageup/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qcod%2Flaravel-imageup/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30630034,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-17T17:32:55.572Z","status":"ssl_error","status_checked_at":"2026-03-17T17:32:38.732Z","response_time":56,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["eloquent","image","image-crop","image-resize","laravel","php","trait","upload"],"created_at":"2024-07-31T08:00:29.154Z","updated_at":"2026-03-17T20:03:50.207Z","avatar_url":"https://github.com/qcod.png","language":"PHP","funding_links":[],"categories":["Popular Packages","PHP","Paquetes utiles"],"sub_categories":[],"readme":"## Laravel ImageUp\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/qcod/laravel-imageup.svg)](https://packagist.org/packages/qcod/laravel-imageup)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE.md)\n[![Build Status](https://img.shields.io/travis/qcod/laravel-imageup/master.svg)](https://travis-ci.org/qcod/laravel-imageup)\n[![Total Downloads](https://img.shields.io/packagist/dt/qcod/laravel-imageup.svg)](https://packagist.org/packages/qcod/laravel-imageup)\n\nThe `qcod/laravel-imageup` is a trait which gives you auto upload, resize and crop for image feature with tons of customization.\n\n### Installation\n\nYou can install the package via composer:\n\n```bash\n$ composer require qcod/laravel-imageup\n```\n\nThe package will automatically register itself. In case you need to manually register it you can by adding it in `config/app.php` providers array:\n\n```php\nQCod\\ImageUp\\ImageUpServiceProvider::class\n```\n\nYou can optionally publish the config file with:\n\n```bash\nphp artisan vendor:publish --provider=\"QCod\\ImageUp\\ImageUpServiceProvider\" --tag=\"config\"\n```\n\nIt will create [`config/imageup.php`](#config-file) with all the settings.\n\n### Getting Started\n\nTo use this trait you just need to add `use HasImageUploads` on the Eloquent model and define all the fields which needs to store the images in database.\n\n**Model**\n```php\n\u003c?php\nnamespace App;\n\nuse QCod\\ImageUp\\HasImageUploads;\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass User extends Model {\n    use HasImageUploads;\n    \n    // assuming `users` table has 'cover', 'avatar' columns\n    // mark all the columns as image fields \n    protected static $imageFields = [\n        'cover', 'avatar'\n    ];\n}\n```\n\nOnce you marked all the image fields in model it will auto upload the image whenever you save the model by hooking in `Model::saved()` event. \nIt will also update the database with new stored file path and if finds old image it will be deleted once new image is uploaded.\n\n\u003e Image field should be as as `VARCHAR` in database table to store the path of uploaded image.\n\n**In Controller**\n```php\n\u003c?php\nnamespace App;\nuse App\\Http\\Controllers\\Controller;\n\nclass UserController extends Controller {\n    public function store(Request $request){\n        return User::create($request-\u003eall());\n    }\n}\n```\n\u003e Make sure to run `php artisan storage:link` to see the images from public storage disk\n\nThat's it, with above setup when ever you hit store method with post request and if `cover` or `avatar` named file is present on request() it will be auto uploaded.  \n\n## Upload Field options\n\nImageUp gives you tons of customization on how the upload and resize will be handled from defined field options, following are the things you can customize:\n\n```php\n\u003c?php\nnamespace App;\n\nuse QCod\\ImageUp\\HasImageUploads;\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass User extends Model {\n    \n    use HasImageUploads;\n    \n    // which disk to use for upload, can be override by field options \n    protected $imagesUploadDisk = 'local';\n    \n    // path in disk to use for upload, can be override by field options \n    protected $imagesUploadPath = 'uploads';\n    \n    // auto upload allowed \n    protected $autoUploadImages = true;\n    \n    // all the images fields for model\n    protected static $imageFields = [\n        'avatar' =\u003e [\n            // width to resize image after upload\n            'width' =\u003e 200,\n            \n            // height to resize image after upload\n            'height' =\u003e 100,\n            \n            // set true to crop image with the given width/height and you can also pass arr [x,y] coordinate for crop.\n            'crop' =\u003e true,\n            \n            // what disk you want to upload, default config('imageup.upload_disk')\n            'disk' =\u003e 'public',\n            \n            // a folder path on the above disk, default config('imageup.upload_directory')\n            'path' =\u003e 'avatars',\n            \n            // placeholder image if image field is empty\n            'placeholder' =\u003e '/images/avatar-placeholder.svg',\n            \n            // validation rules when uploading image\n            'rules' =\u003e 'image|max:2000',\n            \n            // override global auto upload setting coming from config('imageup.auto_upload_images')\n            'auto_upload' =\u003e false,\n            \n            // if request file is don't have same name, default will be the field name\n            'file_input' =\u003e 'photo',\n            \n            // if field (here \"avatar\") don't exist in database or you wan't this field in database\n            'update_database' =\u003e false,\n            \n            // a hook that is triggered before the image is saved\n            'before_save' =\u003e BlurFilter::class,\n            \n            // a hook that is triggered after the image is saved\n            'after_save' =\u003e CreateWatermarkImage::class\n        ],\n        'cover' =\u003e [\n            //...    \n        ]\n    ];\n    \n    // any other than image file type for upload\n    protected static $fileFields = [\n            'resume' =\u003e [\n                // what disk you want to upload, default config('imageup.upload_disk')\n                'disk' =\u003e 'public',\n                \n                // a folder path on the above disk, default config('imageup.upload_directory')\n                'path' =\u003e 'docs',\n                \n                // validation rules when uploading file\n                'rules' =\u003e 'mimes:doc,pdf,docx|max:1000',\n                \n                // override global auto upload setting coming from config('imageup.auto_upload_images')\n                'auto_upload' =\u003e false,\n                \n                // if request file is don't have same name, default will be the field name\n                'file_input' =\u003e 'cv',\n                \n                // a hook that is triggered before the file is saved\n                'before_save' =\u003e HookForBeforeSave::class,\n                \n                // a hook that is triggered after the file is saved\n                'after_save' =\u003e HookForAfterSave::class\n            ],\n            'cover_letter' =\u003e [\n                //...    \n            ]\n        ];\n}\n```\n### Customize filename\n\nIn some case you will need to customize the saved filename. By default it will be `$file-\u003ehashName()` generated hash.\n\nYou can do it by adding a method on the model with `{fieldName}UploadFilePath` naming convention:\n\n```php\nclass User extends Model {\n    use HasImageUploads;\n    \n    // assuming `users` table has 'cover', 'avatar' columns\n    // mark all the columns as image fields \n    protected static $imageFields = [\n        'cover', 'avatar'\n    ];\n    \n    // override cover file name\n    protected function coverUploadFilePath($file) {\n        return $this-\u003eid . '-cover-image.jpg';\n    }\n}\n```\n\nAbove will always save uploaded cover image as `uploads/1-cover-image.jpg`.\n\n\u003e Make sure to return only relative path from override method.\n \nRequest file will be passed as `$file` param in this method, so you can get the extension or original file name etc to build the filename.\n\n```php\n    // override cover file name\n    protected function coverUploadFilePath($file) {\n        return $this-\u003eid .'-'. $file-\u003egetClientOriginalName();\n    }\n    \n    /** Some of methods on file */\n    // $file-\u003egetClientOriginalExtension()\n    // $file-\u003egetRealPath()\n    // $file-\u003egetSize()\n    // $file-\u003egetMimeType()\n```\n\n## Available methods\n\nYou are not limited to use auto upload image feature only. This trait will give you following methods which you can use to manually upload and resize image.\n\n**Note:** Make sure you have disabled auto upload by setting `protected $autoUploadImages = false;` \non model or dynamiclly by calling `$model-\u003edisableAutoUpload()`. You can also disable it for specifig field by calling `$model-\u003esetImagesField(['cover' =\u003e ['auto_upload' =\u003e false]);`\notherwise you will be not seeing your manual uploads, since it will be overwritten by auto upload upon model save.\n\n#### $model-\u003euploadImage($imageFile, $field = null) / $model-\u003euploadFile($docFile, $field = null) \n\nUpload image/file for given $field, if $field is null it will upload to first image/file option defined in array.\n\n```php\n$user = User::findOrFail($id);\n$user-\u003euploadImage(request()-\u003efile('cover'), 'cover');\n$user-\u003euploadFile(request()-\u003efile('resume'), 'resume');\n```\n\n#### $model-\u003esetImagesField($fieldsOptions) / $model-\u003esetFilesField($fieldsOptions) \n\nYou can also set the image/file fields dynamically by calling `$model-\u003esetImagesField($fieldsOptions) / $model-\u003esetFilesField($fieldsOptions)` with field options, it will replace fields defined on model property.\n\n```php\n$user = User::findOrFail($id);\n\n$fieldOptions = [\n    'cover' =\u003e [ 'width' =\u003e 1000 ],\n    'avatar' =\u003e [ 'width' =\u003e 120, 'crop' =\u003e true ],    \n];\n\n// override image fields defined on  model \n$user-\u003esetImagesField($fieldOptions);\n\n$fileFieldOption = [\n    'resume' =\u003e ['path' =\u003e 'resumes']\n];\n\n// override file fields defined on  model\n$user-\u003esetFilesField($fileFieldOption);\n```\n\n#### $model-\u003ehasImageField($field) / $model-\u003ehasFileField($field)\n\nTo check if field is defined as image/file field.\n\n#### $model-\u003edeleteImage($filePath) / $model-\u003edeleteFile($filePath) \n\nDelete any image/file if it exists.\n\n#### $model-\u003eresizeImage($imageFile, $fieldOptions)\n\nIf you have image already you can call this method to resize it with the same options we have used for image fields.\n\n```php\n$user = User::findOrFail($id);\n\n// resize image, it will give you resized image, you need to save it  \n$imageFile = '/images/some-big-image.jpg';\n$image = $user-\u003eresizeImage($imageFile, [ 'width' =\u003e 120, 'crop' =\u003e true ]);\n\n// or you can use uploaded file\n$imageFile = request()-\u003efile('avatar');\n$image = $user-\u003eresizeImage($imageFile, [ 'width' =\u003e 120, 'crop' =\u003e true ]);\n```\n\n#### $model-\u003ecropTo($x, $y)-\u003eresizeImage($imageFile, $field = null)\n\nYou can use this `cropTo()` method to set the x and y coordinates of cropping. It will be very useful if you are getting coordinate from some sort of font-end image cropping library.\n\n```php\n$user = User::findOrFail($id);\n\n// uploaded file from request\n$imageFile = request()-\u003efile('avatar');\n\n// coordinates from request\n$coords = request()-\u003eonly(['crop_x', 'crop_y']);\n\n// resizing will give you intervention image back\n$image = $user-\u003ecropTo($coords)\n    -\u003eresizeImage($imageFile, [ 'width' =\u003e 120, 'crop' =\u003e true ]);\n\n// or you can do upload and resize like this, it will override field options crop setting\n$user-\u003ecropTo($coords)\n    -\u003euploadImage(request()-\u003efile('cover'), 'avatar');\n```\n\n#### $model-\u003eimageUrl($field) / $model-\u003efileUrl($field)\n\nGives uploaded file url for given image/file field.\n\n```php\n$user = User::findOrFail($id);\n\n// in your view \n\u003cimg src=\"{{ $user-\u003eimageUrl('cover') }}\" alt=\"\" /\u003e\n// http://www.example.com/storage/uploads/iGqUEbCPTv7EuqkndE34CNitlJbFhuxEWmgN9JIh.jpeg\n```\n\n#### $model-\u003eimageTag($field, $attribute = '')\n\nIt gives you `\u003cimg /\u003e` tag for a field.\n\n```html\n{!! $model-\u003eimageTag('avatar') !!}\n\u003c!-- \u003cimg src=\"http://www.example.com/storage/uploads/iGqUEbCPTv7EuqkndE34CNitlJbFhuxEWmgN9JIh.jpeg\" /\u003e --\u003e\n\n{!! $model-\u003eimageTag('avatar', 'class=\"float-left mr-3\"') !!}\n\u003c!-- \u003cimg src=\"http://www.example.com/storage/uploads/iGqUEbCPTv7EuqkndE34CNitlJbFhuxEWmgN9JIh.jpeg\" class=\"float-left mr-3 /\u003e --\u003e\n```\n\n### Hooks\nHooks allow you to apply different type of customizations or any other logic that you want to take place before or after the image is saved.\n\n##### Definition types\nYou can define hooks by specifying a class name\n\n```php\nprotected static $imageFields = [\n    'avatar' =\u003e [\n        'before_save' =\u003e BlurFilter::class,\n    ],\n    'cover' =\u003e [\n        //...    \n    ]\n];\n```\n\nThe hook class must have a method named `handle` that will be called when the hook is triggered.\nAn instance of the intervention image will be passed to the `handle` method.\n\n```php\nclass BlurFilter {\n    public function handle($image) {\n        $image-\u003eblur(10);\n    }\n}\n```\n\nThe class based hooks are resolved through laravel ioc container, which allows you to inject any dependencies through the constructor.\n\n\u003e Keep in mind you will be getting resized image in `before` and `after` save hook handler if you have defined field option with `width` or `height`. \nSure you can get original image from `request()-\u003efile('avatar')` any time you want. \n\nThe second type off hook definition is callback hooks.\n```php\n$user-\u003esetImagesField([\n    'avatar' =\u003e [\n        'before_save' =\u003e function($image) {\n            $image-\u003eblur(10);\n        },\n    ],\n    'cover' =\u003e [\n        //...    \n    ]\n]);\n```\n\nThe callback will receive the intervention image instance argument as well.\n\n##### Hook types\nThere are two types of hooks a `before_save` and `after_save` hooks.\n\nThe `before_save` hook is called just before the image is saved to the disk.\nAny changes made to the intervention image instance within the hook will be applied to the output image.\n\n```php\n$user-\u003esetImagesField([\n    'avatar' =\u003e [\n        'width' =\u003e 100,\n        'height' =\u003e 100,\n        'before_save' =\u003e function($image) {\n            // The image will be 50 * 50, this will override the 100 * 100 \n            $image-\u003eresize(50, 50);\n        },\n    ]\n]);\n```\n\nThe `after_save` hook is called right after the image was saved to the disk.\n\n```php\n$user-\u003esetImagesField([\n    'logo' =\u003e [\n        'after_save' =\u003e function($image) {\n            // Create a watermark image and save it\n        },\n    ]\n]);\n```\n\n### Config file\n\n```php\n\u003c?php\n\nreturn [\n\n    /**\n     * Default upload storage disk\n     */\n    'upload_disk' =\u003e 'public',\n\n    /**\n     * Default Image upload directory on the disc\n     * eg. 'uploads' or 'user/avatar'\n     */\n    'upload_directory' =\u003e 'uploads',\n\n    /**\n     * Auto upload images from incoming Request if same named field or\n     * file_input field on option present upon model update and create.\n     * can be override in individual field options\n     */\n    'auto_upload_images' =\u003e true,\n\n    /**\n     * It will auto delete images once record is deleted from database\n     */\n    'auto_delete_images' =\u003e true,\n\n    /**\n     * Set an image quality\n     */\n    'resize_image_quality' =\u003e 80\n];\n```\n\n### Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n### Testing\nThe package contains some integration/smoke tests, set up with Orchestra. The tests can be run via phpunit.\n\n```bash\n$ composer test\n```\n\n### Contributing\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n### Security\n\nIf you discover any security related issues, please email saquibweb@gmail.com instead of using the issue tracker.\n\n### Credits\n- [Mohd Saqueib Ansari](https://github.com/saqueib)\n- [Melek Rebai aka shadoWalker89](https://github.com/shadoWalker89)\n- [João Roberto P. Borges](https://github.com/joaorobertopb)\n\n### About QCode.in\nQCode.in (https://www.qcode.in) is blog by [Saqueib](https://github.com/saqueib) which covers All about Full Stack Web Development.\n\n### License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqcod%2Flaravel-imageup","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqcod%2Flaravel-imageup","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqcod%2Flaravel-imageup/lists"}