1

I need a function that adds a watermark to an animated gif. How I can do that with PHP and ImageMagick?

This is my code, but it doesn't work.

function IMagickWatermark($sf, $sfDest, $sfLogo, $sGravity='southeast'){
        //$sCmd = "composite -compose bumpmap -gravity $sGravity $sfLogo \"$sf\" $sfDest";
        $sCmd = "convert \"$sf\" -coalesce -gravity south -draw 'image over 0,0 0,0 \"$sfLogo\"' \"$sfDest\"";
        exec($sCmd);
        return 1;
    }
$sf = $_SERVER['DOCUMENT_ROOT']."/test/source.gif";
$sfDest = $_SERVER['DOCUMENT_ROOT']."/test/dest.gif";
$sfLogo = $_SERVER['DOCUMENT_ROOT']."/test/logowithtext.gif";
IMagickWatermark($sf, $sfDest, $sfLogo, $sGravity='SouthEast');
hakre
  • 193,403
  • 52
  • 435
  • 836
Nikoole
  • 4,553
  • 4
  • 18
  • 23

2 Answers2

15

Try this:

$animation = ani.gif; 

$watermark = logo.png; 

$watermarked_animation = "morph.gif"; 

$cmd = " $animation -coalesce -gravity South ". 
" -geometry +0+0 null: $watermark -layers composite -layers optimize "; 

exec("convert $cmd $watermarked_animation "); 

enter image description here

Bonzo
  • 5,169
  • 1
  • 19
  • 27
0
<?php
shell_exec('for i in sphere*.gif; do convert $i  -font Arial -pointsize 20 \
      -draw "gravity south \
             fill black  text 0,12 \'Copyright\' \
             fill white  text 1,11 \'Copyright\' " \
      wmark_$i; done');

shell_exec("convert   -delay 20   -loop 0   wmark_sphere*.gif   animatespheres.gif");
$f = fopen("animatespheres.gif", 'rb');
fpassthru($f);
fclose($f);
?>

if you need to debug, remove the fopen,fpassthru,fclose lines and add echo's for the shell_execs.

Janus Troelsen
  • 20,267
  • 14
  • 135
  • 196
  • What kind of files: sphere*.gif wmark_sphere*.gif animatespheres.gif Where is source, destination, file with logo or text..? – Nikoole May 10 '12 at 10:22
  • ``sphere.gif`` are your input files. ``wmark_sphere.gif`` are generated temporary files. ``animatespheres.gif`` is your result. there is no file with logo or text, it is dynamically generated. if you want to watermark using an image, read this: http://www.imagemagick.org/Usage/annotating/ – Janus Troelsen May 10 '12 at 10:30
  • It dont work: ` ini_set("display_errors","1"); ini_set("display_startup_errors","1"); ini_set('error_reporting', E_ALL); $sf = $_SERVER['DOCUMENT_ROOT']."/test/d1cdcdf2cf6dba7a12511d6077783895.gif"; shell_exec("for i in $sf; do convert $i -font Arial -pointsize 20 \ -draw \"gravity south \ fill black text 0,12 'Copyright' \ fill white text 1,11 'Copyright' \" \ wmark_$i; done"); shell_exec("convert -delay 20 -loop 0 wmark_sphere*.gif animatespheres.gif"); fpassthru("animatespheres.gif"); ?>` that code: http://gifbox.ru/test/index.php – Nikoole May 10 '12 at 10:49
  • try now, i edited it to fix the quotes. try to understand the code. $a in a string quoted with double quotes will not work in php cause it thinks it's a php variable, even when it's not. that's why i exchanged the quotes. – Janus Troelsen May 10 '12 at 11:15
  • You wrote: try executing it on the command line (CLI) from a shell on your host machineconvert. I try to do that with command: "/var/www/gifbox.ru/test/d1cdcdf2cf6dba7a12511d6077783895.gif" -coalesce -gravity south -draw 'image over 0,0 0,0 "/var/www/gifbox.ru/test/gifboxru.gif"' "/var/www/gifbox.ru/test/_d1cdcdf2cf6dba7a12511d6077783895.gif" and gif with watermark is created! Byt when i run php script - it doesnt created. – Nikoole May 10 '12 at 11:18
  • I try: Warning: fpassthru() expects parameter 1 to be resource, string given in /var/www/gifbox.ru/test/index.php on line 24 – Nikoole May 10 '12 at 11:23
  • you might have a permissions issue. try running ``chmod -R ugo+rwx .`` from the directory where the images and script are located (``test``) – Janus Troelsen May 10 '12 at 11:31
  • i set permissions for file and folder to 777. When i run script with: `$f = fopen("animatespheres.gif", 'rb');` - iget an error (file animatespheres.gif - does not exixt), but then i run script with: $f = fopen("animatespheres.gif", 'w'); and file was created with size=0b. Then i run with: `$f = fopen("animatespheres.gif", 'rb');`, but filesize = 0b. – Nikoole May 10 '12 at 11:50
  • Thank you very much for trying to help! User Bonzo suggested a working solution. Once again, many, many thanks! – Nikoole May 10 '12 at 11:57