duan1983 2014-05-10 10:04
浏览 116
已采纳

将2个GraphicsMagick命令组合成1个命令,就像在ImageMagick中一样

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

exec("convert -filter Lanczos {$original_image} -thumbnail {$max_width}x{$max_height} -quality 90 {$watermark} -gravity 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 also reduce resources & drive usage?

  • 写回答

1条回答 默认 最新

  • doutang3760 2014-05-17 20:46
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序