Problem/Motivation
Whenever possible, responsive images should be built with srcset rather than the <picture> tag (more). Also, Stage File Proxy, S3 File System, and the Flysystem suite of modules are common solutions to file storage. Unfortunately, when relying on proxied services for image files that are being rendered through an image style that will output a srcset image tag, an exception is thrown that breaks the entire page.
Steps to reproduce
- Install Drupal with the Responsive Images (core) module.
- Create a responsive image style that will output a
srcsetimage tag using this technique. The image styles used within the responsive image style should scale only by one dimension. - Create a content type with an image field.
- Set that image field's formatter to be a "responsive image" using the style created in Step 2.
- Create a piece of content and upload an image that will use that formatter.
- Download and enable the Stage File Proxy contrib module (or any other module that uses a proxied service for storing images) and configure it to point to another origin if a file in the file system does not exist.
- Move the image file from Step 5 outside of the current environment's file system, and into the place where the SFP origin points to, in such a way that when using SFP as designed, it would find and download this image file when it realized the current environment's file system didn't yet have it.
- Navigate to the node page that should output the image using the responsive image formatter.
Expected result: Upon realizing the file isn't in the current environment's file system, Stage File Proxy should grab it from the origin (assuming it exists there) and should be able to use it when rendering the responsive image.
Actual result: An exception is thrown that breaks the entire page:
LogicException: Could not determine image width for 'public://image-name.jpg' using image style with ID: responsive_image_style. This image style can not be used for a responsive image style mapping using the 'sizes' attribute. in responsive_image_build_source_attributes() (line 391 of core/modules/responsive_image/responsive_image.module).
Notes: This error is not thrown when the responsive image style is configured to output a <picture> element. SFP will work as designed and the page/image will be displayed as expected.
Proposed resolution
Unfortunately, I don't know. Looking for guidance here from those more familiar with this functionality...
I assume that a bug that so heavily relies on a contrib module would normally be that module's problem, but this doesn't quite seem to be the case because the exception never gives SFP a chance to fetch the image. I suppose I could have framed it this way: if the image doesn't exist in the file system, an exception is thrown that breaks the whole page, but I would expect only a broken <img> tag on an otherwise OK page. I didn't because that seems more like a "fix your image" problem than a "developers are going to rely on this to work without issue and it won't" problem.
Remaining tasks
TBD
User interface changes
N/A
API changes
N/A
Data model changes
N/A
| Comment | File | Size | Author |
|---|
Issue fork drupal-2827921
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
jelle_sDid you try it with the latest dev version when you filed this issue?
#2822429: template_preprocess_responsive_image() does unnecessary IO by creating Image objects was committed on Nov 1st 2016, and it seems it might have fixed the issue. I tried this using the latest git checkout today and couldn't reproduce the error.
sites/default/files2instead ofsites/default/files).2016-12fromsites/default/filestosites/default/files2.sites/default/files/styles.Comment #3
guschilds commentedJelle_S,
I believe you're right and that the patch in #2822429: template_preprocess_responsive_image() does unnecessary IO by creating Image objects has fixed this bug. I'm no longer able to reproduce it. Seeing as that patch was committed before I filed this issue, I must have been mistaken when I thought I was testing on the most recent version of core.
For reference, I think a detail I'd forgotten to mention in the original description (I just updated it) is that the image styles need to only scale by one dimension (width in my case) and not both. Regardless, I'm still not able to reproduce it with that in place.
Thanks for your time!
Comment #4
chrisfree commentedReopening because I am still able to produce this issue. When the source file for a responsive image style is missing from the local file system, Drupal is unable to determine the width/height of the image and thus, the exception is thrown.
For example, in the following snippet, if the file referenced by
$media_entity->field_media_imageis missing, the page attempting to include it will bomb.We have been able to work around this by forcing a width/height value, like so:
When SFP has had a chance to pull down the source image, for example when visiting the entity's view/edit pages, the page renders without issue. Presumably, because the width/height have been reliably determined within
_responsive_image_build_source_attributes().I'm unsure as to the best path forward, but my gut says that if an image asset is missing from the local filesystem, the responsive image module should simply render a broken image, versus throwing an exception that brings down the entire page.
Comment #5
bkosborneStill in issue in 8.4 so updating version.
Comment #6
bkosborneI created a sister issue to try and solve this in Stage File Proxy as well: #2922292: Support responsive images, because it's not clear who should really be responsible for making this work.
I think it's a little harsh for core to throw an uncaught exception when this happens though. As a site owner, do you really want your site to completely tank when an image file is missing on your server for whatever reason? Wouldn't you rather just a broken image appear?
A more reasonable approach may to log the error & output the image w/o srcset attribute set at all. Instead it would just output the fallback image style.
Comment #7
bkosborneHere's a patch that removes the exception and replaces it with an error in the logs.
Comment #8
chrisfree commentedPatch tested and so far appears to be working. We just deployed this out to a production site and will report back if we find any issues. Thank you @bkosborne!
Comment #9
chrisfree commentedComment #10
catchIt makes sense to make not throw the exception and log the error instead, however I think we should add test coverage here.
Comment #11
markdorisonComment #13
socketwench commentedStill a problem here, on 8.6.1 with SFP and flysystem_s3.
Comment #14
chris burge commentedComment #18
norman.lol#7 fixed the fatal on missing images for me on core 8.9.1.
Comment #20
thirstysix commentedSame issue with Drupal 8.9.15
LogicException: Could not determine image width for 'public://.....jpg' using image style with ID: max_1300x1300. This image style can not be used for a responsive image style mapping using the 'sizes' attribute. in _responsive_image_build_source_attributes() (line 434 of web\core\modules\responsive_image\responsive_image.module).
Comment #21
glynster commentedThis patch resolves the issue for us as well! RTBC +1
Comment #22
chris burge commentedRerolled patch addresses coding standards error.
Comment #23
summit commentedHi, Great! This Patch resolved mij WSOD on homepage D8. Thanks. And please get it in next release, so others don't have this problem.
Thanks for building and rerolling! Greetings, Martijn
Comment #24
xamountPatch at #22 works for me
Comment #26
mikebarkas commentedPatch #22 worked for me. RTBC +1
Tested patch with Drupal 9.3.9 and PHP 8.0.16 using Stage File Proxy 8.x-1.2
Comment #27
andregp commentedWe still need to address #10 and add tests to this issue so +1 to keep it on Needs Work
I tried myself to add some tests for the patch, but the problem is that the code to be tested is inside the responsive_image.module file and I don't know yet how to test code inside *.module files.
Comment #31
glynster commentedRTBC +1 this resolved the problem for us when using proxy file module.
Comment #34
akhouryComment #35
abu-zakham commentedRerolled patch
Comment #36
joshuamiThis issue occurs with the s3fs module as well and I can confirm that the patch correctly handles the error for that contrib module as well. Updating the issue summary to reflect that a core fix would solve the issue for more than just stage file proxy.
Would something like this need tests? The patch basically shifts the error to the error logs rather than disrupting page render. That seems reasonable.
Comment #37
jhedstromThis patch fixes the fatal error, which is great, but it has downstream PHP warning/notice issues as the underlying responsive image code expects certain metadata (width, height) to always be set.
Comment #38
norman.lolComment #39
thejimbirch commentedThe patch in #35 fixes the issue for me with no images and with Stage File Proxy on Drupal 10.4.3.
Leaving as Needs Work though for the comments in #37.
Comment #43
korn3000 commentedport patch for 11.3.1
Comment #44
prudloff commentedI added a test that fails without the patch.
Comment #45
smustgrave commentedCan we turn the test to a kernel.
Comment #46
smustgrave commentedIn an effort to go more kernel can we convert that please.