Menu

Combining 2 commands into 1

Help
Mike Yo
2014-05-09
2014-05-14
  • Mike Yo

    Mike Yo - 2014-05-09

    Hi,

    I was using ImageMagick to create a new resized image with watermark, with this command (in PHP):

    exec("convert -filter Lanczos {$original_image} -thumbnail {$max_width}x{$max_height} -quality 90 {$watermark} -gravi
    ty center -unsharp 2x0.5+0.7+0 -composite {$cached}"); 
    

    Now I switched to GM and am looking for a way to run 1 command to do the same task. The only way I found was to split it to 2 separate commands:

    //create the resized image
      exec("gm convert -filter Lanczos {$original_image} -thumbnail {$max_width}x{$max_height} -quality 90 -unsharp 2x0.5+0.7+0 {$cached}");
      //apply the watermark and recreate the watermarked image, overwriting the previously resized image
      exec("gm composite -quality 90 -dissolve 100 -gravity center {$watermark} {$cached} {$cached}");
    

    Is there a way to combine them into 1 single command and by that maybe reduce resources usage?

    Thank you,
    Mike

     

    Last edit: Mike Yo 2014-05-09
    • Bob Friesenhahn

      Bob Friesenhahn - 2014-05-10

      On Fri, 9 May 2014, Mike Yo wrote:

      Hi,

      I was using ImageMagick to create a new resized image with watermark, with this command (in PHP):

      exec("convert -filter Lanczos {$original_image} -thumbnail {$max_width}x{$max_height} -quality 90 {$watermark} -gravi
      ty center -unsharp 2x0.5+0.7+0 -composite {$cached}");

      Now I switched to GM and am looking for a way to run 1 command to do the same task. The only I found was to split it to 2 separate
      commands:

      //create the resized image
      exec("gm convert -filter Lanczos {$original_image} -thumbnail {$max_width}x{$max_height} -quality 90 -unsharp 2x0.5+0.7+0 {$cach
      ed}");
      //apply the watermark and recreate the watermarked image, overwriting the previously resized image
      exec("gm composite -quality 90 -dissolve 100 -gravity center {$watermark} {$cached} {$cached}");

      Is there a way to combine them into 1 single command and by that maybe reduce resources usage?

      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.

      Bob

      Bob Friesenhahn
      bfriesen@simple.dallas.tx.us, http://www.simplesystems.org/users/bfriesen/
      GraphicsMagick Maintainer, http://www.GraphicsMagick.org/

       
  • Mike Yo

    Mike Yo - 2014-05-14

    Thanks, Bob, I am using GraphicsMagick 1.3.19 2013-12-31 Q8

     

    Last edit: Mike Yo 2014-05-14

Log in to post a comment.