Generate centered thumbnail from first frame of GIF with ImageMagick - php

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.

Related

ImageMagick File is too Big

I'm working with imagemagick to convert images from .tiff to .jpeg and make its thumbanils. The converstion from .tiff into .jpeg is OK but the problem comes when I want use imagemagick to move to other folders with others resolutions and creating its thumbnails. Imagemagick says that image in $path.$destinationPath.$ref.".jpeg" is too big but that image doesn't exist...
-Here it is my code:
exec("/usr/bin/convert ".$tiffPath.$ref.".tiff ".$CommonPath.$sourcePath.$ref.".jpeg");
// Ok until here
if (!file_exists($CommonPath.$destinationPath.$ref.".jpeg"))
{
$executa = "/usr/bin/convert -size 800x800 ".$CommonPath.$sourcePath.$ref.".jpeg -thumbnail 800x800 ".$path.$destinationPath.$ref.".jpeg";
exec($executa);
}
Imagemagick returns the following:
convert: unable to open image $CommonPath.$destinationPath.$ref.".jpeg": File is too big # error/blob.c/OpenBlob/2589.
Thanks in advance!
Your second ImageMagick command is malformed. You have:
$executa = "/usr/bin/convert -size 800x800 ".$CommonPath.$sourcePath.$ref.".jpeg -thumbnail 800x800 ".$path.$destinationPath.$ref.".jpeg";
The -size 800x800 is for creating a new image via xc: or canvas:. It may be confusing the command line to think you have two input images. It is certainly not needed. Try removing it, such as
$executa = "/usr/bin/convert ".$CommonPath.$sourcePath.$ref.".jpeg -thumbnail 800x800 ".$path.$destinationPath.$ref.".jpeg";

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 .

create a PDF catalog from image magick and php?

I have a set of 10 jpegs.
I need to generate 1 SINGLE PDF of these 10 images using PDF.
(1 page = 1 jpeg)
Final result must be a PDF.
Can imagemagick php extension help me with that ?
Yes, it can. Sample code:
<?php
$files = array(realpath('t1.jpg'), realpath('t2.jpg'));
$image = new Imagick($files);
$image->setImageFormat('pdf');
$image->writeImages(__DIR__ . '/file.pdf', true);
The second parameter of Imagick::writeImages() controls if the resulting output is joined into one file.
Footnote: At least on windows, Imagick needs to be used with absolute paths.
convert -density 150 -size 1239x1754 xc:white -density 150 *test.jpg -gravity center -composite test.pdf

ImageMagick incorrect dimensions

I've been on this problem for several hours now. I can't crop/resize a certain image correctly.
The source image has a dimension of 900x398 px
The target dimension is 650x178 px
but the returned dimension is 647x178 px. I dont't get it. This is the command I use:
/usr/bin/convert jpg:"/location/20-prefab_woningen.jpg" -auto-orient -shave 0x78 -resize 650x174 -colorspace RGB "location/new.jpg" &&exit
Is this a common bug? I can't find anything on the web about it. ImageMagick version doesn't seem to matter, tried both local and on the server but I get the same results.
resize tries to fit the image into the specified dimensions. It doesn't force it to exactly that size. See the manual.
Use the !flag to tell IM to ignore the aspect ratio.
/usr/bin/convert jpg:"/location/20-prefab_woningen.jpg"
-auto-orient -shave 0x78
-resize 650x174\!
-colorspace RGB "location/new.jpg" &&exit

PHP ImageMagick: How To Merge Animated GIF Into Static Canvas

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

Categories