Allow to pass save options to ImageInterface::save

Created on 7 January 2014, over 11 years ago
Updated 3 June 2025, 4 days ago

Problem/Motivation

In D7 the contrib module imagecache_actions defines the "Change file format" image effect (submodule imagecache_colorations). This effect is able to alter the output format (and quality setting when the ImageMagick toolkit is in use) of an image derivative. However, in D8 in its current state, this is no longer possible.

What is the difference between D7 and D8 causing this?
In D7 you could get and change the image info array and the save method of the toolkit would use this info array to determine the image format to save to. In D8 the info array has been split over individual protected properties that are publicly exposed by getters only.

Note: this issue is part of #2105863: [meta] Images, toolkits and operations β†’ .

Proposed resolution

Instead of publicly exposing the info array or adding setters for the individual items in the info array, I suggest to add a saveOptions property or getSaveOptions() method to ImageInterfae. This property/method provides an alterable key-value store that can contain options that influence how an image is saved. Think of
- format (output as jpg, png or gif).
- quality for jpg.
- compression level and filters to use for png.
- progressive or not
- ...other?

This key-value store can be used by implementations of ImageToolkitInterface::save() to determine how to save the image.

The idea is that we change the API first with this additional property/method and then let toolkits decide what they accept, though we could (and should) document some keys that are foreseen (quality, format, compression, filter)

Why do it this way?
- This allows to fully utilize the abilities of the underlying toolkit, we are not restricting what parameters can be passed.
- It will make it easier to write a patch for issue #1310452: Allow to override the image toolkit's global JPEG/PNG quality value β†’ (or it will allow us to do it in contrib, if we fail to get that one committed in a timely manner).
- Time: do this API change now, fill in new options later (not being API changes, the latter can be done after beta).

Remarks:
- The png filter is defined differently by different toolkits. The PNG_FILTER_XXX constants are defined by GD and not available when GD is disabled and thus cannot be used by our image system. So a set of own constants that the different toolkits have to map themselves should
be part of introducing that option.
- This patch will only introduce the format and quality options as they are necessary to remove the regression. and don't need UI changes.
- I propose to introduce compression and filter in #1310452: Allow to override the image toolkit's global JPEG/PNG quality value β†’ as well as a UI (in image toolkit settings form) to define them.
- Setting to major as this is a regression from D7.

Remaining tasks

- review patch.

User interface changes

For now: none in the core UI.

API changes

Yes, but additions only.

πŸ“Œ Task
Status

Postponed: needs info

Version

11.0 πŸ”₯

Component

image system

Created by

πŸ‡«πŸ‡·France fietserwin Le Mont-Dore

Live updates comments and jobs are added and updated live.
  • Regression

    It restores functionality that was present in earlier versions.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

  • stale-issue-cleanup

    To track issues in the developing policy for closing stale issues, [Policy, no patch] closing older issues

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Thank you for creating this issue to improve Drupal.

    We are working to decide if this task is still relevant to a currently supported version of Drupal. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or is no longer relevant. Your thoughts on this will allow a decision to be made.

    Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

    Thanks!

Production build 0.71.5 2024