PHP ImageMagick: How To Merge Animated GIF Into Static Canvas - php

i'm trying to add some animated gif into static image (canvas), here's my start code
exec("convert canvas.gif animated.gif result.gif");
with that code, animated gif will placed on top left canvas.
My question is how to make adjustment position of animated in canvas area and maybe resize this animated gif before merge?
Please note, with code above, will add 1 frame extra to result, i dont know how to fix this :(
Update:
My latest code on PHP
$cmd = "convert $animation -gravity center -geometry +0+5 null: $watermark -layers composite -layers optimize GIF:-";
header("Content-type: image/gif");
passthru($cmd, $retval);
Now frames are same with animated source, but sometimes GIF like loosing background frame :(
this is image example for finish result
http://i.stack.imgur.com/xxGzV.gif
How to make background frame looping forever?
Thanks and regards
nb: sorry for bad english ..lol

try:
convert background.jpg animation.gif -loop 0 new_animation.gif

Related

PHP ImageMagick change color in every frame Animated Gif

I am trying to change RGB values in an animated GIF to another RGB value. However in all my attempts it's only changing the color in one frame instead of all frames.
$imgif = new Imagick(HOME_PATH.'/images/6.gif');
$target = 'rgba(238,131,41, 1.0)';
$fill = 'rgba(163,145,144, 1.0)';
$fuzz = 0.05 * $imgif->getQuantumRange()['quantumRangeLong'];
$imgif->opaquePaintImage($target, $fill, $fuzz, false, Imagick::CHANNEL_DEFAULT);
$imgifblob = $imgif->getImagesBlob();
Is there anyway to index a color for the entire GIF and change it? I am rather lost as I don't have much experience with image manipulation.
Sorry, I do not know Imagick that well. But here is how to do it in Imagemagick command line. You may have to loop over each frame in Imagick, since it may not allow for multi-frame images to be processing. But perhaps using readImages would work. See https://www.php.net/manual/en/imagick.readimages.php, where you specify all frames in the gif as 6.gif[0--1], which is all frames from frame 0 to the last frame -1. Sorry, I just do not know. An Imagick expert might be able to help further.
Input:
convert bunny_anim.gif -coalesce -fuzz 10% -fill red -opaque "rgb(51,77,204)" -layers optimize new_bunny.gif

Cut any shape from image ( Imagik/Gd)

Is there any way to cut any shape from square image in PHP ?
Example, I have image with heart shape:
Another image in same size like heart.
Final image:
So my question is there way in PHP to make such effect from two images, or one image ?
You basically just want to copy the opacity of the heart template into the car picture. So, at the command-line, you would do:
convert motor.jpg heart.png -compose copyopacity -composite result.png
And in PHP:
#!/usr/local/bin/php -f
<?php
$template=new Imagick('heart.png');
$image =new Imagick('motor.jpg');
# Copy alpha from template over car image
$image->compositeImage($template,imagick::COMPOSITE_COPYOPACITY,0,0);
$image->writeImage('result.png');
?>

Generate centered thumbnail from first frame of GIF with ImageMagick

I have the following method to generate a thumbnail. It takes any dimensions and converts it to a 600 x 450 image, centered.
private function generateThumbnail ($ext, $name) {
if ($ext === ".png"){
exec('convert -define png:size=1200x900 '.$this->file.' -thumbnail 600x450^ -gravity center -extent 600x450 '.$name);
exec("optipng -o2 -q " . $name);
} else {
exec('convert -define jpeg:size=1200x900 '.$this->file.' -thumbnail 600x450^ -gravity center -extent 600x450 '.$name);
$img = new Imagick($name);
$img->setImageCompression(Imagick::COMPRESSION_JPEG);
$img->setImageCompressionQuality(97);
$img->stripImage();
$img->writeImage($name);
$img->destroy();
}
}
The problem arises when generating a thumbnail from a GIF. It doesn't generate an image from the first frame. One GIF just had a blank image (transparent), another had strange colors/quality.
How do I generate a thumbnail the first frame of a GIF, centered?
You could probably do them with the API but I do not use it as it is limited. But it looks like you still need to use the command line for the optipng program anyway so I would have carried on with the command line.
Anyway back to your problem; how do you know the first frames of the gif are not a transparent image or a strange image?
As #Mark Setchell said you should be able to specify a specific frame with input.gif[0] and I would try that with a different number and see what you get.
I am not an animation use but you should be able to see the gif frames with -coalesce. Anthony has some examples on his site: http://www.imagemagick.org/Usage/anim_basics/#coalesce
This should give you an idea of the frames in the gif:
montage script_k.gif -coalesce \
-tile x1 -frame 4 -geometry '+2+2' \
-background none -bordercolor none coalesce_k_montage.gif
You can see how you gif file is made and perhaps go for the second or third frame rather than the first.

Cropping an image using Jcrop and Imagemagick

I'm confused on how the -crop function works in Imagemagick.
I have the following values from Jcrop.
(x1,y1), (x2,y2), width and height.
And following command:
exec("convert $target_path -crop ".$w."x".$h."+$x+$y +repage $target_path");
Original image:
Result after crop:
My question is, How do I used the coordinates and dimensions from Jcrop, and use them with Imagemagick?
I have no idea what values you are passing into convert, but your command needs to look something like this to extract the light region -if that is your aim:
convert x.png -crop 240x240+120+100 out.png
The first 240 is the width of the cropped area, and the second 240 is its height. The 120 is the x-offset across from the top-left corner and the +100 is the y-offset down from the top.
Or, in general terms, you specify the crop like this
convert input.png -crop ${x}x${y}+${a}+${b} output.png
I feel like an idiot.
I had style='max-width:500px;' on my image during the crop. Removed the style and now it's working.

Generate a image thumbnail without stretching it with ImageMagick 6.2.8

Fill Area Flag ('^' flag) is support IM v6.3.8-3But my client's production server has version ImageMagick 6.2.8
Right now in my local server i use this command to generate thumbnail and it works fine:
convert image.jpg -resize "280x210^" -gravity Center -crop "280x210+0+0" thumbnail.jpg
Since my client's production server doesn't support '^' flag how can i generate a thumbnail without using it? (or maybe calculating it manually in PHP or BASH)
Should i use -extent, does it stretch the image?
I also read this and im not sure if ^ flag is for not letting the image stretch because thats what i want, generate a thumbnail without stretching it.
Note: i dont have root access on the server. Im using PHP and BASH to run the commands.
EDIT:
I also don't want any other background colors while resizing and croping.
try
convert image.jpg -background black -resize 280
-gravity center -crop 280x210+0+0 -extent 280x210 image.c.jpg
I found a solution
This is the PHP function i used:
function imgconvert($in,$out,$size){
$size_arr=explode('x',$size);
$resize=( ($size_arr[0]/$size_arr[1]) > 1.775 ? $size_arr[0].'x':'x'.$size_arr[1]);
system("convert \"$in\" -resize $resize -gravity Center -crop \"$size+0+0\" \"$out\"");
}
It seems that if width/height is larger than 1.775 i should use widthX as resize value and if else than i should use Xheight .

Categories