Generate image manipulations in a Laravel project
Earlier this month Glide joined The League of Extraordinary Packages. This package provides an easy way to generate image manipulations on the fly. Within a couple of hours I had integrated this wonderful package into both my company's legacy and current CMS.
The current CMS is based on Laravel. The Glide-integration is now available as a package on github: freekmurze/laravel-glide.
Installation
Nothing too fancy, you’ll have to:- use composer to pull in the package
- install a service provider
- install a facade
- publish a config file
Usage
Assuming you've got an image named `kayaks.jpg` in `app/storage/images` (this directory can be customized in the config file) you can use this code in a blade view:
<img src="{{ GlideImage::setImagePath('kayaks.jpg')->setConversionParameters(['w'=> 50, 'filt'=>'greyscale']) }}" />
The function will output an URL to a greyscale version of kayaks.jpg
that has a width of 50 pixels. As soon as the URL gets hit by your browser, the image will be generated on the fly. The generated image will be saved in app/storage/glide-cache
(= the cache directory specified in the input file).
The generated URL will also be signed to prevent jerks from generating unwanted manipulations.
The setConversionParameters
-function accepts an array with conversion options. You can take a look at the image API of Glide to see which options are available.
It's also possible to generate an image directly on the server:
GlideImage::setImagePath('kayaks.jpg')
->setConversionParameters(['w'=> 50, 'filt'=>'greyscale'])
->save($fileWhereImageManipulationMustBeSaved);
What are your thoughts on "Generate image manipulations in a Laravel project"?