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.
Related
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.
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
How can I make a screenshot from every page of PDF file and save result as images in PHP? Is it possible?
Maybe the "make a screenshot" can be replaced for your purpose by "create a raster image" for each PDF page?
In this case you could use ImageMagick and/or one of its PHP-enabled libraries. Here is a command line representation:
convert some.pdf[15-19] some.png
This will convert not all pages, but the page range 16--20 (page counting here is zero-based (not intuitive, I know...). To convert all pages, just skip the [15-19] part.
The output PNG names will be some-0.png, some-1.png, ... some-4.png.
To create JPEG or GIF instead of PNG, simply use one of these:
convert some.pdf[15-19] some.jpg
convert some.pdf[15-19] some.gif
By default ImageMagick will use a resolution of 72 PPI. This will indirectly determine the image dimensions of the PNG/JPEG/GIF output. Should you need other output dimensions than the defaults, you have different options, for example:
either add -density
or add -resize
to the command line:
convert -density 200 some.pdf some.png
convert some.pdf -resize 50% some.png
I am using Imagemagick for resizing and cropping image.
Test Image :
I need to re-size it for 300 x 320 frame for this first I am resizing the image and then cropping it and i am using the following commands:
exec("convert /uploadImagePath -thumbnail 300 /newImagePath");
exec("convert /newImagePath -gravity Center -crop 290x310+0+0 /newImagePath");
But it gives me following image
As you can see image is not complete. Where am I mistaken?
(Answer is updated, providing an illustrated example for -liquid-rescale further below now)
Your original image's dimensions are:
489 x 640 pixels
Your desired dimensions seem to be:
290 x 310 pixels
This cannot scale to these dimensions without either:
cropping (do not keep all areas of the intial image)
keeping desired width (give up desired height)
keeping desired height (give up desired width)
distortion (do not keep the aspect ratio when scaling)
padding (add more pixels to one or more edges)
removing pixels where it's not obvious ("liquid rescale" or "seam carving" -- see Wikipedia)
Your result shows '1.' (cropping), which you don't like. So you have options '2.' (keeping width), '3.' (keeping height), '4.' (distortion), '5.' (padding) and '6.' (seam carving) left to test.
'2.': Keeping desired Height
convert WPTgp.jpg -resize x310 keep-height.jpg
Resulting Image has dimensions of 237 x 310 pixels.
Keep Height....
(determine width automatically)
'3.': Keeping desired Width
convert WPTgp.jpg -resize 290x keep-width.jpg
Resulting Image has dimensions of 290 x 380 pixels.
Keep Width.....
(determine height automatically)
'4.': Distortion
convert WPTgp.jpg -resize 290x310\! distorted.jpg
Resulting Image has dimensions of 290 x 310 pixels.
Distorted......
(ignore aspect ratio -- distort image if required to fit dimensions)
'5.': Padding
convert WPTgp.jpg \
-resize 290x310 \
-gravity center \
-background orange \
-extent 290x310 \
padded.jpg
Resulting Image has dimensions of 290 x 310 pixels. (Orange background was added only to demonstrate that the 'extention' of the image did work.)
Padded.........
(keep aspect ratio -- extend image for desired dimensions)
'6.': Seam Carving
convert WPTgp.jpg -liquid-rescale 290x310\! liquid.jpg
The above would be the command you'd spontaneously derive from quick-reading the ImageMagick command options reference. However, it doesn't work well, and instead I used:
convert WPTgp.jpg -liquid-rescale 599x640\! -scale 290x310 liquid.jpg
convert WPTgp.jpg -liquid-rescale 599x640\! -scale 48.4% liquid.jpg
Further below is an explanation why I needed to modify it....
Liquid-rescaled
Sorry -- I cannot provide example picture right now; this requires the additional ImageMagick delegate liblqr (liquid rescaling library) to be installed, which I don't have at this moment) I've now had the opportunity to create a 'liquidly rescaled' version of the original image.
Caveats about Seam Carving / '-liquid-rescale':
As stated above, the last image is not the result of my originally proposed command, but of one of these two modified versions:
convert WPTgp.jpg -liquid-rescale 599x640\! -scale 290x310 liquid.jpg
convert WPTgp.jpg -liquid-rescale 599x640\! -scale 48.4% liquid.jpg
Remember, we have an original image of 489x610 pixels, which we are expected to scale to 290x310 pixels. But -liquid-rescale isn't good at rescaling in two dimensions at once -- it's designed to scale into one direction only (horizontal or vertical). If you try to do both at once, results may not be what you'd expect. Here is the result for the originally proposed command:
convert WPTgp.jpg -liquid-rescale 290x310\! liquid.jpg
LQR gone wrong
That's why I came up with the two modified commands which work in two steps:
First, apply liquid rescaling to the horizontal dimension only, expanding the original's width from 489 pixels to 599 pixels.
Second, apply 'normal' aspect-ratio-keeping scaling to the intermediate result to produce the final image.
Try:
$inputFile = "WPTgp.jpg";
exec("convert {$inputFile} -resize 290x310^ -gravity Center -crop 290x310+0+0 picCropped.png");
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