Problem/Motivation
Follow-up to #2330899: Allow image effects to change the MIME type + extension, add a "convert" image effect, #2313075: Allow users to upload webp files in image fields
For the moment an image effect can not change the extension of the derivative, this will be needed if site builders want to output the same source image in different formats.
Proposed resolution
Add ImageEffect::transformExtension($extensions) and ImageStyle::transformExtension($extensions), $extensions is an array like
array(
$from => '',
$to => '',
)
Remaining tasks
- Update ImageStyleDownloadController::deliver to check if the extension can be changed
- Update $image_style->buildUri() to use transformExtension
- Add code and tests for ImageEffect::transformExtension and ImageStyle::transformExtension
User interface changes
None
API changes
Yes, transformExtension
Comments
Comment #1
attiks commentedComment #2
jelle_sThis patch implements a slightly different solution to the proposed one.
If the extension is changed by ImageStyle::transformExtension, it is added to the filename (e.g. image.png -> image.png.jpg).
I also included a Convert effect for the GD toolkit. Everything seemed to work as expected on my local machine.
Comment #4
jelle_sNew patch, should fix the tests.
Comment #5
jelle_sAnd the interdiff.
Comment #6
jelle_sRe-upload for testbot
Comment #7
mondrakeNice! It would be lovely to have a solution in core to manage image format changes.
Just a couple of considerations:
1) on the style/effect level: IMHO, 'changing image format' implies that both the MIME type and the file extension are changed at the same time. It does not make sense to do one without the other. So how about having a single transformation method, that tackles both MIME type and extension, instead of two transformMimeType() and transformExtension()? My preference would be to have only transformMimeType().
2) on the toolkit level. I am wondering whether it would be possible to use the MIME type instead of a 'format' argument in the toolkit operation. In #2217783: Remove $extension and $mimeType from Image, use image_type_to_xxx function in the getters instead we did a cleanup as we had 3 different properties in Image all referring to the same concept. Now we get the mimetype from the image toolkit through a getter, but we have not had yet discussion what to do if we need to 'set' a image format in a toolkit abstract way (i.e. in GDToolkit we set the internal type, but how shall we do it on an interface level?). In #2063373: Cannot save image created from scratch I am proposing to use the mimetype in the arguments for a refactooring of the createTmp() method that could become an operation to start a new image in memory. I can change it to be 'format' too, but just thought to raise here for discussion - whatever it is, it should be the same in the two issues I think.
Comment #8
attiks commented#7
1/ You're right we can combine both, since #2330899: Allow image effects to change the MIME type + extension, add a "convert" image effect is older, we should combine both inside that issue. I'll leave this issue open for now so we can discuss the best approach.
2/ Does it make sense to commit #2063373: Cannot save image created from scratch first? I guess we need both MIME type and extension, since both jpg and jpeg have the same MIME type. Keep in mind that most of the build in php functions do not have support for webp, so we need to make sure we have a flexible system that does not depend on build in php constants.
Comment #9
jelle_sMarked this as a duplicate of #2330899: Allow image effects to change the MIME type + extension, add a "convert" image effect based on #7 and #8.
Comment #10
fietserwinThis is also a duplicate of (or actually "will be solved by") #2168511: Allow to pass save options to ImageInterface::save.