Compose one animation over another

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
StephenP

Compose one animation over another

Post by StephenP »

Hi all,
I'm having a bit of trouble with putting an animated GIF over another animated GIF. I've got it so that I can overlay an animation over a static image, but then I want to make an animated 'border' for the image, but I can't. Inexplicably, my code worked for a bit, then stopped working - maybe I changed something by accident? Anyways, here's the code, please tell me if you can see me doing anything wrong.

Basically, what I'm trying to do is this:
1) Take a PNG image [created by PHP]
2) Overlay a transparent animated GIF over that PNG [this part works fine]
3) Overlay the animation from step two over a slightly larger, glittery animation, so as to create a border effect.

Code: Select all

	shell_exec("convert glitter/".$_REQUEST['glitter']." -virtual-pixel tile -set option:distort:viewport ".$w."x".$h." -distort SRT 0 tile".$r.".gif"); //the transparent gif

	shell_exec("convert -size ".$w."x".$h." png_temp".$r.".png null: tile".$r.".gif -gravity Center -layers Composite -layers Optimize png".$r.".gif"); //transparent GIF over PNG - works fine
	if($_REQUEST['b'] != "none") // if a border was requested
	{
		shell_exec("convert border/".$_REQUEST['b']." -virtual-pixel tile -set option:distort:viewport ".($w+20)."x".($h+20)." -distort SRT 0 tile".$r.".gif"); //create the background animation
		shell_exec("convert -size ".($w+20)."x".($h+20)." tile".$r.".gif null: png".$r.".gif -gravity Center -layers Composite -compose src-over png".$r.".gif"); //animation 1 over the background animation
    }
However, it doesn't work properly - what I get instead of the mostly solid animation over the glittery one is that the background flickers back and forth between covering and uncovering the foreground image.

I think it might be an issue with how I'm using -layers and -compose, but I'm just at a loss :(
Thanks in advance,
Stephen
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compose one animation over another

Post by fmw42 »

This topic has been discussed before. Suggest you search the archives. But here is one that I recall was discussed before. Read Anthony's comments and look at his examples. Note that it is important that the two animations have the same number of frames and also have the same delays per frame; otherwise it becomes much harder.

viewtopic.php?f=1&t=10510&hilit=composite+animation

On quick examination of your code (I am not a PHP person), put the -compose src_over BEFORE the -layers composite.
StephenP

Re: Compose one animation over another

Post by StephenP »

Thanks for finding that, I'll try it out and report back. :)

-Stephen
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Compose one animation over another

Post by anthony »

merging (overlaying) two animations has one aspect which you have not mentioned.

Timing.

The -layers composite merges frames, it does not look at the timing of those frames.

Are the two animations in sync with the same time delays AND number of frames.

See the raw notes of the very last part of IM Examples Animation Modification page, where I note what needs to happen, though have not added an operator to IM to synchronize the times of an animation.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
StephenP

Re: Compose one animation over another

Post by StephenP »

Thanks for the advice :)
I've had some success (finally) doing it manually in the command line, by coalescing then multiplying each image in so that each list has equal numbers of frames. Hopefully, I can figure out a more intelligent way of doing it, as I don't want much lag in my PHP implementation.

-Stephen
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Compose one animation over another

Post by anthony »

Let us know what you come up with.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply