Hmm,
The "-unique-colors" operator in the above was added to IM version 6.2.8-8, and converts an image into a smaller image containing just one pixel per unique color in the original image. It isn't strictly needed for the above, but it does make things easier, and uses far less disk space.
Finally using any IM before v6.2.9 is likely to continue to add extra 'local' colormaps, as soon as you use one of the later colormap optimizations. The result is that the above colormap optimization will become useless. So if you plan GIF work, keep you IM up-to-date.
Now the magickwand for PHP is 6.2.4 so I use
MagickQuantizeImages($im,216,MW_RGBColorspace,0,true,false);
MagickMapImage($stackIm,$im,1);
But the GIF is big yet