gnahotelsolutions/image-cacher

Creates a resized or cropped version of the image in the cache directory

2.6.0 2024-10-22 13:37 UTC

README

Latest Version on Packagist Build status Quality Score Total Downloads

With this package you can create smaller versions of the same image to optimize user's transfer data. What's the point of downloading a 3000x3000 image on a 200x200 thumbnail? This package is very helpful when dealing with responsive images

Installation

You can install the package via composer:

composer require gnahotelsolutions/image-cacher

Usage

use GNAHotelSolutions\ImageCacher\Cacher;
use GNAHotelSolutions\ImageCacher\Image;

// Image located in public/img/hotel/rooms/double-room.jpg
$image = new Image('hotel/rooms/double-room.jpg', 'img');

$resized = (new Cacher())->resize($image, 1920, 1080); // Get a smaller version of the image or the same if the size is smaller.

$cropped = (new Cacher())->crop($image, 1920, 1080); // Get a cropped version of the image.

Generating a thumbnail

<?php 
$cacher = new Cacher();
$image = new Image('hotel/rooms/double-room.jpg', 'img'); 
?>

<img src="<?= $cacher->crop($image, 200, 200)->getOriginalName(); ?>" class="thumbnail">

Combining with srcset attribute

<?php 
$cacher = new Cacher();
$image = new Image('hotel/rooms/double-room.jpg', 'img'); 
?>

<img src="<?= $cacher->crop($image, 800, 600)->getOriginalName(); ?>"
     srcset="<?= $cacher->crop($image, 400, 280)->getOriginalName(); ?> 400w,
             <?= $cacher->crop($image, 800, 600)->getOriginalName(); ?> 800w"
     sizes="(max-width: 480px) 400px), 800px"
>

Using with Laravel

The package comes with a Facade you can use on your Laravel projects. Use the configuration file to tell where the cache path and the images root path are.

First of all, publish the package configuration file:

php artisan vendor:publish --provider="GNAHotelSolutions\ImageCacher\Adapters\Laravel\ImageCacherServiceProvider"

Adapt the config/image-cacher.php file variables to your needs. Default ones works well with a standard Laravel app.

<img src="{{ ImageCacher::crop('hotel/rooms/double-room.jpg', 800, 600)->getOriginalName() }}">

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email dllop@gnahs.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.