[image processing] Imagick dont process relative path on Windows #268

Open
opened 2026-10-04 23:49:49 +02:00 by adam · 6 comments
Owner

Reported by hakastein on 7 Aug 2014 01:26

Version: 2.6.2

Imagick crash with "Uncaught exception 'ImagickException' with message 'UnableToOpenBlob `./_data/i/upload/2014/08/06/20140806153442-076e3cae-th.jpg': No such file or directory"

Steps to reproduce: Any action with imagick on windows.

Additional information: It can be fixed with realpath. There fix:

file image.class.php

86: $class = 'image_'.$this->library;
87: $source_filepath = realpath($source_filepath);
88: $this->image = new $class($source_filepath);

and

496: file_put_contents($destination_filepath, '');
497: $destination_filepath = realpath($destination_filepath);

Piwigo Bugtracker #3109

**Reported by hakastein on 7 Aug 2014 01:26** **Version:** 2.6.2 Imagick crash with &quot;Uncaught exception &#039;ImagickException&#039; with message &#039;UnableToOpenBlob `./_data/i/upload/2014/08/06/20140806153442-076e3cae-th.jpg&#039;: No such file or directory&quot; **Steps to reproduce:** Any action with imagick on windows. **Additional information:** It can be fixed with realpath. There fix: file image.class.php 86: $class = &#039;image_&#039;.$this-&gt;library; 87: $source_filepath = realpath($source_filepath); 88: $this-&gt;image = new $class($source_filepath); and 496: file_put_contents($destination_filepath, &#039;&#039;); 497: $destination_filepath = realpath($destination_filepath); [Piwigo Bugtracker #3109](http://piwigo.org/bugs/view.php?id=3109) <!-- github-sync-id:97226839 -->
Author
Owner

I'm actually encountering this issue on Linux, here are my specs:

Piwigo 2.9.1
Operating system: Linux
PHP: 7.1.7 (Show info) [2017-08-01 01:55:15]
    (imagick module version 3.4.3RC1)
MySQL: 10.1.21-MariaDB [2017-07-31 21:55:15]
Graphics Library: ImageMagick 6.9.9-0

So here's some more background. My Linux is NixOS (17.03.1623.138313fe01), which is admittedly a bit strange relative to other distributions, and often exposes upstream bugs/quirks having to do with paths. But otherwise I can't really explain why this appears to work in other Linuxes!

This behavior — that the ImageMagick PHP extension doesn't accept relative paths — is marked WONTFIX in their tracker: https://bugs.php.net/bug.php?id=61583

and moreover they note that realpath isn't always a reliable workaround. For source image files, ImageMagick accepts annotations for page numbers, frames, etc… like foo.pdf[3]. Here, realpath would fail because that file doesn't exist. That shouldn't be an issue in Piwigo; however, it does happen with destination image files: the file img123-89abcdef-th.jpg doesn't exist when being handed to writeImage().

So here's a fix that works for me, relative to the 2.9.1 release; but can we always assume that $_SERVER['DOCUMENT_ROOT'] is correctly defined? I'm happy to put this together as a PR on master if it wouldn't break other installations.

--- /nix/store/155a7n3x86bdrnzq0lkf39m9ccpp67j7-2.9.1.zip/admin/include/image.class.php 1969-12-31 19:00:01.000000000 -0500
+++ image.class.php     2017-07-31 21:51:05.946457697 -0400
@@ -84,6 +84,7 @@
     }

     $class = 'image_'.$this->library;
+    $source_filepath = realpath($source_filepath);
     $this->image = new $class($source_filepath);
   }

@@ -491,7 +492,7 @@
   {
     // use 4:2:2 chroma subsampling (reduce file size by 20-30% with "almost" no human perception)
     $this->image->setSamplingFactors( array(2,1) );
-    return $this->image->writeImage($destination_filepath);
+    return $this->image->writeImage($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.$destination_filepath);
   }
 }

Thanks.

I'm actually encountering this issue on Linux, here are my specs: ``` Piwigo 2.9.1 Operating system: Linux PHP: 7.1.7 (Show info) [2017-08-01 01:55:15] (imagick module version 3.4.3RC1) MySQL: 10.1.21-MariaDB [2017-07-31 21:55:15] Graphics Library: ImageMagick 6.9.9-0 ``` So here's some more background. My Linux is NixOS (17.03.1623.138313fe01), which is admittedly a bit strange relative to other distributions, and often exposes upstream bugs/quirks having to do with paths. But otherwise I can't really explain why this appears to work in other Linuxes! This behavior — that the ImageMagick PHP extension doesn't accept relative paths — is marked WONTFIX in their tracker: https://bugs.php.net/bug.php?id=61583 and moreover they note that `realpath` isn't always a reliable workaround. For source image files, ImageMagick accepts annotations for page numbers, frames, etc… like `foo.pdf[3]`. Here, `realpath` would fail because that file doesn't exist. That shouldn't be an issue in Piwigo; however, it does happen with destination image files: the file `img123-89abcdef-th.jpg` doesn't exist when being handed to `writeImage()`. So here's a fix that works for me, relative to the 2.9.1 release; but can we always assume that `$_SERVER['DOCUMENT_ROOT']` is correctly defined? I'm happy to put this together as a PR on master if it wouldn't break other installations. ``` --- /nix/store/155a7n3x86bdrnzq0lkf39m9ccpp67j7-2.9.1.zip/admin/include/image.class.php 1969-12-31 19:00:01.000000000 -0500 +++ image.class.php 2017-07-31 21:51:05.946457697 -0400 @@ -84,6 +84,7 @@ } $class = 'image_'.$this->library; + $source_filepath = realpath($source_filepath); $this->image = new $class($source_filepath); } @@ -491,7 +492,7 @@ { // use 4:2:2 chroma subsampling (reduce file size by 20-30% with "almost" no human perception) $this->image->setSamplingFactors( array(2,1) ); - return $this->image->writeImage($destination_filepath); + return $this->image->writeImage($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.$destination_filepath); } } ``` Thanks. <!-- github-comment-id:319247781 -->
Author
Owner

Oh, I think I understand now the usage of file_put_contents in the snippet above. I figured it could be a stale diff due to drift between 2.6.2 and 2.9.1, but actually it's so that the file exists in order to make realpath work… got it.

Oh, I think I understand now the usage of `file_put_contents` in the snippet above. I figured it could be a stale diff due to drift between 2.6.2 and 2.9.1, but actually it's so that the file exists in order to make `realpath` work… got it. <!-- github-comment-id:319248930 -->
Author
Owner

$_SERVER['DOCUMENT_ROOT'] doesn't work if piwigo is installed in a sub folder of the web server.

The correct call for me was:
return $this->image->writeImage(dirname(realpath($_SERVER['SCRIPT_FILENAME'])).DIRECTORY_SEPARATOR.$destination_filepath);

$_SERVER['DOCUMENT_ROOT'] doesn't work if piwigo is installed in a sub folder of the web server. The correct call for me was: `return $this->image->writeImage(dirname(realpath($_SERVER['SCRIPT_FILENAME'])).DIRECTORY_SEPARATOR.$destination_filepath);` <!-- github-comment-id:458603724 -->
Author
Owner

I also encountered the same issue on a Slackware distro (14.2).
Wouldn't
define('PHPWG_ROOT_PATH', __DIR__ . DIRECTORY_SEPARATOR . './');
on top of i.php be a valid solution? Or would it break other configurations (for example with ext_imagick )?
On my box this works without touching image.class.php.

I also encountered the same issue on a Slackware distro (14.2). Wouldn't `define('PHPWG_ROOT_PATH', __DIR__ . DIRECTORY_SEPARATOR . './');` on top of i.php be a valid solution? Or would it break other configurations (for example with ext_imagick )? On my box this works without touching image.class.php. <!-- github-comment-id:841887259 -->
Author
Owner

as @macgherl57, but on mageia 8

as @macgherl57, but on mageia 8 <!-- github-comment-id:1607811471 -->
Author
Owner

I also encountered the same issue on a Slackware distro (14.2). Wouldn't define('PHPWG_ROOT_PATH', __DIR__ . DIRECTORY_SEPARATOR . './'); on top of i.php be a valid solution? Or would it break other configurations (for example with ext_imagick )? On my box this works without touching image.class.php.

Encountered this same issue running Piwigo on Nginx Windows 10. My images generate fine with GD but once I switched over to ImageMagick, I had to deploy the fix given by @macgherl57 in my i.php to view my images again. Nothing else seems broken so far, Piwigo team should look to deploy this code in release.

> I also encountered the same issue on a Slackware distro (14.2). Wouldn't `define('PHPWG_ROOT_PATH', __DIR__ . DIRECTORY_SEPARATOR . './');` on top of i.php be a valid solution? Or would it break other configurations (for example with ext_imagick )? On my box this works without touching image.class.php. Encountered this same issue running Piwigo on Nginx Windows 10. My images generate fine with GD but once I switched over to ImageMagick, I had to deploy the fix given by @macgherl57 in my i.php to view my images again. Nothing else seems broken so far, Piwigo team should look to deploy this code in release. <!-- github-comment-id:2232357485 -->
Sign in to join this conversation.
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: adam/Piwigo#268