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
Related
I am using Imagick::resizeImage to create a thumbnail PNG image of each page of a pdf file. However, the image I am getting back is really blurry. How can I make it a little sharper? Any pointers would be really appreciated.
I have tried adjusting the 'blur' paramter of Imagick::resizeImage between 0.1 - 1, without success.
$pdfPage = '1Mpublic.pdf[0]';
$im = new imagick($pdfPage);
$im->setImageFormat('png');
// Resize thumbnail image
$imgHeight = $im -> getImageHeight();
$imgWidth = $im -> getImageWidth();
$desiredWidth = 200;
$desiredHeight = resizedImageHeight($imgWidth, $imgHeight, $desiredWidth);
$im -> setResolution(1500, 1500);
$im -> resizeImage($desiredWidth, $desiredHeight, imagick::STYLE_NORMAL, 0.1);
/* Resize image */
function resizedImageHeight($imgWidth, $imgHeight, $desiredImgWidth){
$quoient = $imgWidth/$imgHeight;
$height = $desiredImgWidth/$quoient;
return $height;
}
original pdf link:
https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4905263/pdf/ksgt-06-04-1091539.pdf
Rather than rendering and then resizing the raster, it might be better to render the PDF to the right number of pixels in the first place. It'll be faster, and you can be sure that the amount of sharpness is correct for the content.
For example:
$ time convert -density 50 ksgt-06-04-1091539.pdf[0] x2.png
real 0m0.325s
user 0m0.299s
sys 0m0.024s
Makes:
The -density 50 makes a page about the same number of pixels across as your sample, 425.
In imagick you could do it like this (as #fmw42's excellent answer already says):
#!/usr/bin/env php
<?php
$target_width = 400;
# get natural size, calculate density we need for target width
$im = new imagick();
$im->pingImage($argv[1]);
$geo = $im->getImageGeometry();
$natural_width = $geo['width'];
$density = 72.0 * $target_width / $natural_width;
# load at correct resolution for target_width
$im = new imagick();
$im->setResolution($density, $density);
$im->readImage($argv[1]);
# and write back
$im->writeImage($argv[2]);
Doing both the ping and the read is a little slow in imagick, unfortunately:
$ time ./pdfthumb.php ksgt-06-04-1091539.pdf x.png
real 0m2.773s
user 0m2.737s
sys 0m0.036s
It's not imagick, but vipsthumbnail can do the ping and read in one operation:
$ time vipsthumbnail ksgt-06-04-1091539.pdf -s 400x -o x2.png
real 0m0.064s
user 0m0.064s
sys 0m0.011s
It might be worth considering if speed is important. libvips has a php binding so you can call it directly, but if you do that you'll run into awful licensing problems because it uses the GPL library poppler for PDF rendering, sigh. ImageMagick uses GhostScript and shells out to that for the same reason.
Unfortunately, I do not know Imagick that well. But in Imagemagick command line, I would do what is sometimes called supersampling. That is use a large density to read the PDF, then resize down by the inverse scale factor.
For example, nominal density is 72 dpi. I would read the input PDF at 4*72=288 dpi. Then after rasterizing, I would resize by 1/4=25% or for a larger result by something larger than 25%, say 50%. Here is your first page done both ways:
convert -density 288 ksgt-06-04-1091539.pdf[0] -resize 25% result1.png
convert -density 288 ksgt-06-04-1091539.pdf[0] -resize 50% result2.png
In Imagick, the key is something like:
$imagick = new Imagick();
$imagick->setImageResolution(288, 288);
$imagick->readImage('myfile.pdf');
I am looking for the simplest way of generating image preview from the first page of a pdf file. I have found a nice tutorial, however the code is not complete there. What is the missing part?
In ImageMagick from PHP exec()
exec("convert -density XX image.pdf[0] -resize YY% preview.png")
where XX is the desired density (nominal is 72) but I use something like 288=4*72) to get good quality and YY is the corresponding percent to reduce the image, such as 25%. The [0] means to process only the first page of the pdf.
exec("convert -density 288 image.pdf[0] -resize 25% preview.png")
Adjust these arguments to get the desired output size. You can change png to jpg, but you should then add -quality ZZ before the output to set the jpg compression quality. Values are 0 to 100, nominal for ImageMagick is to use 92.
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 .
I know this has been asked in a few different ways before but I am running into what seems to be a unique problem. I am using the following code to resize animated .gif images:
convert $image -coalesce $image
convert -layers OptimizeTransparency -quality 60 -size 220x220 $image -resize 220 +profile '*' $thumb`
In theory this should generate a thumbnailed/resized version of the .gif at 220px X 220px and keep it at a reasonable size by reducing quality and optimizing transparency, but in production it takes a 255kb .gif and turns it into a 1.5mb thumbnail. What is wrong with this code and how can I create more optimized .gif thumbnails?
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