Cannot convert PDF to JPG using ImageMagick and GhostScript - php

I'm trying to convert a pdf on my local computer to a jpg file and then using Tesseract to read the image text. Simply testing code like this:
$pdf = new Pdf('C:\testing\test.pdf');
$pdf->saveImage('C:\testing\test.jpg');
$tess = new TesseractOCR('C:\testing\test.jpg');
$myresults = $tess->run();
$this->vars['result'] = $myresults;
Produces the following error:
PDFDelegateFailed `The system cannot find the file specified. ' # error/pdf.c/ReadPDFImage/794
I am using the following for my scenario:
OctoberCMS/Laravel
PHP 7.2.34 64 bit
Ghostscript 9.53.3 64 bit (32 bit didn't work either)
ImageMagick 7.0.7-11 Q16 x64
Xampp
Windows 10 64 bit
When I try converting a jpg to a png, ImageMagick works fine, but as soon as I try a PDF, I get the error. I have looked and tried solutions with little luck. I know the issue is most likely Ghostscript, as many have reported that it's the one that gives the most headaches on Windows 10, but I have no idea where to even begin trying to connect it to ImageMagick.
Any help, please?

Well, I did end up finding a workaround for this. Instead of using ImageMagick with Ghostscript, I ended up just using Ghostscript on its own. The conversion works flawlessly without issues.

Related

Converting SVG to another format fail due to delegate not being found

I have PHP code running on Debian stretch with ImageMagick. It tries to convert SVG to another format. Here is how it starts
$im = new Imagick();
$im->readImageBlob($svg);
The variable $svg contains valid SVG code in a string. If I copy this string to a text file with a .svg extension then it opens just fine. But readImageBlob throws an exception saying no delegate for this image format.
I have seen similar questions solved by installing more packages to the system. But I've already installed libxml2-dev, librsvg2-bin, libmagickcore-6.q16-3-extra and libfreetype6-dev.
I have no idea what else I am missing.
I had to prepend the $svg with <?xml version="1.0" ?> and it worked. It least it does read in the SVG and attempts to create png/jpeg. One piece of text gets misplaced during convertion though. So the task in general is still failed. But this is another issue. I think the question is answered.
What do you get from running the following command from PHP exec()
convert -list format
The line SVG should say RSVG or MSVG/XML. Does it show that? if you need RSVG, you will have to install that delegate and then reinstall Imagemagick so that Imagemagick can find it. Imagemagick is used by Imagick. They are not the same. The RSVG delegate can be found by a Google Search or from linuxfromscratch.org/blfs/view/svn/general/librsvg.html. Your svg file seems to render properly for me in Imagemagick using RSVG, but I am not sure what it should look like. It is just a graph set of horizontal lines.
I do not know much about using readImageBlob(). Just use readImage(), where you supply the path to a saved svg image file. That should work. Try that and see what you get.
Here is what I get using RSVG 2.42.2 in Imagemagick 6.9.10.3 Q16 Mac OSX were I have saved your text in a file called test.svg.
convert test.svg test.png
If I force the use of the Imagemagick MSVG/XML, it does not look as good.
convert MSVG:test.svg test2.png

Creating/Saving DirectDraw Surface (DDS) Image via Imagick

This is the first time I use PHP Imagick and experiencing problems with it.
I'm creating a website module where users can upload images that need to be converted to an encrypted file type containing the uploaded image as a DDS texture.
So what I need to do is convert the uploaded image to dds file format.
Code:
$img = new Imagick('test.png'); //Load the uploaded image
$img->setformat('dds'); //Set the format to dds
$img->setImageCompression(Imagick::COMPRESSION_DXT3); //Set compression method
$img->writeimage('test.dds'); //Write/save the dds texture
The problem: the output file is always an empty, 0 byte file.
PNG That needs to be converted: test.png
DDS That needs to be the output: test.dds
Maybe I need other version Imagick or ImageMagick?
My old version of these are:
PHP 5.5.15
Imagick 3.1.2-5.5-ts-vc11-x86
ImageMagick 6.8.4-0-Q16-x86
I am currently testing on Windows 8.1 x64.
Any suggestion/help would be appreciated.
Edit: Edited the question, hopefully its clear now what I am trying to achieve.
Solved: The problem was the version of ImageMagick that had no support for DDS write.
Installed ImageMagick 6.8.6-10-Q16-x86 since the DDS write support was added in that version (newer versions caused the imagick not to be loaded).
Current version of what I use and works:
PHP 5.5.15
Imagick 3.1.2-5.5-ts-vc11-x86
ImageMagick 6.8.6-10-Q16-x86
From the debugging above the issue is that your version of ImageMagick doesn't support writing DXT3 compressed DDS files.
The options to solve this problem are:
Use a different compression format - DXT5 might be okay, but it's not exactly the same as DXT3.
Upgrade to a newer version of ImageMagick and Imagick that support writing in DXT3 format.

PHP-Imagick on OSX, ImagickException: NoDecodeDelegateForThisImageFormat `/my-path/file.jpg' # error/constitute.c/ReadImage/550

My environment is: MacOSX 10.8.3, PHP 5.3.17, ImageMagick 6.8.0, Apache 2.2.22
I have installed ImageMagick and PHP-Imagick module by using macports.
ImageMagick works as expected, for example I can convert a JPG to GIF by using the console command convert /my-path/file.jpg file.gif.
However when I create a new Imagick instance by using a sample code as follow:
<?php
$m = new Imagick('/my-path/file.jpg');
I get the exception:
NoDecodeDelegateForThisImageFormat `/my-path/file.jpg' #
error/constitute.c/ReadImage/550
The format of the file doesnt change the result; I tested different JPG, GIF and PNG images however same exception occurs.
So I suspect, the PHP/Apache - ImageMagick integration part is somewhat faulty.
I saw this post on SO and applied the given resolution (adding the environment variable "MAGICKCODERMODULE_PATH" with the value "/opt/local/lib/ImageMagick-6.8.0/modules-Q16/coders") however that didn't make sense for my case. (I confirmed the existence of this environment variable from my phpinfo)
Outputs of some commands on my system:
identify -list format: http://pastebin.com/tiE7Jr1m
identify -list configure: http://pastebin.com/Zt8yiRhj
I will appreciate any suggestion.
Solution
chmod 755 /opt
Insight and Thoughts
At last. I managed to solve the problem.
As there were no answers and suggestions on SO; I'm not suprised that something not expected was causing it!..
#sathia suggested to define an environment variable to call ImageMagick binaries. However in my case, I need to use phpImagick extension and access ImageMagick through its API, so this suggestion didn't make sense.
But I wanted to give it a try and call ImageMagick's convert binary by using PHP's system function. After calling it, I got a return value of "126"; and 126 means, binary is found however it's not executable!..
I checked the binary in /opt/local/bin/convert and it has appropriate execute permissions, then I checked /opt/local/bin and /opt/local directories as well and they were executable too. However the catch was /opt folder's permission was 700!
I managed to execute chmod 755 /opt and magically error has gone.
I've been there too, this is how I solved it:
<?php
define("IMAGE_MAGICK_PATH","/usr/local/bin/");
$in = '/my-path/file.jpg';
$out = '/my-path/file.gif';
$convertString = IMAGE_MAGICK_PATH." convert ".$yourfile." ".$out;
exec($convertString);
hope it helps

Imagick: PNG support available in command line, but not available in PHP

I found that I did not have PNG support in my Imagick installation, so I installed libpng. Now I can see that PNG format is supported when I run a command
convert -list format
And conversion from PNG to JPG works fine from command line:
convert a.png b.jpg
But when I try to read a PNG file in PHP:
$src = new \Imagick();
$src->readImageBlob( file_get_contents($file) ); // reading PNG format
-it gives me Error 500 ("...no decode delegate for this image format")
Also when I run phpinfo() - I see no PNG in list of Imagick's supported file formats. There are many others, but no PNG.
I tried to I resintall PHP Imagick driver and then restart apache - still no succeess.
PS: I have PHP 5.4 # CentOS
According to the installation requirements. "The amount of formats supported is by Imagick is entirely dependent upon the amount of formats supported by your ImageMagick installation." I imagine you’ll need to rebuild the PHP imagemagick extension. See here: http://php.net/manual/en/imagick.installation.php
Problem solved.
I used a guide for Imagick installation #CentOS from the link below, reinstalled everything exactly as it said there, and it helped - http://www.directadmin.com/forum/showthread.php?t=44449&page=1

Imagick not working

I am working on creating thumbnail image from the first page of pdf file. I almost tired every solution available on the web to make ImageMagick running but I still get the following error
Fatal error: Class 'Imagick' not found in C:\wamp\www\mediabox\application\controllers\cron.php on line 153
what I have done so far.
Installed GPL Ghostscript
Installed ImageMagick-6.7.6-Q16
Downloaded and copied the php_imagick.dll file in ext folder of my php installation.
Added extension=php_imagick.dll line to my php.ini file.
Restarted the apache server and windows many times :)
I am using codeigniter framework.
Php 5.3.10, apache 2.2.21, mysql 5.5.20, wamp 2.2 and win 7
On my localhost page in the loaded extensions category 'Imagemagick is not listed'
The code I have written is as
$image = explode('.', $filename_new);
$image_name = 'files/import/' . $ftp_upload["path"] ."/". $image[0] .".jpg";
//exec("convert -colorspace RGB -geometry 300x400! -density 200 " .$pdf_file."[0] ". $image_name);
$img = $image[0].".jpg" ;
$im = new Imagick($img);
$im->pingImage($img);
$im->readImage($img);
$im->thumbnailImage(100, null);
$im->writeImage( 'files/import/' . $ftp_upload["path"] ."/".$img);
$im->destroy();
The exec command create images but it is very slow I want to create thumbnail images using Imagick class and I hope it will be faster as compared to exec and convert thingy.
Any ideas what should I do to make the Imagick class available for my php ?
Thanks
I am not sure where you got your binaries from, but it seemed like something I experienced about a month ago. Some of the binaries I tried would show up in phpinfo(), but refused to work, whereas others wouldn't even show in phpinfo().
I then just went ahead and compiled my own binaries as per my question here.
I just went and compiled those binaries for you. They are for x86, compiled with VC9 and there are threadsafe and non-threadsafe versions. The version is 3.1.0RC1 (latest).
You can download them here: http://min.us/mtg0Z66BI
Let me know if it works for you :)
On my server the pecl-imagick extension only works with mod_mpm_prefork - it does not work with mod_mpm_worker.so or mod_mpm_event.so
That may be your issue.
If you are only creating jpg add the jpg hint to the exec( ) command which will be faster than Imagick.
Use -define jpeg:size=200x200 before reading the image in - 200x200 is the output image size.
I suspect Imagick is not installed correctly

Categories