Hello! I'll be brief: in exchange for five minutes of your time, the department of PHP development company Live Tiping will tell you about its own microservice for storing and processing downloaded images. It is called
Hermitage . His 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.

What is good Hermitage compared to others
For example, take
Glide . Besides the fact that he and similar services each time deals with the original image, processing it on the fly, he also accepts the manipulation parameters in each request to the image. This not only loads the system, but also forces the client side to transfer parameters for the required version of the image each time.
With Hermitage, the client receives a source that is not processed here and now, but its version. To get the required version of the image, you should send only the version name with the request.
When the server first receives a request for a version, it will manipulate the image according to the parameters it knows in advance, and on subsequent requests will return the finished image.
')
Images are stored on any storage convenient for you, be it AWS S3, Dropbox or FTP. The
FlySystem library helps to abstract from one specific repository and connect any of them, while
Intervention Image is used for image manipulations.
How Hermitage Works
In order to save your time, we made funny pictures about how it all works.
Saving image
Image Request
The image request by the client can proceed in two scenarios: when the image version 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 private key.
You can get the image at the URL:
http://hermitage/{filename}:{version}
where
{filename}
is the file name, and
{version}
is the name of the requested version of the image specified 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 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
.env
file looks like this:
AUTH_SECRET=changeme
Do not forget to generate a random row and set
AUTH_SECRET
.
We register the necessary versions of images in the
config/versions.php
file:
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 have to do is configure the web server. It will be nginx or Apache, you decide.
Links
That's all. If you like
microservice , we will be happy for your commits.
Leave your comments and suggestions in the comments.