Video Tutorial Resize your images with Glide


In this tutorial I invite you to discover Glide. A tool that will allow you to manage image resizing through HTTP calls.

Resize on demand

There are several options when it comes to generating the different image formats needed to run your application.

  • During the'upload of an image, we generate the different image formats.
  • In the template, when we generate the URL to the image we check if the format exists and we generate it if necessary.
  • On demand, the resizing is done through an HTTP request (example image.jpg? w = 500 & h = 200).

Glide makes it possible to quickly implement this third solution. To use it we start by installing the dependency.

composer require league / glide

We can then create a "server" whose role will be to interpret a request to return a resized image in return.

$ server = League  Glide  ServerFactory :: create ((
    'source' => 'path / to / folder / image',
    'cache' => 'path / to / cache',
));

This server can then be used for conversion.

// Glide will automatically interpret parameters like w, h, q, fm ...
$ server-> outputImage ($ path, $ _GET);

Secure with a signature

If you just pass all the parameters to the system you run the risk of having your system exploited by malicious people. To remedy this problem, it is possible to sign the URLs to ensure that the parameters are not altered.

getUrl ('cat.jpg', ('w' => 500));
echo '';

It will then be possible to verify the request upon receipt

validateRequest ($ path, $ _GET);
    return $ server-> outputImage ($ path, $ _GET);
} catch (SignatureException $ e) {
    // We manage the error
}

And There you go ! You can also add extra protection by limiting the maximum image size.

$ server = League  Glide  ServerFactory :: create ((
    'max_image_size' => 1024 * 768,
));

Integration into frameworks

Glide can be easily integrated into the most popular frameworks. These libraries offer better communication with objects Request and Response specific to each framework.

composer require league / glide-cake
composer require league / glide-laravel
composer require league / glide-slim
composer require league / glide-symfony
composer require league / glide-zend