Drupal - Unable to generate the derived image

You should also make sure you do have a graphical library (like php-gd) installed on your server: check /admin/config/media/image-toolkit (D7).


The error is coming from Image module (core) and the logic looks like:

$success = file_exists($derivative_uri) || image_style_create_derivative($style, $image_uri, $derivative_uri);
if ($success) {
  $image = image_load($derivative_uri);
  file_transfer($image->source, array('Content-Type' => $image->info['mime_type'], 'Content-Length' => $image->info['file_size']));
} else {
  watchdog('image', 'Unable to generate the derived image located at %path.', array('%path' => $derivative_uri));
  ...
}

Therefore error happens when Drupal has problem with generating an image derivative by applying all image effects and saving a cached version of the resulting image.


The easiest way reproduce the problem (for diagnosting purposes) is by drush.

  1. Invoke image_style_create_derivative() directly from drush:

    drush -v eval 'image_style_create_derivative(reset(@image_styles()), "public://pictures/picture-123.png", "public://styles/test/test-success.png");
    

    Replace picture-123.png with the existing picture from the log:

    Unable to generate the derived image located at public://styles/foo/public/pictures/picture-x.png

    Or use any other existing one, e.g. drush sqlq "SELECT * FROM file_managed".

    If you don't have access to shell, use Devel module, go to /devel/php and paste the PHP code there.

    Note: If the files folder is owned by Apache user, you should log-in as this user for testing purposes. Otherwise prefix your drush command with sudo -u www-data.

  2. There are the following possibilities.

    • Above test has been successful (file has been generated successfully in your files dir), if so, check if your failing picture from the log actually exists, maybe it was removed from the server.
    • If you have the same error message or the file wasn't created, then it's problem with your permission or missing libraries (check: drush eval "print_r(gd_info());").
    • If you don't have any error and the file wasn't created, then check if you used the right existing files.

Debugging permission problems can be easily achieved by strace. Install it and just prefix any command that you're testing with strace -f (you don't have to be root).

In example:

$ strace -f drush -v eval 'image_style_create_derivative(reset(@image_styles()), "public://existing-image.png", "public://styles/test/test-success.png");' 2>&1 | grep "default/files"
mkdir(".../sites/default/files/styles/test", 0775) = -1 EACCES (Permission denied)
chmod(".../sites/default/files/styles/test", 0775) = -1 EPERM (Operation not permitted)

If you're logged in with different account which owns files, then don't forget to prefix your drush command with sudo -u www-data to run the whole command as Apache user.


Briefly, this is what I do whenever I have this problem:

Change user to the one running your web server (apache, httpd, www-data, nginx etc)

sudo -u [user] -s /bin/bash

and cd directly to the files directory, and try to write random data to a new file.

echo "Random text" > some_file_name

Usually this will fail and you'll get an error message from the OS, moving the problem out from Drupal at least. If this fails to work, you have file permissions issue. This question may be helpful: What are the recommended directory permissions?

Tags:

Media

7