konnco/laravel-imagecast

Make easier to store images into database

Fund package maintenance!
konnco

0.1.1 2022-02-20 15:29 UTC

This package is auto-updated.

Last update: 2024-04-29 04:59:16 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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

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)
];

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.