Add watermark to animated gif with PHP ImageMagick library - php

This works fine for none animated images:
$image = new Imagick($file);
$format = strtolower($image->getImageFormat());
$watermark = new Imagick('watermark.png');
$watermark->scaleImage($image->getImageWidth(), $watermark->getImageHeight());
$image->addImage($watermark);
$image->resetIterator();
$combined = $image->appendImages(true);
$data = base64_encode($combined->getImageBlob());
echo '<img src="data:image/' . $format . ';base64,' . $data . '">';
How can I make this work for animated gifs?
It just puts every frame below each other and the watermark below that.

See below URL i am very sure it is very help full to you:-
Watermark on animated gif with php
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 ");
See also
http://www.imagemagick.org/Usage/anim_mods/#composite
http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=21730

Related

Allow all special characters using imagemagick

I'm using Imagemagik in PHP codeigniter, I want to allow all special character, smiley etc.. but it's not working. Can anyone help me how to do this. Here is my code.
$textonimage = $this->input->post('textonimage');
if(!empty($background)){
$string = $textonimage;
$smart_wordwrap_info = smart_wordwrap($string, 20);
$height_1 = substr_count( $smart_wordwrap_info, "\n" );
$color_string_length = strlen($colors);
if($color_string_length > 7){
$colors_result = substr($colors, 3);
$colors = '#'.$colors_result;
}
$height_2 = $height_1+1;
$height_3 = $height_2-2;
$height_4 = 30*$height_3;
$height = 200+$height_4;
$image = new Imagick();
$draw = new ImagickDraw();
$pixel = new ImagickPixel( $colors );
$image->newImage(350, $height, $pixel);
$image->setImageFormat('png');
$image->writeImage(FCPATH.'uploads/post-image/my_output.png');
$process_file_path = FCPATH.'uploads/post-image/my_output.png';
$target_file_name = mt_rand(10000,99999).'.png';
$target_path = FCPATH.('uploads/post-image/'.$target_file_name);
$target_path_1 = base_url().'uploads/post-image/'.$target_file_name;
exec("convert ".$process_file_path." -gravity center -fill white -font Corsiva -pointsize 30 -interline-spacing 40 -annotate +0+0 '".$smart_wordwrap_info."' ".$target_path);
$upload_image[] = $target_path_1;
#unlink($process_file_path);
}
I tried something like this, but it's not working.
exec("convert ".$process_file_path." -gravity center -fill white -font Corsiva -pointsize 30 -interline-spacing 40 -annotate +0+0 '".($smart_wordwrap_info |sed "s/'/\'/g)."' ".$target_path);

ImageMagick exec commands to reduce noise on white background to PHP Imagick

I need this line:
magick "input.jpg" -fuzz 4% -transparent white -background white -alpha remove -alpha off -quality 85% "result.jpg"
to PHP imagick:
$img = new Imagick($root . '/testall/imagemagick/input.jpg');
// ???
$img->setImageFormat('jpg');
$img->setImageCompressionQuality(85);
$img->writeImage($root . '/testall/imagemagick/result.jpg');
and I can't find fuzz in https://www.php.net/manual/de/book.imagick.php to even start with.
Please help
You might be able to use: Imagick::transparentPaintImage:
$img = new Imagick($root . '/testall/imagemagick/input.jpg');
$fuzz = 4 * \Imagick::getQuantum(); // Copied from the example?
$img->transparentPaintImage('white', 0, $fuzz, false);
...
Well I tried around with andlrc answer and this is the working result:
$img = new Imagick('input.jpg');
$fuzz = Imagick::getQuantum() * ($whitePercentage / 100);
$BackgroundColor = "rgb(255, 255, 255)";
$img->setImageFormat('png');
$img->transparentPaintImage($BackgroundColor, 0, $fuzz, false);
$img->setimagebackgroundcolor('white');
$img = $img->mergeImageLayers( imagick::LAYERMETHOD_FLATTEN );
$img->setImageFormat('jpg');
$img->setImageCompressionQuality($qualy);
$img->writeImage($root . '/testall/imagemagick/result.jpg');

How to resize an animated gif with ImageMagick (php)?

I cannot find a working example. This one is not working, it's not animated:
$imagick = new Imagick($_FILES['file']['tmp_name']);
$format = $imagick->getImageFormat();
if ($format == 'GIF') {
$imagick = $imagick->coalesceImages();
do {
$imagick->resizeImage(500, 500, Imagick::FILTER_BOX, 1);
} while ($imagick->nextImage());
$imagick = $imagick->deconstructImages();
$imagick->writeImages($name, true);
}
$imagick->clear();
$imagick->destroy();
In ImageMagick command line, this works for me
Original Animation:
convert animation.gif -coalesce -resize 500x500 -layers optimize -loop 0 resize.gif
Did you do the equivalent of -coalesce after reading your input animation? Did you do the optimize step after resizing?
Sorry, I do not use PHP Imagick or any other API.

How to add on my image gif's watermark with php?

I try this:
$path = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];
$image = imagecreatefromstring(file_get_contents($path));
$w = imagesx($image);
$h = imagesy($image);
$centerX=round($w/4);
$centerY=round($h/2);
$watermark = imagecreatefrompng('lool-face.png');
$ww = imagesx($watermark);
$wh = imagesy($watermark);
imagecopy($image, $watermark, $centerX, $centerY, 0, 0, $ww, $wh);
eregi('\.(gif|jpeg|jpg|png)$',$path,$regs);
switch( $regs[1] ) {
case 'gif':
header('Content-type: image/gif');
imagegif($image);
break;
but this don't working, the images is curdle.
And try this:
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);
This code does not work I do not know why it does not.
We wish you a directory where there Gif htaccess can do it in gifs watermarking
This class could be your solution:
PHP Classes transparent watermark
Take a look at the link below. I have done the same thing in php. I hope that this will helps you.
https://dl.dropboxusercontent.com/u/58146160/watermark%20image.rar

Watermark on animated gif with php

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');
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 ");
<?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.

Categories