In modern Drupal (8.4/9/10), Media in core has no option to set minimum / maximum resolutions on media items that can be selected.
This is problematic because an image field meant to show full-screen (2,000 pixels+) might have been uploaded as only a 200 pixels across. (Allowing this smaller size to be uploaded and used is all right in other contexts— other fields.) Rather than splitting media types and media libraries, it's better to filter the media library for the images which fit minimum resolution requirements for a given field, or trigger a validation when a too-small image is selected.
For fields of Reference type: Media
with Media type: Image
The image minimum resolutions should be configurable (per field instance if feasible, if not per field is good too)
The minimum width and minimum height should be honored:
- when uploading new images, or
- when selecting existing images from the media library
| Comment | File | Size | Author |
|---|
Issue fork drupal-3016649
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
joseph.olstadThe D8/D9 Media project is in core now so moving this issue to core.
component Media system
after quickly searching it "appears" as if in issue #1223794 this functionality was added to media 7.x-2.x/7.x-3.x/7.x-4.x
#1223794: Widget does not respect settings in image_field_instance_settings_form (min/max resolution)
Comment #3
supermoos commentedThanks for moving it to core. The issue you're refencing is not fixed in drupal 8.
Comment #4
joseph.olstad@supermoos
please note, as a workaround you can enforce image sizes by applying an image style that enforces a max / min resolution
please try the imagick module for D8/D7 , it's also got image optimisation support and optionally removes exif metadata on the 'styled' images but retains the exif metadata on the original image. It's a good replacement for the core image processing.
Comment #5
supermoos commented@joseph.olstad - it's not really a viable workaround though as it doesn't address the main issue of making sure content editors upload "good enough" src imagery - minimum resolutions is needed for this.
Comment #6
joseph.olstad@supermoos, image styles are precisely for that, imagick has several good options , one maintains aspect ratio and lets you choose the dimensions of the final image width/height, you can apply an image style to the field in views or in a twig extension.
however I am not arguing with you about the functionality, there's obviously maybe improvements that could be made, I'm not exactly sure what this functionality should be or how it should behave. The related D7 solution might be very similar. If you have more details/ ideas, on what you expect, please elaborate.
Comment #7
supermoos commented@joseph.olstad - image styles are great, but they can't magically make an image uploaded in the size of say a 100x100 pixels look great at a 1000x1000, for that to work the source image needs to be at least 1000x1000 too - this is possible to configure on the Field Image, but not on media items - therefore I would love to see it as an option on media items too (the same options as on an image field).
Comment #8
joseph.olstadyou can alternatively look at https://www.drupal.org/project/focal_point
focal_point lets your content editors choose what part of the image to focus on by clicking on it.
Comment #9
joseph.olstadah ok, ya a minimum size requirement, yes that is important.
Comment #11
scott_euser commentedThe issue itself references that alt text is not possible, but it is:

Other than that, image dimensions settings per field seems useful but to me it is linked to the view mode of the media type. Eg, if your image media type has the following view modes:
It would make sense to have a different resolution for each Media Type view mode. The doesn't match the current setup of dimensions settings controlled in the image field of course.
One workaround could be multiple image fields in your Image Media Type and each display mode showing a different image field. You would also need a different form mode for each display mode. Each individual image field can have its own dimension settings as is.
Eg,
Comment #12
jaskaran.nagra commentedI did a patch for this here: https://www.drupal.org/project/drupal/issues/3032282
I have attached the patch in this issue to close off the other one. Needs some more work as Drupal 8.7 changes a few connections. See the other issue for more details.
Cheers
Comment #13
jaskaran.nagra commentedAdding another much simpler patch for Drupal 8.7 and above.
Cheers
Comment #14
stefan.kornI created an issue about this earlier: https://www.drupal.org/project/drupal/issues/3008292
So maybe one of the two issues can be closed as a duplicate.
Comment #15
hawkeye.twolfThe current issue title, "Image minimum and maximum resolutions should be available to configure on media fields of image type", differs from what's actually being discussed and proposed here, which is closer to #3008292: ImageItem::getUploadValidators() should be the source of truth for validating uploaded images. Should we move the patch over there and close this issue?
Comment #16
phenaproximaYes, yes we should. Closing this as a duplicate and transferring credit into that issue.
Comment #17
steveoriolThe patch #13 is still applied without problem on drupal 9.1.7
But, i have to see https://www.drupal.org/project/drupal/issues/3008292
Comment #18
greenskin commentedBased on this issue's title and description, I wouldn't call this a duplicate of #3008292: ImageItem::getUploadValidators() should be the source of truth for validating uploaded images. A media image field instance should have the ability to set a min/max resolution for the image. A site could have multiple media image field instances on a collection of paragraph types requiring different min/max resolutions. One could add multiple media image types but that's not a preferred solution, and results in duplicate media items between media types. Say for example you have a paragraph type with a media image field where the uploaded images need to be at least 150x150. Then say you have another paragraph type with a media image field where the uploaded images need to be at least 1200x1200. If you used separate media image types, then you'd effectively have to upload the same image file to each media image type, even though your original is say 2000x2000. Multiple identical files will reside on your server, eating away at your file storage limits.
I'm not sure what the best solution would be here, but the basic logic should be able to be contained within the media instance. Perhaps some logic that evaluates both the media instance's min/max setting as well as the image field's min/max setting and makes sure the selected/uploaded media fits within those parameters. The media library modal preferably should also filter out or disable media items that don't fit the field instance's min/max settings.
Comment #19
avpadernoDrupal 8.8.x isn't supported. Drupal 8.9.x is now open only to security issues.
Comment #20
kasey_mk commentedPatch #13 is great, thank you! Finally, the pixel dimensions we set on the field are honored when uploading images using the media library widget on a paragraph. Resizing images to their maximum pixel dimensions on upload will save us lots of disk storage space and will prevent the errors we've been seeing on images that didn't exceed our maximum file size but did exceed our maximum pixel dimensions.
(Side note, we've discovered that more than 3840 pixels in either dimension will often result in problems during image style creation, resulting in broken thumbnail images or error messages like
LogicException: Could not determine image width for 'public://2021-10/imagename.jpg' using image style with ID: max_1200x1200. This image style can not be used for a responsive image style mapping using the 'sizes' attribute. in _responsive_image_build_source_attributes() (line 386 of core/modules/responsive_image/responsive_image.module)- anyone know why that's the magic number? This patch means we won't run into that problem anymore; I'm just curious.)Comment #21
kasey_mk commentedComment #22
alexpottThis needs tests and the solution in #3008292: ImageItem::getUploadValidators() should be the source of truth for validating uploaded images is really the correct one and already has tests.
Re #18 the fix here still delegates the min and max to the underlying image field so afaics it's not different to the solution in #3008292: ImageItem::getUploadValidators() should be the source of truth for validating uploaded images
Comment #24
kasey_mk commentedWhile waiting on https://www.drupal.org/project/drupal/issues/3008292, this patch fixes an error I'm seeing now with the patch in #13.
Comment #25
kasey_mk commentedForgot to include an interdiff
Comment #26
mrshowerman#24 works for me nicely.
However, it breaks the possibility to upload SVG images through media library, in combination with the SVG Image module. This is due to the fact that the patch in #24 adds a "file_validate_is_image" validator, which fails for SVGs even when svg is an allowed file extension.
I feel that adding this validator isn't really necessary for the purpose of this issue, so I simply left it out. Also, I updated the remaining code to patch #32 of #3008292-32: ImageItem::getUploadValidators() should be the source of truth for validating uploaded images.
Comment #29
jasonawantI think there's some confusion about this issue vs. #3008292: ImageItem::getUploadValidators() should be the source of truth for validating uploaded images. The patches provided here in this issue probably should not be used in favor of the of using the patches/merge-request from #3008292: ImageItem::getUploadValidators() should be the source of truth for validating uploaded images, so that it may progress.
This issue, as a feature request, is asking to add minimum and maximum resolutions configuration on the media entity reference field to 1) allow site builders to configure these options per media image usages and 2) use these configured values when validating an uploaded image instead of the media entity's image field configured values. This is entirely different than what the patches in this issue solve.
The current issue summary also includes a separate feature request to manage the image title and alt text per media entity reference field. This is likely to be one or two separate related feature request issue(s).
Tagging as needs issue summary update.
Comment #31
mlncn commentedUpdated issue summary. I dropped the part about configuring maximum resolutions per field because i don't see the use case; if it is a server space limitation that would be sitewide, no?
Comment #32
kasey_mk commented@mlncn being able to configure the maximum resolution is absolutely critical to the problem we were experiencing, where people were uploading images that were way too big for Drupal to process (3840 pixels in either dimension seems to be the breakpoint). The uploads would appear to finish, but none of the image styles could be processed from it.
Patch #24 solved that problem for us, allowing people to upload images of any size via any field which would then be properly resized and processed.
Comment #33
kasey_mk commentedComment #34
jvogt commentedThanks, Kasey. Setting max resolution is critical for us as well. It's about making the images fit for purpose. We want to prevent users from bogging down the site storage with 5000px wide images that will only ever be used as a 100px wide thumbnail.
Comment #35
rajeevgoleDoes the patch also take care of the #2nd(when selecting existing images from the media library) use case mentioned in the issue description?
Comment #36
jvogt commented@rajeevgole: no I don't believe it does. It seems to alter only the Media library upload form.
Comment #37
jvogt commentedI'm not sure the patches in #24/26 address the original issue. I think they are (and I am) looking for a way to control the image resolution from a Media reference field, with Image media as an allowed type, rather than from the Image field within a Media type. I realize this means two places where min/max can be configured, but I'm really trying to avoid creating a separate Media type for images where I want to control the resolution at time of upload.
It's possible that this just isn't how Media entities are intended to be used in Drupal 8/9/10+, but if that's the case, I'm struggling to find any documentation to that effect or a good workaround for my use case. The entity_browser module provides the "Upload images as media items" widget that allows you to specify an upload location, which overrides the selected Media type's location, so maybe that's a more appropriate place for this kind of feature.
Comment #40
bhanu951 commentedRerolled patch from #26 against 11.x branch.
Comment #41
bhanu951 commentedComment #42
smustgrave commentedHave not tested
But issue summary should be updated to match standard template please
Sounds like something that could use some test coverage also.
Thanks.
Comment #43
mariohernandez commentedI tested the patch in Drupal 10.4 and it looks like the original issue has not been resolved. I am able to set min/max resolutions at the Image media type level, but those settings are applied on all media reference fields of type image. Has anyone been able to fix this so min/max resolutions can be individually configured per each instance of a media reference field of type image?