Problem/Motivation

According to PHP documentation when converting image to WebP quality argument can be passed to set the quality of the image.
In gd.php default is set to 80% and there is no way to configure or change this value.
function imagewebp($image, $to = null, $quality = 80): bool {}

Steps to reproduce

Go to /admin/config/media/image-toolkit
Select GD2 for image processing
Only JPEG quality is configurable not WebP.

Proposed resolution

Make the quality settings configurable and pass the configured value to imagewebp().

User interface changes

When selected GD2 to process images add an additional field to take the WebP quality value similar to ones taking JPEG.

Before

Before

After

Before

When lossless selection has been done, it hides the WebP integer field.
Before

Issue fork drupal-3320689

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

majid.ali created an issue. See original summary.

majid.ali’s picture

StatusFileSize
new7.63 KB
majid.ali’s picture

Status: Active » Needs review
cilefen’s picture

Version: 9.5.x-dev » 10.1.x-dev
Status: Needs review » Needs work
Issue tags: -webp, -gd2, -toolkit
majid.ali’s picture

Issue summary: View changes
sandeepsingh199’s picture

StatusFileSize
new6.85 KB

fixed the #2 custom command failed issue for D10.1.x. Removed Aggregator module changes, as we know from D10.1.x aggregator is not a part of Drupal core. We fix separately for Aggregator module.

sandeepsingh199’s picture

Status: Needs work » Needs review
nivethasubramaniyan’s picture

StatusFileSize
new288.26 KB
new320.58 KB

I have verified the changes and attached the images for reference

jcnventura’s picture

Status: Needs review » Needs work

Does it even make sense to add this to the Drupal 6 and 7 migration? It is obvious that this value is not defined in any version before D10, so I question the need to the migration of that non-existing setting.

Also, a cursory look at the current contents of the cspell dictionary shows that file is case-insensitive being 100% lower-case, so there is certainly no need to add 2 versions of 'WEBP' to it.

And maybe a suggestion, I fear for an explosion of image_xxxx_quality, maybe instead of image_jpeg_quality and image_webp_quality, we should replace both with image_quality and use the same value for both jpeg and webp, but this last one is my personal opinion, and would benefit from the opinion of others.

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

majid.ali’s picture

I agree with #9 that this dose not exist before D10 so its not needed for migration of D6, D7. Also i have removed 'WEBP' cspell dictionary 'webp' is enough. However i beg to differ on your last suggestion and i think both quality configuration should be configurable individually. Since now JPEG is fallback of WebP in my case i want to configure JPEG to low quality and webp to high quality or even lossless.

I have also added configuration for the lossless WebP if php >= 8.1. The issue is discussed here https://www.drupal.org/project/drupal/issues/3228600

majid.ali’s picture

Status: Needs work » Needs review
jcnventura’s picture

Status: Needs review » Needs work

Well, the introduction of lossless is most likely causing the first test to fail. And I believe the other 2 tests are failing because of the remaining change to core/modules/system/migrations/system_image_gd.yml.

This is a patch for Drupal 10 which requires PHP 8.1 as a minimum (https://www.drupal.org/docs/system-requirements/php-requirements), so no need to check if the IMG_WEBP_LOSSLESS is defined, as it should always be there.

If this is ever ported back to Drupal 9, then the check would be useful but only for the backport.

Also, please do interdiffs between the new patch and the previous one.

majid.ali’s picture

StatusFileSize
new5.92 KB

Re rolling patch #11 for 9.5.x.

sahil.goyal’s picture

Hard to get the diff without interdiff ☹️

fskreuz’s picture

Status: Needs work » Needs review
StatusFileSize
new5.9 KB
new5.93 KB

Reroll of #11 for 9.5.x and 10.1.x

fskreuz’s picture

Reroll #11 against 9.5.x and 10.1.x once more (#16 rerolled against older versions of those branches).

The last submitted patch, 16: core-webp_quality_config-3320689-9.5.x.patch, failed testing. View results

The last submitted patch, 17: core-webp_quality_config-3320689-9.5.x.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 17: core-webp_quality_config-3320689-10.1.x.patch, failed testing. View results

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.

andypost’s picture

I think instead of configuration for each format core can introduce new common option

Also tests needs to be fixed

elaman’s picture

Patch for 10.2.x.

stevensf66’s picture

#23 works fine for me. Thanks for this great patch. Hope this functionnality will be add in future release of Drupal....

kevinquillen’s picture

Note, this will not retain a color profile. If an image was saved out with Adobe RGB profile, it will be removed on webp conversion with image styles.

himynameisseb’s picture

#23 works fine for me too. Thanks!

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

sokolff’s picture

I can confirm that MR #30 works fine for Drupal 10.3.0. Thank you!

nielsonm’s picture

Status: Needs work » Needs review
StatusFileSize
new7.7 KB

Created a patch for 10.2.x as well.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new114 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

nielsonm’s picture

Status: Needs work » Needs review
StatusFileSize
new5.81 KB

Re-rolled patch for 10.3.x

smustgrave’s picture

Status: Needs review » Needs work

Still appears to need upgrade path. Also any fixes please put into an MR.

bl457xor’s picture

Added setting webp_quality config value on form submit, to #34 10.3.x patch.

danny-dee’s picture

Re-roll #36 for 10.2.4

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

sokru’s picture

Title: Allow WEBP quality to be configurable in GD2 image manipulation toolkit settings » Allow WebP quality to be configurable in GD2 image manipulation toolkit settings
Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs upgrade path, -Needs upgrade path tests

- Added upgrade path and tests for it.
- Changed the UI facing texts from "WEBP" to "WebP".
- Since Drupal 11 requires PHP 8.3, there's no need to check for support of IMG_WEBP_LOSSLESS
- Hide the outdated patches.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs screenshots, +Needs usability review, +Needs change record

UI changes should be documented in the summary so tagging for that.

Once issue summary is complete will probably need usability sign off for the new UI.

Also change would need a change record

sokru’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs screenshots, -Needs change record
StatusFileSize
new150.73 KB
new220.54 KB
new171.45 KB

I assume usability review could think following issues:
- Position of lossless checkbox on form.
- Need for lossless checkbox on form. Since the IMG_WEBP_LOSSLESS equals to 101 value ( see: https://php.watch/versions/8.1/gd-webp-lossless). We could remove the checkbox and treat the 100 selection same as lossless selection (This is how Intervention/image does it)

simohell’s picture

Usability review

We discussed this issue at #3474035: Drupal Usability Meeting 2024-09-20. That issue will have a link to a recording of the meeting. For the record, the attendees at last Friday's usability meeting were @AaronMcHale, @anmolgoyal74, @benjifisher, @rkoller, @shaal, @simohell, and @zetagraph.

If you want more feedback from the usability team, a good way to reach out is in the #ux channel in Slack.

Recommendations

Use radio-buttons to show both the options as equal choices. This will be clearer for the user and there even is an edge case, where a Safari browser supports lossy format but not lossless. Label them “WebP quality” to match JPEG quality as equal section.

WebP quality
- Lossless compression
- Lossy compression

As "Lossy compression" is selected display the numeric field indented visually to the same level with the radio-button.

WebP quality
- Lossless compression
- Lossy compression
[ 75% ] Set the image quality for WebP manipulations. Ranges from 0 to 100. Higher values mean better image quality but bigger files.

The label text is a suggestion, it was not decided by consensus in the meetiong.

It is also suggested to change the word “define” to “set” in the compression settings description.

Updating documentation

Include the information about WebP setting into documentation at https://www.drupal.org/docs/7/core/modules/image/working-with-images#s-s...

Add a note, that in some old browsers or operating systems versions it may be, that a browser supports the lossy compression but not the lossless compression.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: -Needs usability review +Usability

Thanks usability team!

the_g_bomb’s picture

Can I ask how does this affect webp conversions of jpegs in an image style?

Does the 75% get applied to the jpeg and then a further 75% when converted to webp?

tolstoydotcom’s picture

In my case, I might want to decide the quality on the fly. For instance, if an image is 100x100 I'd use a 90 quality, if it's 4000x4000, I'd use a 70 quality. I might also want different qualities for webp vs jpeg. I doubt if both are rare requests.

So, it'd be great if there were an object representing ImageFormatConfiguration or similar that would hold the quality for a specific format and could also hold other things later on. Those objects could then appear on admin/config/media/image-toolkit in a list and the user could add or remove such objects and change their values.

And, it'd be great if there was something like an ImageQualityEvent that would be fired before doing an operation. The event would have getQuality/setQuality in addition to a reference to the image and contextual data like where it's going to be stored, etc. The event would be created with the default quality, and then listeners could change that based on the dimensions, size, or location of the image.

Not to distract from the above, but on a related note, GDToolkit is confusing in that it appears to mix different functionalities together, like holding an image and saving that image. IMNSHO, it'd be good to split it into GDToolkitImage and GDToolkit (or GDToolkitManager or something). It also seems more inflexible than other parts of Drupal.

flyke’s picture

The MR does not apply to D11.3.0

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

trackleft2’s picture

Status: Needs work » Needs review
StatusFileSize
new237.59 KB

I've created a new merge request (!14253) that incorporates the changes from !8420, but makes the following improvements based on comment #42:

Form giving equal weight to form elements

Changes implemented:

  • Replaced checkbox with radio buttons for "Lossless compression" vs "Lossy compression" options
  • Wrapped both JPEG and WebP settings in fieldsets to give them equal visual weight
  • Used Form API #states to conditionally show/hide the quality field based on compression type selection
  • Added visual indentation (margin) to associate the quality field with the "Lossy compression" option
  • Removed redundant labels for cleaner UI
  • Changed "Define" to "Set" in field descriptions per usability feedback

The quality field now only appears when "Lossy compression" is selected.

Response to Comment #44
Good question! No, the quality settings are not applied cumulatively. Each format uses its own quality setting:

When saving a JPEG, it uses the JPEG quality setting (75% by default)
When saving a WebP, it uses the WebP quality setting (80% by default)
If you use an image style with a "Convert" effect to change a JPEG to WebP, the WebP quality setting applies to the final output. The JPEG quality only affects images saved as JPEG format. You can verify this in the GDToolkit.php:289-370 where each format's quality is handled separately based on the current image type.

Response to Comment #45
Those are interesting feature suggestions for more advanced quality management. However, they would represent significant architectural changes to the image toolkit system and are beyond the scope of this issue, which focuses on making WebP quality configurable (similar to how JPEG quality already is).

Your suggestions about:

Dynamic quality based on image dimensions
Format-specific configuration objects
Event-driven quality determination via ImageQualityEvent
...would be excellent topics for follow-up issues. The current implementation provides the foundation by making WebP quality configurable, and future enhancements could build on this.

trackleft2’s picture

Status: Needs review » Needs work

Test failure, setting to needs work.

trackleft2’s picture

Status: Needs work » Needs review

I've updated the test to account for our new form structure. All new and existing tests pass now.

trackleft2’s picture

StatusFileSize
new193.68 KB

Here is an additional screenshot to show the lossless option selected.
Lossless, no quality percentage

trackleft2’s picture

Here is a snippet we could add to the change record that describes how this could break existing sites.

Backward Compatibility Changes

Form API field paths have changed on the image toolkit configuration form:

The JPEG quality field on the image toolkit configuration form (/admin/config/media/image-toolkit) has been moved into a fieldset for visual consistency with the new WebP quality settings. This changes the form field path:

  • Old path: gd[image_jpeg_quality]
  • New path: gd[jpeg_quality_wrapper][image_jpeg_quality]

Impact:

  • Code using hook_form_alter() or hook_form_FORM_ID_alter() to modify the JPEG quality field on the toolkit configuration form must update the field path
  • Custom form submit handlers that read $form_state->getValue(['gd', 'image_jpeg_quality']) from the toolkit configuration form must update to $form_state->getValue(['gd', 'jpeg_quality_wrapper', 'image_jpeg_quality'])
  • Automated tests interacting with this form field must update their field selectors

Configuration keys remain unchanged:

The configuration keys stored in system.image.gd have not changed:

  • jpeg_quality (unchanged)
  • webp_quality (new)
  • webp_lossless (new)

Only the form structure has changed, not the underlying configuration storage.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new5.82 KB

The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

trackleft2’s picture

Status: Needs work » Needs review
Issue tags: +no-needs-review-bot

I am setting this back to needs review, and adding the no-needs-review-bot tag, because the review bot is testing the wrong merge request, and this MR needs maintainer review. I do not feel comfortable overwriting the existing merge request without some indication from the maintainers that this change to the form is in fact acceptable. If this one is acceptable, feel free to merge it, or say, please merge MR !14253 into MR !8420 I will do so.

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.

smustgrave’s picture

Status: Needs review » Needs work

MR appears to need a rebase please

jane_irwin changed the visibility of the branch 3320689-allow-webp-quality to hidden.

mradcliffe’s picture

Issue tags: +Chicago2026
jane_irwin’s picture

As part of DrupalCon Chicago 2026 Contribution day, heddn updated the branch to main and with Rishi Kulshreshtha's help I rebased the MR as smustgrave requested. The pipeline passed, so I think it's good to re-review and test.

I also updated the Change Record with the notes from trackleft2 in comment #53.

rishi.kulshreshtha’s picture

Status: Needs work » Needs review
igorgoncalves’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new72.99 KB

Im at Chicago 2026, just tested the MR @jane_irwin made with a clean Drupal 11.x on simplytest.me. and i can see the changes, new form behaviors and validation.

heddn’s picture

Status: Reviewed & tested by the community » Needs review

I've posted some random thoughts on the MR that could possibly be accommodated in some comment updates. Marking NR to see if anyone else agrees.

godotislate’s picture

Status: Needs review » Needs work

Thanks, everyone, for your work on this so far! I have some additional comments on the MR.