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

Issue fork drupal-3016649

Command icon 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

supermoos created an issue. See original summary.

joseph.olstad’s picture

Project: D7 Media » Drupal core
Version: 8.x-1.x-dev » 8.7.x-dev
Component: Media Field » media system
Related issues: +#1223794: Widget does not respect settings in image_field_instance_settings_form (min/max resolution)

The 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)

supermoos’s picture

Thanks for moving it to core. The issue you're refencing is not fixed in drupal 8.

joseph.olstad’s picture

@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.

supermoos’s picture

@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.

joseph.olstad’s picture

@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.

supermoos’s picture

@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).

joseph.olstad’s picture

you 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.

joseph.olstad’s picture

ah ok, ya a minimum size requirement, yes that is important.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

scott_euser’s picture

The 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:

  1. Large featured box
  2. Medium featured box
  3. Small featured box

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,

  1. Large featured box, uses field_image_large
  2. Medium featured box, uses field_image_medium
  3. Small featured box, uses field_image_small
jaskaran.nagra’s picture

I 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

jaskaran.nagra’s picture

Adding another much simpler patch for Drupal 8.7 and above.

Cheers

stefan.korn’s picture

I 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.

hawkeye.twolf’s picture

The 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?

phenaproxima’s picture

Status: Active » Closed (duplicate)
Issue tags: -core media

Yes, yes we should. Closing this as a duplicate and transferring credit into that issue.

steveoriol’s picture

The 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

greenskin’s picture

Status: Closed (duplicate) » Needs work

Based 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.

avpaderno’s picture

Version: 8.8.x-dev » 9.2.x-dev

Drupal 8.8.x isn't supported. Drupal 8.9.x is now open only to security issues.

kasey_mk’s picture

Patch #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.)

kasey_mk’s picture

Status: Needs work » Reviewed & tested by the community
alexpott’s picture

Status: Reviewed & tested by the community » Needs work

This 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

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.1.10 (June 4, 2021) and Drupal 9.2.10 (November 24, 2021) were the last bugfix releases of those minor version series. Drupal 9 bug reports should be targeted for the 9.3.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

kasey_mk’s picture

While waiting on https://www.drupal.org/project/drupal/issues/3008292, this patch fixes an error I'm seeing now with the patch in #13.

kasey_mk’s picture

StatusFileSize
new871 bytes

Forgot to include an interdiff

mrshowerman’s picture

Version: 9.3.x-dev » 9.4.x-dev
Status: Needs work » Needs review
StatusFileSize
new1015 bytes
new1.77 KB

#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.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

jasonawant’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update

I 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.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mlncn’s picture

Title: Image minimum and maximum resolutions should be available to configure on media fields of image type. » Image minimum resolutions should be configurable per field instance (for fields of Reference type Media with Media type Image) and these should be honored when uploading new images or selecting existing ones from the media library
Issue summary: View changes
Issue tags: -Needs issue summary update +Field UX

Updated 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?

kasey_mk’s picture

@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.

kasey_mk’s picture

Title: Image minimum resolutions should be configurable per field instance (for fields of Reference type Media with Media type Image) and these should be honored when uploading new images or selecting existing ones from the media library » Image minimum and maximum resolutions should be configurable per field instance (for fields of Reference type Media with Media type Image) and these should be honored when uploading new images or selecting existing ones from the media library
jvogt’s picture

Thanks, 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.

rajeevgole’s picture

Does the patch also take care of the #2nd(when selecting existing images from the media library) use case mentioned in the issue description?

jvogt’s picture

@rajeevgole: no I don't believe it does. It seems to alter only the Media library upload form.

jvogt’s picture

I'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.

Bhanu951 made their first commit to this issue’s fork.

bhanu951’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update, +Needs tests

Have not tested

But issue summary should be updated to match standard template please

Sounds like something that could use some test coverage also.

Thanks.

mariohernandez’s picture

I 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?

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.