THESE CODES ARE PROVIDED FOR AN EXAMPLE OF HOW TO USE IMAGEMAGICK WITH PHP. CARE SHOULD BE TAKEN WHEN ACCEPTING USER INPUT.
I TAKE NO RESPONSABILTY FOR ANY PROBLEMS THAT MAY OCCURE WHEN USING ANY OF THIS CODE.
IT IS UP TO YOU TO SECURE THE CODE AND VALIDATE USER INPUT.
Equivalent to a call to the -coalesce operator.
More information to follow.$cmd = " $input6 -layers Coalesce +repage "; exec("convert $cmd coalesce.png");
( Apply the layer disposal methods set in the current image sequence to form a fully defined animation sequence, as it should be displayed. Effectively converting a GIF animation into a 'film strip'-like animation. )
Crop the second and later frames to the smallest rectangle that contains all the differences between the two images.
More information to follow.$cmd = " $input6 -layers CompareAny +repage "; exec("convert $cmd compareany.png");
( No GIF -dispose methods are taken into account.
This exactly the same as the -deconstruct operator, and does not preserve animations normal working, especially when animation used layer disposal methods such as 'Previous' or 'Background'. )
As 'compare-any' but crop to the bounds of any opaque pixels which become transparent in the second frame.
More information to follow.$cmd = " $input6 -layers CompareClear +repage "; exec("convert $cmd compareclear.png");
( That is the smallest image needed to mask or erase pixels for the next frame. )
As 'compare-any' but crop to pixels that add extra color to the next image.
More information to follow.$cmd = " $input6 -layers CompareOverlay +repage "; exec("convert $cmd compareoverlay.png");
( As 'compare-any' but crop to pixels that add extra color to the next image, as a result of overlaying color pixels. That is the smallest single overlaid image to add or change colors. This can be used with the -compose alpha composition method 'change-mask', to reduce the image to just the pixels that need to be overlaid. )
This like 'coalesce' but shows the look of the animation after the layer disposal method has been applied, before the next sub-frame image is overlaid.
More information to follow.$cmd = " $input7 -layers Dispose +repage "; exec("convert $cmd dispose.png");
( That is the 'dispose' image that results from the application of the GIF -dispose method. This allows you to check what is going wrong with a particular animation you may be developing. )
Create a canvas the size of the first images virtual canvas using the current -background color, and -compose each image in turn onto that canvas.
More information to follow.$cmd = " -background skyblue 0.png 1.png 2.png 3.png -layers flatten +repage "; exec("convert $cmd flatten.png");
( Images falling outside that canvas is clipped. Final image will have a zero virtual canvas offset. This usally used as one of the final 'image layering' operations overlaying all the prepared image layers into a final image. For a single image this method can also be used to fillout a virtual canvas with real pixels, or to underlay a opaque color to remove transparency from an image. )
As 'flatten' method but merging all the given image layers into a new layer image just large enough to hold all the image without clipping or extra space.
More information to follow.$cmd = " -background skyblue 0.png 1.png 2.png 3.png -layers merge +repage "; exec("convert $cmd merge.png");
( The new images virtual offset will prevere the position of the new layer, even if this offset is negative. the virtual canvas size of the first image is preserved. Caution is advised when handling image layers with negative offsets as few image file formats handle them correctly. )
As 'flatten' method but expanding the initial canvas size of the first image so as to hold all the image layers.
More information to follow.$cmd = " -background skyblue 0.png 1.png 2.png 3.png -layers Mosaic +repage "; exec("convert $cmd mosaic.png");
( However as a virtual canvas is 'locked' to the origin, by defination, image layers with a negative offsets will still be clipped by the top and left edges. This method is commonly used to layout individual image using various offset but without knowning the final canvas size. The resulting image will, like 'flatten' not have any virtual offset, so can be saved to any image file format. )
Optimize a coalesced animation, into GIF animation using a number of general techniques.
More information to follow.$cmd = " $input6 -layers Optimize +repage "; exec("convert $cmd optimize.png");
( This currently a short cut to apply both the 'optimize-frame', and 'optimize-transparency' methods but may be expanded to include other optimization methods as they are developed. )
Optimize a coalesced animation.
More information to follow.$cmd = " $input6 -layers OptimizeFrame +repage "; exec("convert $cmd optimizeframe.png");
( Optimize a coalesced animation, into GIF animation by reducing the number of pixels per frame as much as possible by attempting to pick the best layer disposal method to use, while ensuring the result will continue to animate properly. There is no guarantee that the best optimization is found. But then no reasonably fast GIF optimization algorithm can do this. However this does seem to do better than most other GIF frame optimizers seen. )
As 'optimize-frame' but attempt to improve the overall optimization by adding extra frames to the animation, without changing the final look or timing of the animation.
More information to follow.$cmd = " $input6 -layers OptimizePlus +repage "; exec("convert $cmd optimizeplus.png");
( The frames are added to attempt to separate the clearing of pixels from the overlaying of new additional pixels from one animation frame to the next. If this does not improve the optimization (for the next frame only), it will fall back to the results of the previous normal 'optimize-frame' technique.
There is the possibility that the change in the disposal style will result in a worsening in the optimization of later frames, though this is unlikely. In other words there no guarantee that it is better than the normal 'optimize-frame' technique. For some animations however you can get a vast improvment in the final animation size.
)
Given a GIF animation, replace any pixel in the sub-frame overlay images with transparency.
More information to follow.$cmd = " $input7 -fuzz 3% -layers OptimizeTransparency +repage "; exec("convert $cmd optimizetransparency.png");
( Given a GIF animation, replace any pixel in the sub-frame overlay images with transparency, if it does not change the resulting animation by more than the current -fuzz factor. This should allow a existing frame optimized GIF animation to compress into a smaller file size due to larger areas of one (transparent) color rather than a pattern of multiple colors repeating the current disposed image of the last frame. )
Remove (and merge time delays) of duplicate consecutive images, so as to simplify layer overlays of coalesced animations.
More information to follow.$cmd = " $input6 -layers RemoveDups +repage "; exec("convert $cmd removedups.png");
( Usually this a result of using a constant time delay across the whole animation, or after a larger animation was split into smaller sub-animations. The duplicate frames could also have been used as part of some frame optimization methods. )
Remove any image with a zero time delay.
More information to follow.$cmd = " $input6 -layers RemoveZero +repage "; exec("convert $cmd removezero.png");
( Remove any image with a zero time delay, unless ALL the images have a zero time delay (and is not a proper timed animation, a warning is then issued). In a GIF animation, such images are usually frames which provide partial intermediary updates between the frames that are actually displayed to users. These frames are usally added for improved frame optimization in GIF animations )