1

I'm having the following code:

$animation = 'animation.gif';

$watermark = 'watermark.jpg';

$watermarked_animation = 'watermarked.gif';

$cmd = "$animation -extent 400x255 -coalesce -gravity southwest -geometry +0+0 null: $watermark -layers composite -layers optimize";

exec("convert $cmd $watermarked_animation", $output);

print_r($output);

And I want to output the image string to stdout instead of saving it to the file?

How can I achieve that?

Thank you in advance!

Marian
  • 1,154
  • 2
  • 15
  • 25
  • Also, you're actively providing convert with an output file (`$watermarked_animation`). Try replacing it with `-` to write to stdout. – ccKep May 13 '12 at 13:04
  • What does stdout mean in this case - I know nothing about Linux. Do you want to display the gif in the browser without saving it? – Bonzo May 13 '12 at 13:04
  • @ccKep: I don't want to use the php class of ImageMagick. I just need this one command. I also want to avoid 3rd party php scripts. – Marian May 13 '12 at 13:07
  • The library is not 3rd party, it's a wrapper around the same library that convert uses. Anyway, as has already been said: replace your output filename with a `-` and you should be good to go. – ccKep May 13 '12 at 13:08

2 Answers2

2

Have you tried putting a hyphen at the position of the target filename? Worked for me, didn't test it with your more complicated filename however..

So for instance this works nicely:

convert test.jpg -size 50x50 -

To take care of the charset give this a try:

exec('LANG=\"en_US.UTF8\" convert test.jpg -size 50x50 -');
André Hoffmann
  • 3,505
  • 1
  • 25
  • 39
  • It outputs to stdout, but the file looks corrupted when I use imagecreatefromstring. The good thing is that the file has the correct width and height. – Marian May 13 '12 at 13:15
  • You should be able to just echo it out with the correct header set, eg. `header('Content-Type: image/gif');` in your case. – ccKep May 13 '12 at 13:17
  • Adding `LANG=\"en_US.UTF8\"` stops the execution. As for echoing it out, I know it can be done, but it should have worked with `imagecreatefromstring` as well if something wasn't wrong. – Marian May 13 '12 at 13:23
  • I might have gotten the quotes wrong, you can give it another shot! – André Hoffmann May 13 '12 at 13:57
  • I found the problem: `exec` outputs each line without trailing whitespace, such as `\n`. – Marian May 13 '12 at 15:02
  • 1
    Using the `system` did the trick! Woohoo! :) – Marian May 13 '12 at 15:18
0

You can try this way too.

$im = new Imagick('image.jpg');
header("Content-type: image/jpeg");
$output = (string) $im;
echo $output;
Danny Hong
  • 1,474
  • 13
  • 21