
Hermitage - Solving Your Image Storage and Processing Issues
Hello! I’ll be brief: in exchange for five minutes of your time, the Live Typing PHP development department will tell you about its own microservice for storing and processing uploaded images. It is called Hermitage . Its task is to present the image in different versions according to predefined parameters. Hermitage will be useful in situations where you need a standalone and scalable server for storing images and manipulating them.

For example, take Glide . In addition to the fact that he and similar services deal with the original image every time, processing it on the fly, he also accepts manipulation parameters in every image request. This not only loads the system, but also forces the client side to pass parameters for the required version of the image each time.
With Hermitage, the client receives not the source code processed here and now, but its version. To obtain the desired version of the image, you must pass with the request only the name of the version.
When the server for the first time receives a request for a version, it will manipulate the image according to parameters that are previously known to it and, upon next calls, it will give the finished image.
Images are stored on any storage convenient for you, whether it be AWS S3, Dropbox or FTP. FlySystem library helps to abstract from one specific storage and connect any of them , and Image from Intervention is used to manipulate images .
In order to save your time, we made funny pictures about how it all works.
Saving an image Image

request Image

request by the client can proceed in two scenarios: when the version of the image is already in the cloud, and when it is not there.
In the first case, the service gives the client a version of the image, receiving it directly from the cloud:

And in the second, it performs the necessary manipulations on the image, saves it to the cloud and gives it to the client:

Hermitage API is ugly simple and provides the basic functionality: save, delete and get the necessary version of the image.
For security reasons, requests for saving and deleting an image require a signature that is unique to each request and based on the request URL, http method, current timestamp, and secret key.
You can get the image by the URL:
where
More information on the API can be found here .
There is also a ready-made client for working with the API in PHP: hermitage-php-client and an adapter for the popular Yii2 framework .
Requirements:
Run the following Composer command:
Copy the file
The local file
Do not forget to generate a random string and register
We write the necessary versions of the images in the file
At the end of all you can do is configure the web server. It will be nginx or Apache, you decide.
That's all. If you liked the microservice , we will be glad to hear your commits.
Leave your comments and suggestions in the comments.

Why Hermitage is good compared to others
For example, take Glide . In addition to the fact that he and similar services deal with the original image every time, processing it on the fly, he also accepts manipulation parameters in every image request. This not only loads the system, but also forces the client side to pass parameters for the required version of the image each time.
With Hermitage, the client receives not the source code processed here and now, but its version. To obtain the desired version of the image, you must pass with the request only the name of the version.
When the server for the first time receives a request for a version, it will manipulate the image according to parameters that are previously known to it and, upon next calls, it will give the finished image.
Images are stored on any storage convenient for you, whether it be AWS S3, Dropbox or FTP. FlySystem library helps to abstract from one specific storage and connect any of them , and Image from Intervention is used to manipulate images .
How Hermitage Works
In order to save your time, we made funny pictures about how it all works.
Saving an image Image

request Image

request by the client can proceed in two scenarios: when the version of the image is already in the cloud, and when it is not there.
In the first case, the service gives the client a version of the image, receiving it directly from the cloud:

And in the second, it performs the necessary manipulations on the image, saves it to the cloud and gives it to the client:

API
Hermitage API is ugly simple and provides the basic functionality: save, delete and get the necessary version of the image.
For security reasons, requests for saving and deleting an image require a signature that is unique to each request and based on the request URL, http method, current timestamp, and secret key.
You can get the image by the URL:
http://hermitage/{filename}:{version}
where
{filename}
is the name of the file, and {version}
is the name of the requested version of the image, registered in the configuration file of this application. More information on the API can be found here .
There is also a ready-made client for working with the API in PHP: hermitage-php-client and an adapter for the popular Yii2 framework .
How to deploy Hermitage
Requirements:
- PHP> = 7.0
- PHP APCU
- GD2 / ImageMagick
Installation
Run the following Composer command:
composer create-project livetyping/hermitage-skeleton hermitage
Configuration
Copy the file
.env.example
to.env
cp .env.example .env
The local file
.env
looks like this:AUTH_SECRET=changeme
###
# Adapter
##
STORAGE_ADAPTER=local
# AWS S3
#STORAGE_ADAPTER=s3
#STORAGE_S3_REGION=
#STORAGE_S3_BUCKET=
#STORAGE_S3_KEY=
#STORAGE_S3_SECRET=
Do not forget to generate a random string and register
AUTH_SECRET
. We write the necessary versions of the images in the file
config/versions.php
:/**
* [
* '{version-name}' => [
* 'type' => '{manipulator-name}',
* // manipulator options
* ],
* ]
*
* Default manipulators:
* - resize {@see \livetyping\hermitage\foundation\images\processor\manipulators\Resize}
* - fit {@see \livetyping\hermitage\foundation\images\processor\manipulators\Fit}
*/
return [
'mini' => [
'type' => 'resize',
'height' => 200,
'width' => 200,
],
'small' => [
'type' => 'resize',
'height' => 400,
'width' => 400,
],
'thumb' => [
'type' => 'fit',
'height' => 100,
'width' => 100,
],
];
At the end of all you can do is configure the web server. It will be nginx or Apache, you decide.
References
That's all. If you liked the microservice , we will be glad to hear your commits.
Leave your comments and suggestions in the comments.