ImageMagick (Imagick) error - Laragon localhost

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

I’m running PHP7.4

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.

1 Like

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. :shifty:

Just leave it running overnight. It’s worth it :smiley:

1 Like

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

1 Like

Thanks chaps.

DIRECTORY_SEPARATOR or wrapping the path in quotes doesn’t seem to work.

I’ll have a go with hard-coded paths next.

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

<?php
$destpath = 'newsletter-1.pdf';
$imgfile = 'newsletter-1.jpg';
$imagick = new Imagick();
$imagick->setResolution(150, 150);
$imagick->readImage($destpath.'[0]');
$imagick->thumbnailImage(150, 212, true, true);
$imagick->writeImages($imgfile, false);
echo '<p>Front page image created.</p>', "\n";

Putting double quotes round the file names gives an invalid argument error.

Update:

I found this issue on the Imagick GitHub and added realpath() to $destpath and $imgfile only to get a different error

Fatal error : Uncaught ImagickException: PDFDelegateFailed `The system cannot find the file specified. ’ @ error/pdf.c/ReadPDFImage/794 in C:\laragon\www\website\adminbl\testnl.php:6 Stack trace: #0 C:\laragon\www\website\adminbl\testnl.php(6): Imagick->readimage(‘C:\laragon\www\…’) #1 {main} thrown in C:\laragon\www\website\adminbl\testnl.php on line 6

I then tried the same version on my server and got a 500 error. So it looks like I can’t win.

Try this - it worked for me:

Edit:

In the original installation why should the rights=“none”

1 Like

Thanks. I did see mention of the policy.xml file in another post somewhere. Thing is I don’t have one. I don’t even have an ImageMagick directory.

If you do not have an Imagemagick directory then the following would fail:

$imagick = new Imagick();

I am using Ubuntu and after installing ImageMagik found the directory here:

/etc/Imagemagick-6/policy.xml

Script that works for me:

<?php declare(strict_types=1);
error_reporting(-1);
ini_set('display-errors', '1');


$destpath = 'news1.pdf';
$imgfile  = 'newsletter-1.jpg';


$imagick = new Imagick();
$imagick->setResolution(150, 150);
$imagick->readImage($destpath.'[1]');

$imagick->thumbnailImage(150, 212, true, true);
$imagick->writeImages($imgfile, false);

echo '<p>Front page image created.</p>', "\n";

$imagick->readImage($destpath);  // <==== line 57

$imagick->thumbnailImage(150, 212, true, true);
$imagick->writeImages($imgfile, false);

My prob is with my Windows install. It all works fine on my live Linux server.

I have a test script which was with the tutorial for installing ImageMagick/Imagick on Largon which works fine.

:upside_down_face:

Is there some Windows utility for finding files?

There must be a /etc/Imagemagick-6/policy.xml buried in your directory structure.

I’ve searched the entire c: drive for imagemagick and all it has come up with is two ImageMagickObject dll files.

I always get to a prompt and use dir policy.xml /s /p from the root directory. There’s probably a graphical way of doing it.

Out of interest is there any reason you can’t use Imagemagick directly?

I understood Imagick was in effect the interface for using ImageMagick in PHP.

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.

1 Like

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.

You are luck to get Imagick installed as I did it once but could never do it a second time although that was a few years ago.
Have alook at this page: https://mlocati.github.io/articles/php-windows-imagick.html

1 Like

The following script is working, so I assumed I had all I needed:

<?php
$width = 400;
$height = 210;

$img = new Imagick();
$img->newImage($width, $height, new ImagickPixel('transparent'));

$draw = new ImagickDraw();
$draw->setFillColor('#777bb4');
$draw->ellipse($width / 2, $height / 2, $width / 2, $height / 2, 0, 360);
$img->drawImage($draw);

$draw->setFillColor('black');
$draw->setStrokeColor('white');
$draw->setStrokeWidth(2);
$draw->setTextKerning(-8);
$draw->setFontSize(150);
$draw->setGravity(Imagick::GRAVITY_CENTER);

$img->annotateImage($draw, 0, -10, 0, 'php');
$img->setImageFormat('png');

header('Content-Type: image/png');
echo $img;

Yes it looks like Imagick is working OK.

I have just tried the code on my server and as you say it works OK on Linux.