Calling file_view_file($file, $view_mode) with a custom thumbnail-size will create HTML-code containing an IMG-tag which combines the URL for the thumbnail with width and height attributes appropriate for the original image.

This was noticed when, after an upgrade of Drupal core and modules, thumbnail-images inserted through a WYSIWYG-editor were not displayed with the correct dimensions. But manually calling 'file_view_file' from PHP-code yielded the same (erroneous) HTML-code.

Pre-upgrade, the same 'file_view_file'-call generated HTML-code which lacked the width- and height-attributes on the IMG-tag and hence worked correctly. There were no other differences in the generated HTML-code.

Post-upgrade module-versions:
Media: 7.x-2.0-beta2 (datestamp: 1464653492).
File_entity: 7.x-2.0-beta3 (datestamp 1464653173)
Styles: 7.x-2.0-alpha8+5-dev (datestamp 1380638144)

CommentFileSizeAuthor
#2 styles-2795913-1.patch437 bytesMarco Vervoort
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Marco Vervoort created an issue. See original summary.

Marco Vervoort’s picture

FileSize
437 bytes

I traced the problem back to a change in the 'file_entity'-module.
In issue
https://www.drupal.org/node/1987568
the behaviour of the file_entity module was changed: formerly the file-object conained the width and height of the image as subproperties of the 'metadata' property, afterwards these were duplicated to properties of the file-object itself. This change was to improve compatibility with the 'image' module. Unfortunately, when calling
file_view_file($file, $view_mode)
the width and height attributes are passed through and end up as attributes of the image-tag, even if the $view_mode is a thumbnail of a different size.

I managed to fix the problem by modifying the 'styles' module, adding code to the 'imageStyle' function to reset the width- and height-attributes when an imageStyle-effect is applied. I am attaching a patch.

However, there may be other approaches (involving changes in the media- or file_entity modules) that are superior.

hansfn’s picture

Status: Active » Reviewed & tested by the community

I can confirm the problem and that the patch fixed it. I posted a (duplicate) issue in the Media queue before finding this one - #2890487: Original image width / height inserted when using the (obsolete?) styles module. I'm setting this to RTBC even though the fix is hackish.

irk’s picture

This patch worked on my site--spent a week chasing down this issue and where it originated, and finally images are scaling properly again. Thanks!