I have received the following reply on this from Bob Friesenhahn, GraphicsMagick Maintainer:
You did not say what version of GraphicsMagick you are using. Modern versions support a '-compose' option which may be put on the command line after the input file name to remember the composition algorithm to use. This composition algorithm is then used if the -mosaic or -extent operators are used to do a composition. You can also use a -page option after the input file name to locate the image when it is composited with prior images in the list. Due to a weakness in GM's convert command processing, the -mosaic or -extent operators must be the last command prior to saving the output file. I believe that ImageMagick's -composite must be a version of -mosaic which adds more features (e.g. -mosaic might not support gravity but -composite does). It seems like GraphicsMagick should implement something completely compatible with ImageMagick's -composite. Regardless, there is an effective workaround available if you need to use your existing GM commands. If you have a modern GraphicsMagick which supports 'gm batch', then you can use the 'mpr' coder ("Magick Persistent Registry") to remember intermediate images between commands and you can easily adapt your two commands to execute with full efficiency using the existing command lines. This Unix shell example should give you some ideas:
{
echo convert seaworld.jpg mpr:temporary
echo convert mpr:temporary crap.jpg
} | gm batch -prompt off -echo on
convert seaworld.jpg mpr:temporary
convert mpr:temporary crap.jpg
Notice that the output of the first command was saved (as an image handle as natively used within GraphicsMagick) into 'mpr:temporary' and then the second command took input from 'mpr:temporary' and wrote the final output file. You can use arbitrary string arguments to 'mpr:' so you can have several images "in flight". With this approach you can use 'gm convert' and 'gm composite' in the same batch command. I am not sure how one would best access this batch facility from PHP but if PHP can stream commands to it from a pipe, then it can run for quite a long time as a co-process to PHP and save considerable compute time and overhead.