Download the PHP package lidai/laravel-imageup without Composer
On this page you can find all versions of the php package lidai/laravel-imageup. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download lidai/laravel-imageup
More information about lidai/laravel-imageup
Files in lidai/laravel-imageup
Package laravel-imageup
Short Description Auto Image upload, resize and crop for Laravel eloquent model using Intervention image
License MIT
Homepage https://github.com/lidaijfm/laravel-imageup
Informations about the package laravel-imageup
Laravel ImageUp
The lidai/laravel-imageup
is a trait which gives you auto upload, resize and crop for image feature with tons of customization.
Installation
You can install the package via composer:
The package will automatically register itself. In case you need to manually register it you can by adding it in config/app.php
providers array:
You can optionally publish the config file with:
It will create config/imageup.php
with all the settings.
Getting Started
To 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.
Model
Once 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.
It will also update the database with new stored file path and if finds old image it will be deleted once new image is uploaded.
Image field should be as as
VARCHAR
in database table to store the path of uploaded image.
In Controller
Make sure to run
php artisan storage:link
to see the images from public storage disk
That'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.
Upload Field options
ImageUp 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:
Customize filename
In some case you will need to customize the saved filename. By default it will be $file->hashName()
generated hash.
You can do it by adding a method on the model with {fieldName}UploadFilePath
naming convention:
Above will always save uploaded cover image as uploads/1-cover-image.jpg
.
Make sure to return only relative path from override method.
Request 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.
Available methods
You 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.
Note: Make sure you have disabled auto upload by setting protected $autoUploadImages = false;
on model or dynamiclly by calling $model->disableAutoUpload()
. You can also disable it for specifig field by calling $model->setImagesField(['cover' => ['auto_upload' => false]);
otherwise you will be not seeing your manual uploads, since it will be overwritten by auto upload upon model save.
$model->uploadImage($imageFile, $field = null) / $model->uploadFile($docFile, $field = null)
Upload image/file for given $field, if $field is null it will upload to first image/file option defined in array.
$model->setImagesField($fieldsOptions) / $model->setFilesField($fieldsOptions)
You can also set the image/file fields dynamically by calling $model->setImagesField($fieldsOptions) / $model->setFilesField($fieldsOptions)
with field options, it will replace fields defined on model property.
$model->hasImageField($field) / $model->hasFileField($field)
To check if field is defined as image/file field.
$model->deleteImage($filePath) / $model->deleteFile($filePath)
Delete any image/file if it exists.
$model->resizeImage($imageFile, $fieldOptions)
If you have image already you can call this method to resize it with the same options we have used for image fields.
$model->cropTo($x, $y)->resizeImage($imageFile, $field = null)
You 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.
$model->imageUrl($field) / $model->fileUrl($field)
Gives uploaded file url for given image/file field.
$model->imageTag($field, $attribute = '')
It gives you <img />
tag for a field.
Hooks
Hooks 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.
Definition types
You can define hooks by specifying a class name
The hook class must have a method named handle
that will be called when the hook is triggered.
An instance of the intervention image will be passed to the handle
method.
The class based hooks are resolved through laravel ioc container, which allows you to inject any dependencies through the constructor.
Keep in mind you will be getting resized image in
before
andafter
save hook handler if you have defined field option withwidth
orheight
. Sure you can get original image fromrequest()->file('avatar')
any time you want.
The second type off hook definition is callback hooks.
The callback will receive the intervention image instance argument as well.
Hook types
There are two types of hooks a before_save
and after_save
hooks.
The before_save
hook is called just before the image is saved to the disk.
Any changes made to the intervention image instance within the hook will be applied to the output image.
The after_save
hook is called right after the image was saved to the disk.
Config file
Changelog
Please see CHANGELOG for more information on what has changed recently.
Testing
The package contains some integration/smoke tests, set up with Orchestra. The tests can be run via phpunit.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-imageup with dependencies
laravel/framework Version ~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
intervention/image Version ^2.7