konnco / laravel-imagecast
Make easier to store images into database
Fund package maintenance!
konnco
Installs: 3 703
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 0
Open Issues: 1
Requires
- php: ^7.4|^8.0|^9.0
- illuminate/contracts: ^9.0
- intervention/image: ^2.5
- kornrunner/blurhash: ^1.2
- spatie/laravel-package-tools: ^1.4.3
Requires (Dev)
- brianium/paratest: ^6.4
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- pestphp/pest-plugin-parallel: ^1.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
README
This package is designed to simplify our code for managing the laravel image.
Alpha Version
Attention! This package is still under development, we still looking the best pattern we can apply, and in the way, the development may break your application, we would not recommended you using to use this application on production until this package is fully released.
Installation
Laravel Version
You can install the package via composer:
composer require konnco/laravel-imagecast
You can publish the config file with:
php artisan vendor:publish --provider="Konnco\ImageCast\ImageCastServiceProvider" --tag="laravel-imagecast-config"
This is the contents of the published config file:
return [ 'disk' => env('IMAGECAST_DISK', 'public'), 'path' => 'images', 'blurhash' => env('IMAGECAST_BLURHASH', false) ];
Usage
Easy! just apply the Image
into the image type attribute, example :
import these file
use Konnco\ImageCast\Casts\Image;
and implement it like this
protected $casts = [ 'avatar' => Image::class, ];
Also you can applying custom configuration for each field, example :
protected $casts = [ 'avatar' => Image::class.":80,images/account/avatar,jpg", 'banner' => Image::class.":80,images/account/avatar,png", ];
with parameters :quality,savePath,extension
. For the savePath
variable you may want to insert random variable like date as the folder name, you can follow the example
protected $casts = [ 'avatar' => Image::class.":80,images/account/avatar/{date:Y-m-d},jpg", ];
After defining all of those configuration you can start uploading the image, example :
$user = New User(); $user->name = $request->name; $user->avatar = $request->photo; $user->save();
you can fill the avatar fields with all of these supported type :
- string - Path of the image in filesystem.
- string - URL of an image (allow_url_fopen must be enabled).
- string - Binary image data.
- string - Data-URL encoded image data.
- string - Base64 encoded image data.
- resource - PHP resource of type gd. (when using GD driver)
- object - Imagick instance (when using Imagick driver)
- object - Intervention\Image\Image instance
- object - SplFileInfo instance (To handle Laravel file uploads via Symfony\Component\HttpFoundation\File\UploadedFile)
Url Generator
With the ImageCast Url Generator you can define the image width and height only with the url, if you already get used with cloudinary, you will thank this package.
We should configure the 404 handler for Laravel. Open App\Exceptions\Handler
and and the code below inside the render
method.
use Konnco\ImageCast\ImageCastExceptionHandler; public function render($request, Throwable $exception) { return new ImageCastExceptionHandler($exception, request()->url(), function(){ return parent::render($request, $exception); }); }
We already added the helpers inside the ImageCast
and it can be defined like script below :
return $user->avatar->toUrl();
Base64
You can also convert your image to base64 image with this method
return $user->avatar->toBase64();
Idea
We really appreciate your idea and contribution into this package :)
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.