To reproduce add a new remote image to a node, the image preview is not working
posted in http://drupal.org/node/1577560. I don't know who should be the owner.

Thanks

Comments

agoradesign’s picture

I have the same problem. After researching, I can confirm that it has to do with the file entity update. In unstable4 a new function checking if a file is local (file_entity_file_is_local() ) was introduced, which is used e.g. in file_entity_file_formatter_file_image_view() which is only supporting the display of local files from this change on.

This new behaviour was introduced during a bugfix for the media module: http://drupal.org/node/1142630#comment-5690580

As the fact that this function is only able to show local files is documented as well ("This formatter is only capable of displaying local images. If the passed in file is either not local or not an image, nothing is returned, so that file_view_file() can try another formatter."), this is not really a bug, it works by design.

But as not seeing the preview images is annoying, the Remote Stream Wrapper module should implement an own image formatter to correct this.

For a quick fix, I just implemented the hook_file_formatter_info_alter() by myself and changed the $info['file_image']['view callback'] to an own function, which is a copy of the file_entity_file_formatter_file_image_view() function from before that patch, that changed the display.

See my hacky solution below:

function YOUR_MODULE_file_formatter_info_alter(&$info) {
  if (isset($info['file_image'])) {
    // alter the view callback in order to display remote files
    // this is the hacky solution for the problem
    // cleaner but a bit more time consuming would be implementing an own file formatter!
    $info['file_image']['view callback'] = 'YOUR_MODULE_file_formatter_file_image_view';
  }
}

function YOUR_MODULE_file_formatter_file_image_view($file, $display, $langcode) {
  // Prevent PHP notices when trying to read empty files.
  // @see http://drupal.org/node/681042
  if (!$file->filesize) {
    return;
  }

  // Do not bother proceeding if this file does not have an image mime type.
  if (strpos($file->filemime, 'image/') !== 0) {
    return;
  }

  // Undo changes made by http://drupal.org/node/1142630#comment-5690580
  $scheme = file_uri_scheme($file->uri);
  $local_wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_LOCAL);
  if (isset($local_wrappers[$scheme]) && $image = image_load($file->uri)) {
    if (!empty($display['settings']['image_style'])) {
      $element = array(
        '#theme' => 'image_style',
        '#style_name' => $display['settings']['image_style'],
        '#path' => $file->uri,
        '#width' => $file->image_dimensions['width'],
        '#height' => $file->image_dimensions['height'],
      );
    }
    else {
      $element = array(
        '#theme' => 'image',
        '#path' => $file->uri,
        '#width' => $file->image_dimensions['width'],
        '#height' => $file->image_dimensions['height'],
      );
    }
    return $element;
  }
}
cmalek’s picture

agoradesign, the code from your comment #1 helped me out tremendously in getting my AmazonS3 backed file entites to render their image styles. Thanks!

egarias’s picture

Thanks!
worked for me too!

lookatthosemoose’s picture

I can confirm this issue as well.

socialnicheguru’s picture