Generate a image thumbnail without stretching it with ImageMagick 6.2.8 - php

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 .

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 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.

How to increase quality of image through imagemagick binary convert?

I'm working in an image conversion using imageMagick binary convert. When i resize a small image into a larger image and also increase the quality of image.
Here's my sample code:
$img = 'old_image.png';
$path1= 'new_img.png';
exec("convert $img -quality 100% -density 600 -resize 2480x3508 -depth 400 $path1");
When i used this command its working fine and it convert large image with loss of quality.
When i need to increase quality by using sharpen 50% code in exec command it doesn't create a proper image and no response in exec command.
$img = 'old_image.png';
$path1= 'new_img.png';
exec("convert $img -sharpen 99% -quality 100% -density 600 -resize 2480x3508 -depth 400 $path1");
Here I'm using image magick binary convert. How to achieve this image quality. Any help would be appreciated.
Think sharpen works like blur and takes an argument of {radius}x{sigma}
What version are you using? Maybe look at some of the docs that correspond to it...
http://www.imagemagick.org/Usage/blur

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

ImageMagick: What is this convert-command doing?

I'm trying to port a PHP script to Ruby and until now I only used ImageMagick to convert from one file-format to another. Meaning: Yes, I'm an ImageMagick newbie. ;-)
Somewhere inside the PHP script the following code is executed:
$output = array();
$returnValue = 0;
$cmd = 'convert '.$pngFile->path.' -resize 1x1 -alpha on -channel o -format "%[fx:u.a]" info:'
exec($cmd, $output, $returnValue);
Using the ImageMagick documentation for convert I identified the following options:
-resize 1x1 Resize to 1x1 pixels (right?)
-alpha on Activate alpha-channel
-channel o Apply options to the opacity image-channel
My questions:
What does -format "%[fx:u.a]" exactly do? I know that u is a symbol for first image in sequence and a one for alpha. But I don't get what the whole expression really does.
What does info: stand for?
What does this convert-command exactly do?
Thank you very much for your kind help.
Please note: The accepted answer on the following question has a very good answer to this question:
Understanding ImageMagick's convert and translating to Ruby RMagick
Seems like it is computing the average opacity. The info format is a dummy image format that will instruct convert to output image information to stdout (: means stdout) in the format %[fx:u.a]. Resizing to 1x1 is probably a way of averaging.

Categories