Gmagick set density before loading PDF - php

Converting a PDF to an image using Gmagick in PHP renders a very poor quality image.
The solution in Imagick was to call setResolution(x,y) before loading the PDF file. This would change the -density option.
There is no setResolution(x,y) in Gmagick, and unfortunately, calling setimageresolution(x,y) just throws an error:
PHP Fatal error: Uncaught exception 'GmagickException' with message 'Can not process empty Gmagick object'
Calling setimageresolution(x,y) after loading the PDF has no effect, and I can't find a way to set the -density option before loading the file.
EDIT: I would be happy for a way to set the default density system-wide. I do have root access.

I bumped into a similar problem and solved it with the following code:
$image = new \Gmagick();
$image->setresolution(300, 300);
$image->readimage('sample.pdf[0]');
Note that the setresolution method is not documented in PHP anywhere, but it seems to work – at least for me.

Related

Convert pdf to jpg using php Magick

I am looking for a solution to that problem, I am trying to convert pdf to image using php image magic, here is the code, I have tried many solutions but still I am not able to understand why it shows same issue every time
$FILE = realpath(__DIR__ . '/file.pdf');
$im = new Imagick();
$im->pingImage($FILE);
$pageCount = $im->getNumberImages();
echo $pageCount;
If I put jpg file in it, works fine, but if I add file.pdf it shows this error
Fatal error: Uncaught ImagickException: PDFDelegateFailed `The system
cannot find the file specified. ' # error/pdf.c/ReadPDFImage/794
Note: I have installed ghost script 9.52 64 bit , Imagick 7. something! , I am looking for the help
I had the same problem and after a week trying to fix, I discovered that this problem seems to be related to the recents Ghostscript version.
I solved removing it and installing the version 9.26 after trying all of those.

Uncaught ImagickException: UnableToOpenBlob `application/pdf': No such file or directory

I want to convert the PDF to JPG by using imagick, but after I press submit it keeps showing a fatal error: uncaught imagick error. Do you guys know if this is code problem or an imagick extension problem?
Your $_FILES['fileToUpload']['type'] stores the MIME type and you are assigning it to the variable $pdf_file and you are trying to read this with imagick. This can't work. You could var_dump($_FILES) and see if you can find the actual file in there.

Handling corrupt/truncated files with PHP gd-png

I'm working on a script that has to work on a large set of images and I need a graceful way to handle corrupt/truncated images in the set.
Currently my script works for all valid images but crashes when it runs into a truncated file. It must've sneaked into the folders but I want to handle this case nevertheless.
Its a *.png.tmp file and when feeding it to imagecreatefrompng(), it causes a fatal error and stops my script.
Fatal error: imagecreatefrompng(): gd-png: fatal libpng error: Read Error: truncated data...
It is recognized as PNG by both getimagesize() and exif_imagetype().
Since I can't handle it as an exception, is there a way to check if the file is valid without crashing my script and without relying on extensions in the filename?
I can live with skipping the image, but I don't know how to recognize it as a problem image before hitting the fatal error.
Did you try to suppress error by using # operator, and check if image is actually created?
foreach ($imagePaths as $imagePath)
{
$image = #imagecreatefrompng($imagePath);
if (!$image)
{
//maybe delete bad image?
unlink($imagePath);
continue;//do nothing in this iteration anymore
}
//do your magic here
}

PHP Imagick : RAF file not being processed

I am trying to read a RAF file in php using Imagick but somehow its not working. I have looked into the formats supported by Imagick and RAF is listed.
I later tried to simply read RAF file and echo it out but still no luck.
I keep on getting this error:
Uncaught exception 'ImagickException' with message 'unable to open image `/tmp/magick-H2rs1zGJ.ppm': No such file or directory
I tried rendering a png file saved in the same directory and it worked but somehow its not working for RAF.
$imagePath = "uploaded.raf";
$imagick = new \Imagick($imagePath);
header("Content-Type: image/jpg");
echo $imagick->getImageBlob();
Any help is appreciated.
Thank you!

PHP Imagick PDF to JPEG Problem

I am having this error that comes up when I try to convert certain pdfs into JPEgs
The error message is:
[28-Mar-2011 13:24:02] PHP Fatal error: Uncaught exception '
Stack trace:
#0 /home/bobdole/public_html/viewfile.php(41): Imagick->__con
#1 {main}
thrown in /home/bobdole/public_html/viewfile.php on line 41
The code is:
$im = new imagick($file_location);//Line 41 is here
$im->setImageFormat( "jpg" );
$pdf_pages=$im->getNumberImages();
Any idea what is causing the problem and how to fix it? It does not happen all the time, only with certain PDFs.
GhostScript is installed.
I spent a lot of time playing around with all kinds of PDF-files and imagemagick. This might help others trying to get it all to work. I've found so many solutions here, wouldn't even know how to thank everyone, so here it goes ;)
After a lot of useless tinkering, I figured it wasn't imagemagick which didn't work, but Ghostsript. I had to get the latest version of Ghostscript (i had to build it from source, the packages weren't new enough). I tested it all with pretty much all the available PDF-versions. Updating ghostscript worked pretty well, but still some PDF's were not accepted.
In the end I checked out the logs and discovered that ghostscript fonts are key to the issue. I updated those. After this, it all went smooth. Actually have a site which converts any PDF, right now.

Categories