Generate thumbnails based on folder

In the 3.3.12 release we introduced a way to generate thumbnails based on the folders in a given path.

For instance if you have a module that allows the administrator to upload an image, you probably want to create a thumbnail to show it in the datagrid. But on the frontend you will need the image in differents sizes, for instance a square 128x128-image to use in the overview, or a image where the maximum width is 720px.

Well from now on you don't have to worry about writing that code. Just create the folders:

  • Use 128x128 as foldername to generate an image where the width will be 128px and the height will be 128px.
  • Use 720x as foldername to generate an image where the width will be 720px, the height will be calculated based on the aspect ratio.
  • Use x430 as foldername to generate an image that where the height will be 430px, the width will be calculated based on the aspect ratio.

In your module you can use the code below:

$this->form->getField('image')->generateThumbnails($imagePath, time() . '.png');

Where $imagePath is the folder that contains the thumbnail folders, if it doesn't exists a folder called source will be created to store the source-file.

Generating thumbnails using the CLI

bin/console forkcms:thumbnails:generate --folder=Users/avatars