PHP Converting PDF's to images -dUseCropBox - php

I'm trying to convert a PDF to an image and I need to make sure that the -dUseCropBox parameter is specified for when calling Ghostscript. Can this be done?
convert "/var/www/vhosts/site.co.uk/httpdocs/uploads/source_pdf/PP4SDpdf.pdf" -resize 500X500 "/var/www/vhosts/site.co.uk/httpdocs/uploads/image_pdf/SaturdayTest.jpg"
It works well but just need to get the Ghostscript parameter in.

Is it acceptable for you to run Ghostscript directly (instead of having convert call it anyway) ?
I ask, because convert does not do the PDF => JPEG conversion by itself. It calls Ghostscript as its 'delegate' to do the job. So for convert to work you need to have access to a functional Ghostscript installation on that system anyway... .
But how to add custom parameters to converts commandline to pass them through to Ghostscript's commandline isn't easy to figure out. Ghostscript's commandline isn't exactly easy either, but at least it is fully documented at a well-known place (see Use.htm, Devices.htm and Ps2pdf.htm there).
Here is a command that would convert your input PDF to a series of JPEGs (one file for each PDF page). I'm assuming Windows -- for Linux just replace the ^ by \ and gswin32c.exe by gs:
gswin32c.exe ^
-o "d:/path with spaces/to/output/dir/input_page_%03d.jpeg ^
-sDEVICE=jpeg ^
-dJPEQ=95 ^
-r720 ^
-g5000x5000 ^
-dUseCropBox=true ^
"d:/path/to/input.pdf"
Explanation:
-dJPEGQ sets the JPEG quality. Accepts integer values in the range 0..100. Higher values create bigger files... (Ghostscript's default for JPEGQ is set to 75.)
-r720 sets a (rather high) resolution of 720dpi. Higher values create bigger files... (Ghostscript's default for its jpeg output device would be 72dpi.)
-g5000x5000 gives the file dimension in pixels. (Note: when decreasing the -r... value you MUST also accordingly decrease the -g... value to keep the same dimension in userspace inches or mm.)
You could also add -dPDFFitPage=true if that is useful for you.

The switch for imagemagick (the convert-command) is:
-define pdf:use-cropbox=true
see http://www.imagemagick.org/Usage/formats/#ps_reading

Related

Image sequence breaks in gif animation

I am trying to animate all images in a directory with imagemagick's morph operator. I am calling the command-line arguments with PHP's exec command. The images hold a specific file name such as 000000.jpg, 000001.jpg, 000002.jpg, 000003.jpg, and so on.
the following code works fine from image sequence 000000.jpg to 000009.jpg, but when I have images such as 0000010.jpg, the 0000010.jpg comes after 000001.jpg while I want 000002.jpg should come after 000001.jpg. Can you please point out how to modify the code so that I can retain a sequence in gif animation?
$command = "convert -set delay 5 -loop 0 $img_dir/00000*.jpg -morph 10 $img_dir/morph.gif";
exec($command);
Normally, the length of the name is kept constant, so if you add an extra digit in going from 9 to 10, you remove one of the leading zeroes to keep the length the same. I mean, most people do this:
000008
000009
000010
000011
whereas you appear to have done this where the length changes at 10:
000008
000009
0000010
0000011
I would suggest you change the program that generates the naming so it generates a constant length name then everything will work properly.
Failing that, if you wish to, or are obliged to, stick with your naming system, ImageMagick will accept a list of files in any order you like, so you can create a file called filelist.txt or somesuch, with contents like this
000000.jpg
000001.jpg
000002.jpg
...
000009.jpg
0000010.jpg
0000011.jpg
and use it like this:
convert -delay 100 -loop 0 #filelist.txt anim.gif
Alternatively, you could let bash print your filenames always with 5 leading spaces using a command such as this:
printf "00000%d.jpg " {0..4}
000000.jpg 000001.jpg 000002.jpg 000003.jpg 000004.jpg
and embed that in your ImageMagick command
convert -delay 100 -loop 0 $(printf "00000%d.jpg " {0..4}) anim.gif

Make a screenshot every page of PDF file

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

Combining 2 GraphicsMagick commands into 1 command, like in ImageMagick

I was using ImageMagick to create a new resized image with watermark, with this single command (in PHP):
exec("convert -filter Lanczos {$original_image} -thumbnail {$max_width}x{$max_height} -quality 90 {$watermark} -gravity center -unsharp 2x0.5+0.7+0 -composite {$cached}");
Now I switched to GM and am looking for a way to run 1 command to do the same task. The only way I found was to split it to 2 separate commands:
//create the resized image
exec("gm convert -filter Lanczos {$original_image} -thumbnail {$max_width}x{$max_height} -quality 90 -unsharp 2x0.5+0.7+0 {$cached}");
//apply the watermark and recreate the watermarked image, overwriting the previously resized image
exec("gm composite -quality 90 -dissolve 100 -gravity center {$watermark} {$cached} {$cached}");
Is there a way to combine them into 1 single command and by that maybe also reduce resources & drive usage?
I have received the following reply on this from Bob Friesenhahn, GraphicsMagick Maintainer:
You did not say what version of GraphicsMagick you are using. Modern
versions support a '-compose' option which may be put on the command
line after the input file name to remember the composition algorithm
to use. This composition algorithm is then used if the -mosaic or
-extent operators are used to do a composition. You can also use a
-page option after the input file name to locate the image when it
is composited with prior images in the list. Due to a weakness in
GM's convert command processing, the -mosaic or -extent operators must
be the last command prior to saving the output file. I believe that
ImageMagick's -composite must be a version of -mosaic which adds more
features (e.g. -mosaic might not support gravity but -composite does).
It seems like GraphicsMagick should implement something completely
compatible with ImageMagick's -composite.
Regardless, there is an effective workaround available if you need to
use your existing GM commands.
If you have a modern GraphicsMagick which supports 'gm batch', then
you can use the 'mpr' coder ("Magick Persistent Registry") to remember
intermediate images between commands and you can easily adapt your two
commands to execute with full efficiency using the existing command
lines. This Unix shell example should give you some ideas:
{
echo convert seaworld.jpg mpr:temporary
echo convert mpr:temporary crap.jpg
} | gm batch -prompt off -echo on
convert seaworld.jpg mpr:temporary
convert mpr:temporary crap.jpg
Notice that the output of the first command was saved (as an image
handle as natively used within GraphicsMagick) into 'mpr:temporary'
and then the second command took input from 'mpr:temporary' and wrote
the final output file. You can use arbitrary string arguments to
'mpr:' so you can have several images "in flight".
With this approach you can use 'gm convert' and 'gm composite' in the
same batch command.
I am not sure how one would best access this batch facility from PHP
but if PHP can stream commands to it from a pipe, then it can run for
quite a long time as a co-process to PHP and save considerable compute
time and overhead.

PHP imagick detect transparency

I want to be able to detect whether an image is transparent or not using the Imagick PHP extension.
So far, the only luck I've been having is to run the exec() / some other command, and use the ImageMagick command line tool to achieve this. Here's what I mean:
exec("identify -verbose example_transparent_image.png | grep \"Alpha\"", $output);
$is_transparent = !empty($output) ? true : false;
The logic is simple. Do a verbose check on the image in question: if the output contains any alpha information, that means it uses transparency.
It seems that the PHP imagick extension should have this as one of its commands, but the lack of documentation is killing me. It seems silly to have to run this kind of check each time.
Ahhh, solved (I think). Imagick has a function getImageAlphaChannel() which returns true if it contains any alpha information and false if it doesn't.
Make sure you have ImageMagick 6.4.0 or newer.
http://www.php.net/manual/en/function.imagick-getimagealphachannel.php
Maybe this
http://ru.php.net/manual/en/function.imagick-identifyimage.php
What's about this?
substr((new Imagick($FILE))->identifyImage()['type'], 0, -5) == 'Alpha'
look at the documentation of identifyImage. You will notice the missing documentation of the functions output. It's just a parsed version of
identify -verbose $FILE (from the imagick package)
where type identifies the image's type (compare source).
You can see that imagick returns the value from some MagickTypeOptions array which is defined here. This array contains an -Alpha and -Matte version for every image type if it's color palette contains alpha.
Theoretically you could save an image with such palette without using it, but every decent programm should swith to the non-alpha version in this case. But false positives are possible but should be rare.
Also I don't check for the -Matte image types because in the array is defined in a way that for every image type constant there are two entries with different names (-Alpha and -Matte), but as -Alpha comes first this name will be returned for that image type.

ImageMagick failing to convert to JPG

We recently installed the latest version of ImageMagick onto our Linux server. I seem to be having issues performing the most basic of tasks.
I am running this command line:
/usr/bin/convert /location/to/source/design.ai /location/to/save/output.jpg
Unfortunatly is saves design.jpg as an illustrator file (if I rename the file to output.ai it opens). Even if I do this:
/usr/bin/convert /location/to/source/design.ai -rotate 90 /location/to/save/design.jpg
It rotates the file and saves again as an illustrator document. This happens with all filetypes (e.g. png, bmp, etc...)
It appears ImageMagick cannot figure out what I want it converted to and just saves as the same file type.
Any ideas on fixing this?
Regards:
John
(Yes, McKay is properly right. This question would be better placed at serverfault.)
But I have an idea. By doing 'convert' only one gets a hint at the bottom:
To specify a particular image format, precede the filename
with an image format name and a colon (i.e. ps:image) or specify the
image type as the filename suffix (i.e. image.ps).
Perhaps convert gets confused by the path given.
So you could try this:
convert /location/to/source/design.ai output.jpg
or
convert /location/to/source/design.ai jpg:/location/to/save/output.jpg
Regards
Sigersted

Categories