This package is designed to simplify our code for managing the laravel image.
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.
Tags | Laravel Version |
---|---|
0.0.7 | Laravel 8 |
0.1.0 | Laravel 9 |
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)
];
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)
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();
You can also convert your image to base64 image with this method
return $user->avatar->toBase64();
We really appreciate your idea and contribution into this package :)
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.