Chaining Successful Commands

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
dunnma
Posts: 8
Joined: 2009-10-01T21:11:58-07:00
Authentication code: 8675309

Chaining Successful Commands

Post by dunnma »

I have 2 commands that function correctly independently. I have tried to use the miff command to chain them, but I get an error (below). I also cannot figure out how to "merge" them.

First: (pretty simple, just applies the overlay to image and saves as result)

Code: Select all

composite -gravity center  overlay.png  image.png  result.png
Second: (takes the result.png image and adds transparent rounded corners to it saving as final)

Code: Select all

convert result.png -resize 128x128! \
      -format 'roundrectangle 1,1 %[fx:w+0],%[fx:h+0] 10,10' \
      -write info:tmp.mvg \
      -matte -bordercolor none -border 1 \
      \( +clone -alpha transparent -background none \
         -fill white -stroke none -strokewidth 0 -draw @tmp.mvg \) \
      -compose DstIn -composite \
      \( +clone -alpha transparent -background none \) \
      -compose Over -composite final.png
  rm -f tmp.mvg
Here is the code and error from using miff:

Code: Select all

composite -gravity center  overlay.png  image.png  miff:- |\
convert -resize 128x128! \
      -format 'roundrectangle 1,1 %[fx:w+0],%[fx:h+0] 10,10' \
      -write info:tmp.mvg \
      -matte -bordercolor none -border 1 \
      \( +clone -alpha transparent -background none \
         -fill white -stroke none -strokewidth 0 -draw @tmp.mvg \) \
        -compose DstIn -composite \
      \( +clone -alpha transparent -background none \) \
      -compose Over -composite final.png

[b]Error: [/b]convert: image sequence is required `+clone' @ convert.c/ConvertImageCommand/865.
Can anyone show me how to combine these two?

Thanks!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Chaining Successful Commands

Post by snibgo »

"miff:" is a file format, not a command. The "-" means send it to stdout. You need a corresponding "-" in the next convert to read from stdin.

convert - -resize 128x128! \
etc
snibgo's IM pages: im.snibgo.com
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Chaining Successful Commands

Post by el_supremo »

An alternative method would be to do the composite within the convert command so that you don't need to call the composite command at all.
The first line of the convert would become something like this:

Code: Select all

convert -gravity center image.png overlay.png  -compose -resize 128x128! -gravity northwest\
The -gravity at the end changes the gravity back to the default so that the later composites work properly.
Also note that when using the convert program the two images of a composite are specified in the opposite order.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
dunnma
Posts: 8
Joined: 2009-10-01T21:11:58-07:00
Authentication code: 8675309

Re: Chaining Successful Commands

Post by dunnma »

Thanks gents...I did the first thing (silly me forgot the -). Now my problem is that when I run the command from the command line it works, when I try to do it through php's shell_exec it does not. Here is my code:

Code: Select all

$output = shell_exec( "/usr/bin/composite -gravity center  /full/path/to/overlay.png  /full/path/to/image.png miff:- |\
/usr/bin/convert - -resize 128x128! \
      -format 'roundrectangle 1,1 %[fx:w+0],%[fx:h+0] 10,10' \
      -write info:tmp.mvg \
      -matte -bordercolor none -border 1 \
      \( +clone -alpha transparent -background none \
         -fill white -stroke none -strokewidth 0 -draw @tmp.mvg \) \
      -compose DstIn -composite \
      \( +clone -alpha transparent -background none \) \
      -compose Over -composite /full/path/to/test.png
  rm -f tmp.mvg");

		echo 'Output: '.$output.'<br /><br /><img src="test.png" /><br /><br />';
No image is created and there is no "output" from the command line. I have checked the permissions on both the source image and the folder being written to and even made sure they are 777 and I still get nothing. Any clue on this one?

Should I post this as a separate topic under Users?
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Chaining Successful Commands

Post by el_supremo »

Ooops. There's a mistake in my version - it should be "-composite" not "-compose":

Code: Select all

convert -gravity center image.png overlay.png  -composite -resize 128x128! -gravity northwest\
Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Chaining Successful Commands

Post by fmw42 »

I am not an expert on PHP, but it may not allow you to pipe commands. Seems like the pipe may be replacable by clones and other parens, but I have not studied your command that well.

Or perhaps you need to use exec rather than shell_exec?

One of the PHP experts needs to comment.

See my reply to viewtopic.php?f=1&t=11270&start=15#p54939 and to viewtopic.php?f=1&t=15525

They may help you.

Post a link to your input images and your good output image and we can see what to do about helping.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Chaining Successful Commands

Post by snibgo »

I would do the obvious things, like ensuring that some IM command works, then splitting this one into two so it doesn't use pipes, and so on.
snibgo's IM pages: im.snibgo.com
dunnma
Posts: 8
Joined: 2009-10-01T21:11:58-07:00
Authentication code: 8675309

Re: Chaining Successful Commands

Post by dunnma »

Thanks for the help guys. Actually the problem is that when running it in the shell_exec function it doesn't like it broken up on multiple lines.

i.e. this doesn't work:

Code: Select all

shell_exec( "/usr/bin/composite -gravity center  /full/path/to/overlay.png  /full/path/to/image.png miff:- |\
/usr/bin/convert - -resize 128x128! \
      -format 'roundrectangle 1,1 %[fx:w+0],%[fx:h+0] 10,10' \
      -write info:tmp.mvg \
      -matte -bordercolor none -border 1 \
      \( +clone -alpha transparent -background none \
         -fill white -stroke none -strokewidth 0 -draw @tmp.mvg \) \
      -compose DstIn -composite \
      \( +clone -alpha transparent -background none \) \
      -compose Over -composite /full/path/to/test.png
  rm -f tmp.mvg");
this does:

Code: Select all

shell_exec( "/usr/bin/composite -gravity center  /full/path/to/overlay.png  /full/path/to/image.png miff:- | /usr/bin/convert - -resize 128x128! -format 'roundrectangle 1,1 %[fx:w+0],%[fx:h+0] 10,10' -write info:tmp.mvg -matte -bordercolor none -border 1 \( +clone -alpha transparent -background none -fill white -stroke none -strokewidth 0 -draw @tmp.mvg \) -compose DstIn -composite \( +clone -alpha transparent -background none \)  -compose Over -composite /full/path/to/test.png");
Notice how the second one doesn't user the \ to put things on a new line? Food for thought and hopefully this helps others down the road.

Thanks again!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Chaining Successful Commands

Post by snibgo »

Ah, yes, of course. "\" is bash, not PHP. And I'm not sure if you need to escape the brackets. Splitting the lines in PHP would be something like:

Code: Select all

shell_exec( <<<theEnd
  /usr/bin/composite -gravity center  /full/path/to/overlay.png  /full/path/to/image.png miff:- |
  /usr/bin/convert - -resize 128x128!
      -format 'roundrectangle 1,1 %[fx:w+0],%[fx:h+0] 10,10'
      -write info:tmp.mvg
      -matte -bordercolor none -border 1
      ( +clone -alpha transparent -background none
         -fill white -stroke none -strokewidth 0 -draw @tmp.mvg ) 
      -compose DstIn -composite 
      ( +clone -alpha transparent -background none ) 
      -compose Over -composite /full/path/to/test.png
  rm -f tmp.mvg
theEnd
);
snibgo's IM pages: im.snibgo.com
dunnma
Posts: 8
Joined: 2009-10-01T21:11:58-07:00
Authentication code: 8675309

Re: Chaining Successful Commands

Post by dunnma »

Yeah...I figured that was it...although I thought that calling shell_exec would actually run the command as a bash command. Go figure. :?

Hopefully this might help out others if the come this way.
Post Reply