Problem/Motivation

I want to add a convert image style for change all image extension to WEBP, but when convert GIF to WEBP, it lost animation.Is the ImageMagick convert function don't support GIF to WEBP?Or my configuration not correct.

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

pockyl created an issue. See original summary.

mondrake’s picture

You’re probably hitting a limitation of Imagemagick itself here, see https://superuser.com/questions/1323522/how-can-i-convert-animated-gif-t...

internetter’s picture

You can test if your ImageMagick server command works with webp-animations, just try

convert source.gif -coalesce target.gif.webp

In Image-Toolkit settings you can also look if ImageMagick is supporting webp:

/admin/config/media/image-toolkit

then open "image formats" and look for the supported formats under "FORMAT LIST".

But I found a code part at:
web/modules/contrib/imagemagick/src/Plugin/ImageToolkit/ImagemagickToolkit.php:797

// When destination format differs from source format, and source image
    // is multi-frame, convert only the first frame.
    $destination_format = $this->arguments()->getDestinationFormat() ?: $this->arguments()->getSourceFormat();
    if ($this->arguments()->getSourceFormat() !== $destination_format && ($this->getFrames() === NULL || $this->getFrames() > 1)) {
      $this->arguments()->setSourceFrames('[0]');
    }

So it is always using only the first frame of every animation. Perhaps I can make an patch to change this.

bye
Martin

internetter’s picture

Perhaps this would be a solution:

// When destination format differs from source format, and source image
    // is multi-frame, convert only the first frame.
    $destination_format = $this->arguments()->getDestinationFormat() ?: $this->arguments()->getSourceFormat();
    if ($this->arguments()->getSourceFormat() !== $destination_format 
        && !in_array($destination_format, ['gif', 'png', 'webp']) 
        && ($this->getFrames() === NULL || $this->getFrames() > 1)
    ) {
      $this->arguments()->setSourceFrames('[0]');
    }
internetter’s picture

Here is it....

internetter’s picture

StatusFileSize
new968 bytes

Sourceformat is UpperCase. So this is better.

ravimalviya2000’s picture

Hello Team,

same issues i have found on my site also.is there any solution we have found for this issues.

dom.’s picture

Status: Active » Reviewed & tested by the community

RTBC for patch at #6.
Also, to get this to work, don't forget to activate the "-coalesce" option in the advanced settings (not really sure why this is not activated by default in the module?)

mondrake’s picture

Category: Support request » Feature request
Status: Reviewed & tested by the community » Needs work
Issue tags: -image styles (convert) +Needs tests

I do not think this is the right way to address this. It would introduce a behaviour change vs current state where only the first frame is converted for animated GIFs. We might argue whether the current state is the right one or not, but no matter what we cannot simply change the behaviour like this.

I'd rather go a path similar to #2786435: Support '-coalesce' of Animated GIF image files where a configuration setting was introduced for the purpose. So each site owner to decide.

We also need tests.

vredko’s picture

Status: Needs work » Needs review
StatusFileSize
new1.24 KB

thanks @internetter but patch#6 doesn't work for me.
i've created new patch. also checking "-coalesce" option which can be apply to gif and webp destination format.

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

trackleft2 changed the visibility of the branch 3295470-convert-image-style-cannot to hidden.

trackleft2’s picture

Status: Needs review » Needs work

The patch from #10 works for me, I've create a merge request with the patch so we can continue work on this.
Changing to Needs work, because the maintainer said this needs tests.

Read more about https://usage.imagemagick.org/anim_basics/#coalesced on the imagemagick site.

trackleft2’s picture

Status: Needs work » Needs review

I've added a PHPUnit test

joegraduate’s picture

Status: Needs review » Needs work
Issue tags: -Needs tests

Manually triggered the "tests only" pipeline on the MR and saw that the new test added passes even without the other MR changes which I don't think is the expected result.

Spoke to @trackleft2 about this on Slack and he mentioned that he thinks he needs to add something to the test that verifies the generated file is still actually a multiframe image.

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

berdir’s picture

I ported this to 5.x, but the changed part moved to a different place, which no longer seems to have access to getFrames(), so I wasn't sure how to handle it there and kept this part in the current location instead for now.

berdir’s picture

Status: Needs work » Needs review

I ported the MR to the 5.x branch, tests seem quite broken right now, apparently unrelated to this, but I manually tested it and it worked for my use case.

mondrake’s picture

Status: Needs review » Needs work

Rebuilt the MR by resetting to 5.0.x and reapplying the diff. There are legit PHPStan and test failures to fix. For PHPStan, it's enough to only target next major.

mondrake’s picture

Version: 8.x-3.x-dev » 5.0.x-dev

joegraduate changed the visibility of the branch 4.0.x to hidden.

joegraduate changed the visibility of the branch 5.0.x to hidden.