I have a script that uploads a newsletter and then using ImageMagick creates a thumbnail image of the front page. It works fine on my Linux host, but on my localhost I get an error
Fatal error: Uncaught ImagickException: UnableToOpenBlob ‘…/newsletter/newsletter-02038683966.pdf’: No such file or directory @ error/blob.c/OpenBlob/3315 in C:\laragon\www\website\adminbl\upload-newsletter.php:57 Stack trace: #0 C:\laragon\www\website\adminbl\upload-newsletter.php(57): Imagick->readimage(‘…/newsletter/n…’) #1 {main} thrown in C:\laragon\www\website\adminbl\upload-newsletter.php on line 57
This is the bit that creates the image
$imgfile = $destdir.'newsletter-'.$number.'.jpg';
$imagick = new Imagick();
$imagick->setResolution(150, 150);
$imagick->readImage($destpath.'[0]'); // <==== line 57
$imagick->thumbnailImage(150, 212, true, true);
$imagick->writeImages($imgfile, false);
$destpath is ../newsletter/newsletter-02038683966.pdf
I’ve tried wrapping $desdir and $destpath in realpath() but then I get another error
Fatal error: Uncaught ImagickException: PDFDelegateFailed `The system cannot find the file specified. ’ @ error/pdf.c/ReadPDFImage/794 in C:\laragon\www\website\adminbl\upload-newsletter.php:57 Stack trace: #0 C:\laragon\www\website\adminbl\upload-newsletter.php(57): Imagick->readimage(‘C:\laragon\www\…’) #1 {main} thrown in C:\laragon\www\website\adminbl\upload-newsletter.php on line 57
Have you tried using the DIRECTORY_SEPARATOR constant instead of /? I don’t think PHP itself cares, but ImageMagick might?
Also, I see you’re on windows. If you’re on windows 10 I can recommend using WSL2 to run Linux inside windows. Even though PHP works on windows it always feels a bit awkward.
Thanks squire. I haven’t tried using DIRECTORY_SEPARATOR so it’s worth a try.
I got as far as enabling WSL - now I think about it, I’m not sure if it was WSL2 - but I bottled out of installing a distro over our s-l-o-w internet connection.
Not being picky but you are using Imagick and not Imagemagick directly.
The first thing I would do is run the code with hard coded paths preferably with everything in the same folder. That will prove the code dose or dose not work.
I suspect this and would echo out $imgfile to see what it contains. It might be worth tying to put " " around the path ‘"’.$destdir.‘newsletter-’.$number.‘.jpg"’;
EDIT: Sorry I ment $destpath although the above could be the same problem
Okay, so I have simplified the code to just the Imagick bits and put a test PDF in the same directory. Works on my live site but on localhost gives
Fatal error : Uncaught ImagickException: UnableToOpenBlob ‘newsletter-1.pdf’: No such file or directory @ error/blob.c/OpenBlob/3315 in C:\laragon\www\website\adminbl\testnl.php:6 Stack trace: #0 C:\laragon\www\website\adminbl\testnl.php(6): Imagick->readimage(‘newsletter-1.pd…’) #1 {main} thrown in C:\laragon\www\website\adminbl\testnl.php on line 6
Imagick is the php API for Imagemagick, but I use Imagemagick directly as I find it easier to use and it has more options.
You can check out my website rubblewebs.co.uk I have a batch file on my desktop I can just drop files into to modify them and it runs from my localhost as well.
Working with pdf’s you would need to install Ghostscript first and Imagemagick would find on install. Ghostscript is used by Imagemagick when working with pdf files.
Just looking online you do need to install Imagemagick as well. I assumed if Imagemagick was not installed it would give a different error.
Linux usually installs a version of IMagemagick automatically although a very old version that has been patched.