ImageMagick is installed correctly on XAMPP yet cannot read in a file for editing/conversion

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
TomXampp
Posts: 14
Joined: 2015-12-02T00:21:23-07:00
Authentication code: 1151

ImageMagick is installed correctly on XAMPP yet cannot read in a file for editing/conversion

Post by TomXampp » 2015-12-02T00:39:01-07:00

My webserver has ImageMagick installed and available (and works correctly), and so I want to make use of it. To do that, I need to run it on my localhost, which is XAMPP, to test code, etc. I've succeeded in installing it on XAMPP, however it fails to be able to read a specified file for operation. Below I've listed precisely how I've set up ImageMagick on XAMPP and an description of the problem, which I hope can be reproduced by a reader and the solution found. It may simply be a set-up issue, or it may be a true bug within the version of ImageMagick I'm using.

I'm using the current version of XAMPP (Version 5.6.14) with Apache/2.4.17 (Win32) and PHP/5.6.16 on Windows 8.1

To install ImageMagick locally, I created this directory off the root:

c:\ImageMagick

Then, from here:

http://windows.php.net/downloads/pecl/deps/

...I downloaded the X86 thread-safe version that would match the DLL (which you have to obtain separately, which mystifies me). The version I downloaded was:

ImageMagick-6.9.1-2-vc11-x86.zip

I unzipped it to C:\ImageMagick and then moved the contents of its "bin" subfolder to C:\ImageMagick, so that everything is in the root of that directory.

I then added C:\ImageMagick to my PATH and verified that it worked by first exectuting

Code: Select all

convert
from a command line at a different directory, which then caused the help screen info to display. I then further verified it worked by performing an image resizing from the command line successfully. So far so good.

Then, following the advice culled from other online postings about how to do this, I copied the CORE_RL_*.DLL files and the IM_MOD_RL*.DLL files from C:\ImageMagick to c:\xampp\apache\bin

I then added this system environment variable:

Code: Select all

MAGICK_HOME=c:\imagemagick
The next step is to add php_imagemagick.dll to the php\ext folder in XAMPP, but there's no such DLL among those in the ImageMagick ZIP files/EXEs. To get it, I had to go here:

http://windows.php.net/downloads/pecl/r ... /3.3.0rc2/

...and download:

php_imagick-3.3.0rc2-5.6-ts-vc11-x86.zip

...I then unzipped it to a dedicated "junk" directory and copied the php_imagemagick.dll file it contained to c:\xampp\php\ext

I then edited the php.ini file in c:\xampp\php to include this directive:

Code: Select all

extension=php_imagemagick.dll
Now...the moment of truth:

I started XAMPP and checked the PHP_ERROR_LOG, and found no errors. I then ran PhpInfo.php and confirmed that ImageMagick was loaded; here is the screen shot:

Image

So, that would seem to indicate that XAMPP recognizes ImageMagick, and that is has been installed correctly and with no errors.

Now for the meat of the issue: when the following script is run from my live server (DreamHost), it successfully converts the X.JPG image as indicated (notice the redundant check for the imagick extension at the beginning):

Code: Select all

<?php
if (!extension_loaded('imagick')) {
    echo "imagick not installed...";
} else {
	echo "imagick installed...";
}
echo "Start...";
$uploaded_filename = "x.jpg";
$im = new Imagick();
echo "Created object...";
$im->readImage("x.jpg");
echo "Read JPG...";
$im->resizeImage(528, 0, Imagick::FILTER_LANCZOS2SHARP, 1);
$im->stripImage();
$im->writeImage("x_lg.jpg");
$im->clear();
$im->destroy(); 
echo "Done."
?>
However, when I run this code from my XAMPP localhost, it gets as far as creating the $im (the ImageMagick object), but it does not proceed to read in the image with the readImage() function ("Created object..." is written to the screen, but nothing beyond that, and no output file is created).

Any suggestion as to what I may have omitted in my installation?

TomXampp
Posts: 14
Joined: 2015-12-02T00:21:23-07:00
Authentication code: 1151

Re: ImageMagick is installed correctly on XAMPP yet cannot read in a file for editing/conversion

Post by TomXampp » 2015-12-03T01:25:09-07:00

It turns out that ImageMagick needs the full path to your image files, even if they are in the same directory as the script that is executing ImageMagick on them. It's that simple. Here's how to do it:

Say your images are kept in an "images" folder off the root of your site. On XAMPP localhost, the full path might be:
C:/xampp/htdocs/mysite/images/x.jpg
Thus:

Code: Select all

$_SERVER['DOCUMENT_ROOT'] would hold "C:/xampp/htdocs/mysite";
    $path_to_your_files = "images/";
    $image_file_name = "x.jpg";
Put it all together with this string:

Code: Select all

$my_image = $_SERVER['DOCUMENT_ROOT'] . "/" . $path_to_your_files . $image_file_name;
On the live server, the full path might be:
/the_inaccessible_superfolders_of_your_website/mysite.com/images/x.jpg
Thus:

Code: Select all

$_SERVER['DOCUMENT_ROOT'] would hold "/the_inaccessible_superfolders_of_your_website/mysite.com"
    $path_to_your_files = "images/";
    $image_file_name = "x.jpg";
Again, put it all together with this string:

Code: Select all

$my_image = $_SERVER['DOCUMENT_ROOT'] . "/" . $path_to_your_files . $image_file_name;
That is what must be fed into

Code: Select all

new Imagick($my_image);
in order for it to work. I hope this helps someone else struggling with this issue.

Bonzo
Posts: 2970
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: ImageMagick is installed correctly on XAMPP yet cannot read in a file for editing/conversion

Post by Bonzo » 2015-12-03T03:40:12-07:00

Imagick is not the same as Imagemagick.

When using Imagick or the Imagemagick in my xammp install I never gave the full path. I guess you problem is you are reading your images from outside localhost. I have no idea why you would be doing this if you intend on using your code on a server later.

Post Reply