Skip to content

Image Generation Issues in Magento 2

If you find that images are not being generated in the media/cache directories when visiting pages that show product images - here are a few things to check.

Serving from pub

  • If you are using pub to serve the application, ensure that the following is in your env.php file.
        'directories' => [
            'document_root_is_pub' => true
        ],
    

This tells Magento to treat the directory structure differently and also directly affects how it searches for search images to display on the frontend, if this flag is not set the images may not generate on the fly.

Be sure to clear the cache and delete var/resource_config.json as this contains the configuration for how media is looked up (this should be regenerated automatically).

Flag documentation Env Reference

Directory and file permissions

The Magento web user needs to be able to write the certain directories for the application to work correctly. Refer to the official documentation for how to correctly setup permissions for security and correct functionality.

File System Setup

Debug get.php

Magento makes a call to pub/get.php when it fails to find an image, the purpose of this file is to generate missing images when the file is not found, it will also serve the 404 page for Magento when there is no file available.

A breakpoint can be set in this file when hitting a media URL to check what the issue may be, if this file is not called you may have an issue with Nginx/Apache .htaccss configuration.

The var/resource_config.json file is used during the call to get.php to assert that the media is allowed to be generated.