Imagick / imagick

🌈 The Imagick PHP extension 🌈
http://pecl.php.net/imagick
Other
548 stars 138 forks source link

Problems Opening PNG on Windows Apache Server with PHP, ImageMagick, & Imagick #607

Open AntumDeluge opened 1 year ago

AntumDeluge commented 1 year ago

OS: Windows 10 64-bit Apache: 2.4.54 PHP: 7.4.33 (cli) (built: Nov 2 2022 16:00:55) ( ZTS Visual C++ 2017 x64 ) (thread safe) ImageMagick: 7.1.0-18 Q16 x64 2021-12-14 (ImageMagick-7.1.0-18-vc15-x64) Imagick: (php_imagick-3.7.0-7.4-ts-vc15-x64)

>php -m
[PHP Modules]
bcmath
bz2
calendar
Core
ctype
date
dom
exif
fileinfo
filter
ftp
gd
gettext
hash
iconv
imagick
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
readline
Reflection
session
SimpleXML
SPL
standard
tokenizer
xml
xmlreader
xmlwriter
zip
zlib

[Zend Modules]

I'm having trouble opening images with Imagick on my Apache server on Windows. The error trace is as follows:

[Wed Mar 29 17:53:34.227745 2023] [mpm_winnt:notice] [pid 16076:tid 396] AH00455: Apache/2.4.54 (Win64) OpenSSL/1.1.1p PHP/7.4.33 configured -- resuming normal operations
[Wed Mar 29 17:53:34.227745 2023] [mpm_winnt:notice] [pid 16076:tid 396] AH00456: Apache Lounge VS16 Server built: Jun 22 2022 09:58:15
[Wed Mar 29 17:53:34.227745 2023] [core:notice] [pid 16076:tid 396] AH00094: Command line: 'C:\\Environment\\Apache2\\bin\\httpd.exe -d C:/Environment/Apache2'
[Wed Mar 29 17:53:34.228742 2023] [mpm_winnt:notice] [pid 16076:tid 396] AH00418: Parent: Created child process 12680
[Wed Mar 29 17:53:34.661077 2023] [mpm_winnt:notice] [pid 12680:tid 404] AH00354: Child: Starting 150 worker threads.
[Wed Mar 29 17:53:57.009857 2023] [php7:error] [pid 12680:tid 3084] [client 127.0.0.1:51638] PHP Fatal error:  Uncaught ImagickException: UnableToOpenBlob 'data/sprites/outfit/body/000-nonude.png': No such file or directory @ error/blob.c/OpenBlob/3527 in scripts\\imageprocessing.php:141\nStack trace:\n#0 scripts\\imageprocessing.php(141): Imagick->__construct('data/sprites/ou...')\n#1 scripts\\imageprocessing.php(277): OutfitDrawer->load_part('body', '000', 2, '-nonude')\n#2 scripts\\imageprocessing.php(309): OutfitDrawer->create_outfit(Array, 2)\n#3 createoutfit.php(51): OutfitDrawer->loadOrCreate('body-0-0_dress-...', 2)\n#4 {main}\n  thrown in scripts\\imageprocessing.php on line 141, referer: http://stendhal.localhost/character/deluge.html

The line of code in question is here. The image file does exist & is accessible. The data folder normally a symbolic link to another directory. But to make sure it wasn't an issue with Apache following symbolic links, I copied the folder directly into the virtual host root. ImageMagick & Imagick are both installed from the pre-built packages provided by PECL.

It has not problem on my server running on Ubuntu with ImageMagick 6.9.11 & Imagick 3.6.0. So I tried downgrading to older packages with similar results. I don't remember the error messages from the older versions, but I can look into it again if need be.

I prefer to be able to use versions for PHP 7 as that is the version our remote server I am developing for uses.

Please let me know if there is any other information needed.

Update: I just tried using PHP 8.1 with php_imagick-3.7.0-8.1-ts-vs16-x64 package with the same result.

Danack commented 1 year ago

Can you clarify, is this a one-off error, or is it all images?

I prefer to be able to use versions for PHP 7

It won't be anything to do with PHP itself.....it will probably be one of:

Can you try in order:

  1. converting that file using the command line tools with something like:
convert test.png -resize 50% output.png

and see if that works at all for it.

  1. Opening it with a really simple PHP script like:
<?php

$image = new Imagick(__DIR__ . "/test.png");
$image->cropImage(48, 64, 20,  64);
$image->writeImage(__DIR__ . "/output.png")

And seeing if that works.

  1. Checking the machine isn't really low on diskspace.
AntumDeluge commented 1 year ago

Can you clarify, is this a one-off error, or is it all images?

All images.

converting that file using the command line tools

>convert 000-nonude.png -resize 50% test.png

Scaled the image down to half size.

Opening it with a really simple PHP script...

This works from both a PHP script & the interactive shell:

php > echo file_exists("000-nonude.png");
1
php > $img = new Imagick("000-nonude.png");
php > $img->cropImage(48, 64, 0, 0);
php > $img->writeImage("test.png");
$img = new Imagick("000-nonude.png");
$img->cropImage(48, 64, 48, 0);
$img->writeImage("test.png");

Checking the machine isn't really low on diskspace.

>dir C:\
 Volume in drive C is Windows
 Volume Serial Number is EF62-FCC0

 Directory of C:\
...
               0 File(s)              0 bytes
              17 Dir(s)  483,371,753,472 bytes free

From all this I gather my PHP configuration is fine. Perhaps the issue is my apache configurations.

Edit: I should add that opening images with the gd/gd2 extension works:

imagepng("path/to/image.png");
AntumDeluge commented 1 year ago

Using an absolute path to the image fixes the error. So that is a hint at what is wrong. Likely it is an issue with Apache. But it seems odd that relative paths work with other PHP functions.

AntumDeluge commented 1 year ago

I believe the problem is with the path that is being sent to ImageMagick. If I start my Apache server daemon from the virtual host directory, it can find the images. It appears that the current working directory is being passed to ImageMagick instead of the directory of my virtual host.

But I'm still not sure if Apache, ImageMagick, or Imagick is the source of the problem. As I said in my original post, it works fine on my Ubuntu/Linux machine. I don't know what the difference is on my Windows server.