12

When simply calling the Imagick class:

$image = new Imagick('/images/magick/atmsk.png');

I get the error message:

Fatal error: Uncaught exception 'ImagickException' with message 'unable to open file `/images/magick/atmsk.png' @ png.c/ReadPNGImage/2889' in .../imag.php:4 Stack trace: #0 .../imag.php(4): Imagick->__construct('/images/magick/...') #1 {main} thrown in .../imag.php

I have checked memory available as per another posting here and that is ok!

Dzseti
  • 447
  • 1
  • 7
  • 18
  • 1
    what happens if you use the full path to the image file, for instance something like `$_SERVER['DOCUMENT_ROOT'] . '/images/magick/atmsk.png'` – gmartellino Mar 16 '13 at 07:25
  • Then $image shows Imagick Object !! This was the problem ... thanks for the quick help – Dzseti Mar 16 '13 at 07:29

2 Answers2

29

Use the full path to the image, for example:

$image = new Imagick($_SERVER['DOCUMENT_ROOT'] . '/images/magick/atmsk.png');
gmartellino
  • 697
  • 5
  • 16
  • I have tried the following and still get the error: `$im = new Imagick(); $im->readImage(realpath($path));` – Anriëtte Myburgh Jun 02 '15 at 21:51
  • 4
    this might come in helpful for some: `$imagemagickisawful=substr($_SERVER['SCRIPT_FILENAME'], 0, strrpos($_SERVER['SCRIPT_FILENAME'], "/") )."/";` then use `$imagemagickisawful.$your_path_you_would_use_otherwise` – user151496 Jun 26 '15 at 09:59
  • Awesome, worked like a charm... Here is my code I have used: But don't forget to mention your Sitename withing the code as follow: Input: `$im = new Imagick($_SERVER['DOCUMENT_ROOT'] . '/site.com/images/image.jpg');` Output: `$im->writeImages($_SERVER['DOCUMENT_ROOT'] . '/site.com/images/image.jpg', true);` and it worked like a charm :) – Jodyshop Aug 16 '19 at 07:59
0

Thanks to @gmartellino answer and clarification. As I can't add a comment yet, its here incase someone has a similar situation.

In my case I had to access other sub folders in the parent.

The server path would look something like this

$path_file = "../other_folder/".$filename.".".$filetype;

The URL is

$path_file = $_SERVER['HTTP_ORIGIN']."/other_folder/".$filename.".".$filetype;

Hence, the used path is

$path_file = $_SERVER['DOCUMENT_ROOT']."/other_folder/".$filename.".".$filetype;

When using the Imagick class to read a file, use the full system path to the file rather than a URL

Hope it helped.

Osama Hussein
  • 69
  • 2
  • 8