A simple wrapper for PHP Intervention Library to provide a more simple interface and convenient way to convert images to webp

Overview

Laravel webp

Latest Version on Packagist

GitHub Code Style Action Status Total Downloads


This package is a simple wrapper for PHP Intervention Library to provide a more simple interface and convenient way to convert images to webp - next generation format - extension, and resize them to render only needed sizes.

Installation

You can install the package via composer:

composer require eyadhamza/laravel-webp

You can publish the config file with:

php artisan vendor:publish --provider="EyadHamza\LaravelWebp\LaravelWebpServiceProvider" --tag="webp-config"

This is the contents of the published config file:

the values represent the default values that will be used to convert the image, you can set the values of height and width to the values you would like to have in the converted image.

return [
    'quality' => 70,
    'height' => null,
    'width' => null
];

Usage

Converting Images:

To use the package on a given Eloquent Model you should use HandleWebpConversion Trait

class TestModel extends Model
{
    use HandleWebpConversion;
}

The Package will look for a protected property named $imageFields that will be used by default to change the attributes value in the database when saving the model.

class TestModel extends Model
{
    use HandleWebpConversion;
    protected $fillable = [
        'image',
        'avatar'
    ];

    protected array $imageFields = [
        'image',
        'avatar'
    ];
}

Now after you prepared your model, you can call the saveImageAsWebp() method on any model object that has the url of the image.

$testModel->saveImageAsWebp();

The saveImageAsWebp() method will do the following:

1- Convert The image on disk to webp

2- keep the old image

3- change the attributes defined in $imageFields in the database to point to the new image path with the new extension

4- Create a log entry in the laravel.log file with the details of the operation and the before and after size e.g:

Image: public/test.jpg Before: 0.3126 after: 0.0460 Percentage: 85.27

What if you want to delete the old image?

$testModel->overwriteImageAsWebp();

The overwriteImageAsWebp() method will do just like the previous, but it will delete the old image from disk


Resizing Images

If you tried Lighthouse to test your application, you may have got a suggestion that the rendered image is actually smaller than the image sent by the server.

A solution to that will be using a controller that changes the size on demand.

But that solution is not the best solution as image processing is an intensive operation for the CPU resources, you don't want to have your page resize everytime your page reloads.

So, a better approach is to do that task one time and save another resized image on disk.

And It's Simple Enough!

Let's say that you want to resize the image to 400x400, the image here is stored in a column named "image" in the database, you can pass any column that contains a valid absolute url of an image.

$testModel->resize('image', 400, 400);

The resize('image', $width, $height) method will do the following:

1- will look for the value at "image" with the given model object

2- Save a version of the image on disk to webp with the new dimension with a new name that ends with _400x400.webp

2- Return the Full Path of the image, so you can use it somewhere in your views.

Optimize Existing Images:

What if you already have images on your local storage that are not optimized, and surely those images' path are also stored in the database.

It will be kinda hard to do that manually and convert each one by hand!

The Solution is Here as an Artisan Command:

First we will modify files on disk then we will run another command to modify the database attributes - defined in $imageFields - to point to the new location.

php artisan public:to-webp

This will convert images in the public directory, you can specify a file using the directory option

php artisan public:to-webp directory = 'public/images'

Note that the command will keep the old images, if you want to delete the old images you can pass --overwrite to the command

php artisan public:to-webp --overwrite

If you would like to optimize the files in your static assets you can pass --assets

php artisan public:to-webp --assets

Now to modify the database attribute value to point to the new webp image we run the command, and we pass the name of the Eloquent model like this - this assumes that you have your models under App\Models - alternatively you may pass the full class name.

php artisan images:to-webp Post

The previous command will look for the protected property $imageField in your model.

Alternatively, you can pass the name of the attribute as will.

php artisan images:to-webp User avatar

If you want to use more methods that conveniently change your image path you may refer to the ImageToWebpService class

You can also call the methods directly using the Facade ImageToWebp

Testing

Warning: Tests Are Potentially Destructive

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.

You might also like...
Online All in One Video & Audio Downloader From YouTube,Facebook,Twitter,Pinterest,Instagram,MXtakatak,IPL, Tiktok and 1000+ More Sites too

DLhut Contact me If You Find ANy Bug ... PHP Search and Download any Videos from any site. Online All in One Video & Audio Downloader From YouTube,Fac

3DS Town Square (3DSTS) is a website built and designed for the Nintendo 3DS. Also see GitHub pages for more info.

3DSTownSquare 3DS Town Square (3DSTS) is a website built and designed for the Nintendo 3DS. Supported PHP versions The only tested versions is 7.4.29,

A web interface for MySQL and MariaDB

phpMyAdmin A web interface for MySQL and MariaDB. https://www.phpmyadmin.net/ Code status Download You can get the newest release at https://www.phpmy

A unified front-end for different queuing backends. Includes a REST server, CLI interface and daemon runners.

PHP-Queue A unified front-end for different queuing backends. Includes a REST server, CLI interface and daemon runners. Why PHP-Queue? Implementing a

 amadeus-ws-client: PHP client for the Amadeus GDS SOAP Web Service interface
amadeus-ws-client: PHP client for the Amadeus GDS SOAP Web Service interface

amadeus-ws-client: PHP client for the Amadeus GDS SOAP Web Service interface This client library provides access to the Amadeus GDS SOAP Web Service i

The API & platform builder, build your apps 10x faster even more, it's open source & 100% free !
The API & platform builder, build your apps 10x faster even more, it's open source & 100% free !

The API & platform builder, build your apps 10x faster, even more. It's open source & 100% free ! Try live demo Why badaso ? 100% FREE - No need for e

A full-featured home hosted Cloud Drive, Personal Assistant, App Launcher, File Converter, Streamer, Share Tool & More!
A full-featured home hosted Cloud Drive, Personal Assistant, App Launcher, File Converter, Streamer, Share Tool & More!

A Fully Featured home-hosted Cloud Storage platform and Personal Assistant that Converts files, OCR's images & documents, Creates archives, Scans for viruses, Protects your server, Keeps itself up-to-date, and Runs your own AppLauncher!

Now Introducing a new and easy way to manage your clients and MyOwnFreeHost hosting accounts.

Warning This is a beta version of Xera. Use it for testing purpose only. You are be responsible for any loss or damages that may occor from using this

Switch the DokuWiki interface language according to the accept-language request header

Switch the DokuWiki interface language according to the accept-language request header

Releases(v1.3.5)
Owner
eyad hamza
FCIS Bio Department Student, Backend Developer
eyad hamza
BicBucStriim streams books, digital books. It fills a gap in the functionality of current NAS devices that provide access to music, videos and photos

BicBucStriim streams books, digital books. It fills a gap in the functionality of current NAS devices that provide access to music, videos and photos -- but not books. BicBucStriim fills this gap and provides web-based access to your e-book collection.

Rainer Volz 392 Dec 31, 2022
Provide modular structure into your symfony application.

Symfony Modular Bundle Provide modular structure into your symfony application. Features Configure doctrine Entity/Document mapping. Load services in

Anthonius Munthi 2 Sep 8, 2021
Uploader is a set of small classes for sending images, files, and media received by a form of your application

Uploader is a set of small classes for sending images, files, and media received by a form of your application. The Uploader handles, validates and sends the files to your server. Image class can still handle sizes with the gd library.

null 5 Dec 22, 2022
Provides access to Cloudflare Images API for Laravel projects

Cloudflare Images Provides access to Cloudflare Images API for Laravel projects Table of contents Installation Configuration Using Installation To get

Dmytro 20 Dec 16, 2022
SPFtoolbox is a Javascript and PHP app to look up DNS records such as SPF, MX, Whois, and more

SPFtoolbox is a Javascript and PHP app to look up DNS records such as SPF, MX, Whois, and more

Charles Barnes 216 Dec 30, 2022
Simple-podcast-generator - 👉 A very simple way to host your podcast files and generate the RSS Podcast feed 🎙

Podcast RSS Feed Generator A very simple way to host your podcast files and generate the RSS Podcast feed ?? ?? Contents Summary Requirements Installa

♚ PH⑦ de Soria™♛ 11 Dec 2, 2022
Simple and lightweight OOP wrapper for PHP's low-level sockets extension (ext-sockets)

clue/socket-raw Simple and lightweight OOP wrapper for PHP's low-level sockets extension (ext-sockets). PHP offers two networking APIs, the newer stre

Christian Lück 321 Dec 15, 2022
Online All in One PHP Video & Audio Downloader From YouTube,Facebook,Twitter,Pinterest,Instagram,MXtakatak,IPL, Tiktok and 1000+ More Sites too

DLhut Contact me If You Find ANy Bug ... PHP Search and Download any Videos from any site. Online All in One Video & Audio Downloader From YouTube,Fac

Vijay Kumar 4 Nov 8, 2021
Simple web interface to manage Redis databases.

phpRedisAdmin phpRedisAdmin is a simple web interface to manage Redis databases. It is released under the Creative Commons Attribution 3.0 license. Th

Erik Dubbelboer 3k Dec 31, 2022
phpRedisAdmin is a simple web interface to manage Redis databases.

phpRedisAdmin phpRedisAdmin is a simple web interface to manage Redis databases. It is released under the Creative Commons Attribution 3.0 license. Th

Erik Dubbelboer 2.8k Dec 1, 2021