mr-timofey / laravel-aio-images
All-in-one Laravel image processing
Installs: 4 120
Dependents: 1
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 1
Open Issues: 0
pkg:composer/mr-timofey/laravel-aio-images
Requires
- php: >=7.1.0
- intervention/image: ^2.4
- spatie/image-optimizer: ^1.0
README
This package includes the following:
- images database table migration;
- images Eloquent model;
- controller for uploads and on-the-fly/on-demand image processing/caching;
- service provider.
Any uploaded or generated image is automatically optimized using the spatie/image-optimizer package.
On-the-fly image generation just uses intervention/image package.
Requirements
- PHP 7.1
- Laravel or Lumen 5
Installation
sudo apt-get install pngquant gifsicle jpegoptim optipng composer require mr-timofey/laravel-aio-images
Laravel
php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5" php artisan vendor:publish --provider="MrTimofey\LaravelAioImages\ServiceProvider" php artisan migrate
Migration will create the aio_images table which contains a json field props.
You can change it to text if your database does not support JSON/JSONB fields.
Although it is not recommended.
If you want to use storage/app/public as a place to store all your images (configured by default):
php artisan storage:link
For Laravel <= 5.4 add
Intervention\Image\ImageServiceProvider,
MrTimofey\LaravelAioImages\ServiceProvider
to your app.providers config.
See config/aio_images.php file for a further configuration instructions.
Do not forget to configure aio_images.pipes!
Lumen
Add Intervention\Image\ImageServiceProviderLumen, MrTimofey\LaravelAioImages\ServiceProvider
service providers to bootstrap/app.php.
Copy contents of
config.php
to config/aio_images.php.
Create a migration php artisan make:migration create_aio_images_table and copy contents
from here
to the just created migration file (database/migrations/xxxx_xx_xx_xxxxxx_create_aio_images_table).
See config/aio_images.php file for a further configuration instructions.
Do not forget to configure aio_images.pipes!
Predefined routes
route('aio_images.upload'),POST multipart/form-data- image uploads handler. Both multiple and single image uploads are supported. Field names does not matter since the controller just usesIlluminate\Http\Request@allFiles()to get your uploads.route('aio_images.original', $image_id)- original image path.route('aio_images.pipe', [$pipe, $image_id])- processed image path.
Usage example:
// add relation to a table /** @var Illuminate\Database\Schema\Blueprint $table */ $table->string('avatar_image_id')->nullable(); $table->foreign('avatar_image_id') ->references('id') ->on('aio_images') ->onDelete('set null'); // add relation to a model public function avatarImage() { $model->belongsTo(ImageModel::class, 'avatar_image_id'); } // create pipe config in config/aio_images.php [ // ... 'pipes' => [ // /storage/images/avatar/image-id.jpg 'avatar' => [ // $interventionImage->fit(120) ['fit', 120], // $interventionImage->greyscale() ['greyscale'] ] ] ]; // display original avatar echo '<img src="' . route('aio_images.original', ['image_id' => $model->avatar_image_id]) . '" alt="Original avatar" />'; // display 120x120 squared grey colored avatar echo '<img src="' . route('aio_images.pipe', ['pipe' => 'avatar', 'image_id' => $model->avatar_image_id]) . '" alt="Processed with pipe [avatar]" />'; // same with ImageModel instance echo '<img src="' . $image->getPath() . '" alt="Original avatar" />'; echo '<img src="' . $image->getPath('avatar') . '" alt="Processed with pipe [avatar]" />'; // upload image manually from any of your custom controllers use Illuminate\Http\Request; use MrTimofey\LaravelAioImages\ImageModel; function upload(Request $req) { return ImageModel::upload($req->file('image')); }