Download the PHP package escolalms/images without Composer
On this page you can find all versions of the php package escolalms/images. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download escolalms/images
More information about escolalms/images
Files in escolalms/images
Package images
Short Description Escola Headless LMS Images manipulation
License MIT
Informations about the package images
Image processing module
What does it do
The package creates resized images from source by given parameters. This is a headless approach so the backend doesn't know the requested sizes before frontend requests any.
The input images are stored by Laravel in any of available disk (local storage/s3/any bucket). Once a resized version is requested a cached version in created and returend. Below are examples to show the overall idea.
The initial resize is done by Intervention/image with GD
driver. That can be configured.
After inital resized all the images are optimized with image-optimizer.
For best results binaries must be installed. EscolaLMS prepared Docker Images are available for development (tag work
) and production (tag prod
).
Installation
composer require escolalms/images
php artisan migrate
Database
image_caches
- table for saving the original and resized image path
id | path | hash_path | created_at | updated_at |
---|---|---|---|---|
1 | avatars/1/avatar.jpg | imgcache/68840270724ee4ff0b481f4fbd2299e13dfe2447.jpg | 2022-05-05 11:29:04 | 2022-05-05 11:29:04 |
3 | avatars/4/avatar4.jpg | imgcache/d0f1ab43ae7f2924682b2ce734a24fa7066a8ea7.jpg | 2022-05-05 11:30:49 | 2022-05-05 11:30:49 |
2 | avatars/2/avatar2.jpg | imgcache/94c6c83db6562161d38620a74e4e07fb3d9e39ed.jpg | 2022-05-05 11:30:39 | 2022-05-05 11:30:39 |
Examples
Default. One image as 302
redirect result .
Basic resize is made by URL API call which redirects to new created file
Example GET
call
http://localhost/api/images/img?path=test.jpg&w=100
call should return resized image to width 100px- checks if file exsitis
- if not, creates one with availabe libraries
- returns 302 redirect
- example
http://localhost/storage/imgcache/891ee133a8bb111497d494d4c91fe292d9d16bb3.jpg
(assuming you're using local disk storage, in case of s3 location origin would differ)
Resizing many images at once. JSON array as a result.
Example POST
call like
generates following result
Hashing algorithm
There is simple algorithm to guess the result image URL. This allows frontend application to know the processed URL without calling API. As follows
then result URL would be
Endpoint
There is API endpoints documentation
Tests
Run ./vendor/bin/phpunit
to run tests.
Events
This package extends filesystem
.
FileDeleted
- The event is dispatched when you usedelete
method on theStorage
facade.FileStored
- The event is dispatched when you useput
,putFile
orputFileAs
method.
Listeners
This package listens for FileDeleted
and FileStored
events and removes the resized images from the given path.
How to use this on frontend
Below is our totally headless approach on generating images
The following example tries to achives 2 purposes
- generate image on fly, frontend decide what sizes are needed
- images are not served by API
The idea is that since we know tha hashing algoritm for cached images we can guess that the URL will be like. If that URL is throwing 404 then we're calling the API endpoint to generate one. Fortunately this endpoint creates an requested image, caches it and returns redirect which is good for image src.
A major disadvantage of this approach is that first user once will get 404 in networking and experince few seconds delay before image is rendered after not founded.
Working example is availabe in doc folder.
All versions of images with dependencies
laravel/framework Version >=8.0
escolalms/core Version ^1
intervention/image Version ^2
intervention/imagecache Version ^2
spatie/laravel-image-optimizer Version ^1.7.1
escolalms/settings Version ^0