Skip to page content or skip to Accesskey List.
Search evolt.org
evolt.org login: or register

Work

Main Page Content

PHP frontend to ImageMagick

Rated 4.05 (Ratings: 4) (Add your rating)

Log in to add a comment
(30 comments so far)

Want more?

 
Picture of gvtulder

Gijs van Tulder

Member info | Full bio

User since: February 05, 2002

Last login: September 17, 2007

Articles written: 6

What is ImageMagick?

ImageMagick is a powerful set of image manipulation utilities. It can read, write and manipulate images in many image formats. It can resize, rotate, sharpen, color reduce or add any other special effect to your images. And, best of all, ImageMagick is directly available from the command line. In this article, we will write a script to make it available from the query string. You can then use it, for example, to automatically generate thumbnails of your images.

What our script will do

We will write a script that we can copy-paste in a directory with images and that enables us to use ImageMagick's convert utility on each of the images in that directory. The script will enable us to give convert commands by changing the query string.

Maybe a simple example will better explain this idea. You've got an image: http://wwww.example.com/img/image.jpg. You copy the ImageMagick script magick.php to the same directory. The image is now also available as http://www.example.com/img/magick.php/image.jpg. So far, your image hasn't changed. Now, imagine you want a thumbnail of the image with a width of exactly 200 pixels. You can get that image by requesting the url: http://www.example.com/img/magick.php/image.jpg?resize(200).

On receiving a request, the script will:

  1. Parse the query string;
  2. Convert the query string to an ImageMagick command string;
  3. Run ImageMagick on the image;
  4. Send the modified image to the browser.

As you see, the script will run ImageMagick for every request. This isn't very efficient. As you will probably use just a few commands (e.g. thumbnail and original image) in your html files, caching the output will speed up the system. We will add a point 5 to the list. The output of ImageMagick should be cached. The script should send the cached image if it exists, so ImageMagick won't be generating the same image over and over again.

Commands

You can use the standard commands/options of ImageMagick's convert utility. The command is followed by the command's parameters. These parameters are enclosed in brackets. Multiple commands are separated by a plus sign.

ImageMagick uses < and > in some parameters. You can't use these in html-documents. Instead of < and >, you may use { and } in your query string. The scripts then converts { to < and } to >.

Here are a few example convert commands and their query equivalent.

Command line Query string
-resize &quot;100x150&quot; ?resize(100x150)
-resize &quot;800x600&gt;&quot; -roll &quot;90&quot; ?resize(800x600})+roll(90)
-flip -resize &quot;800x600&gt;&quot; -flop ?flip+resize(800x600})+flop

Extra commands

The long list of ImageMagick commands didn't contain some things I wanted to do. I added three 'extra' commands to the script to do this.

part

The first of these commands is part(<i>width</i>x<i>height</i>). With ImageMagick's crop command, it is possible to get a part of the image. Unfortunately, this command only accepts absolute parameters. It can crop w by h pixels, starting x pixels from the left and y pixels from the top of the image. But what if I want to get 100x100 pixels from the center of the image? That's impossible if I don't know the size of the image.

Enter the part command. It resizes the image to match either the preferred width or the preferred height. Then it crops the image to get the center part of that resized image. And that's what I wanted to do.

colorizehex

ImageMagick's colorize command accepts only decimal RGB numbers, on a 0 to 100 scale. To colorize with red gives

colorize
100/0/0
. This isn't ideal for web use, since html uses hex codes to identify colors. The colorizehex(<i>hex</i>) command does accept hex colors. It converts them to the ImageMagick notation. Example: a red colorize is done with colorizehex(FF0000).

type

The type(<i>type</i>) is available in ImageMagick. It's just not a part of the commands, but is appended to the name of the output file (e.g. jpg:output.jpg). I wanted to include it in the query string, so I made it a command. You can now convert the image to jpeg by using type(jpg) in your query.

Before we start

There are just two minor points left before we can start coding.

Do you have ImageMagick?

ImageMagick should be installed on your system before you can use it in your scripts. This means you will either have to install it yourself, or have your server admin do it for you.

If your server is running PHP in safe mode, which it is likely to be if you're using a (free) shared host, your scripts don't have the right to execute shell commands. As this script runs ImageMagick as a shell command, you won't be able to use it. You could a. ask your hosting provider to disable safe mode or b. use the GD library to generate your images. ImageMagick is far more powerful than the GD library, but you can use the latter even in safe mode.

Why write your own script?

Directly running convert isn't the only way to use ImageMagick in your scripts. The Imagick module from the PEAR library, PerlMagick, a Perl interface to ImageMagick, can do this too. Then why bother and write your own script? Because it gives you a far more flexible system. You just enter your commands as the query string, and the script just sends them to ImageMagick. The PEAR module, for instance, has a special PHP function for each ImageMagick command. The script would have to translate the commands to the corresponding functions, for which it would need an array with all possible commands and functions. The direct method, withouth PEAR module, is therefore faster to write.

The script

And, finally, here's the script that makes it all possible. If you copy all parts, you'll end up with one script. Place it in your image directory, and it's ready for use.

Configuration

You can specify where your images are and where you want the script to cache the processed images. It defaults to the current directory, which is probably where you want it. If the convert utility isn't available in the PATH environment variable of your web server, you'll need to specify the full path.

<?php
//  location of source images (no trailing /)
$image_path = '.';

//  location of cached images (no trailing /)
$cache_path = '.';

//  location of imagemagick's convert utility
$convert_path = 'convert';

Check input

The path and file name of the requested image is available as $_SERVER['PATH_INFO']. We need to check that such information is given, and that the file exists.

// first, check if an image location is given
if (!isset($_SERVER['PATH_INFO'])) {
    die('ERROR: No image specified.');
}
$image = $image_path.$_SERVER['PATH_INFO'];

// next, check if the file exists
if (!file_exists($image)) {
    die('ERROR: That image does not exist.');
}

Parse commands

We need a regular expression to parse the query string and extract commands and parameters.

// extract the commands from the query string
// eg.: ?resize(....)+flip+blur(...)
preg_match_all('/\+*(([a-z]+)(\(([^\)]*)\))?)\+*/',
               $_SERVER['QUERY_STRING'],
               $matches, PREG_SET_ORDER);

We now have an array $matches. Each element in that array is another array, with in the third element (position 2) the command name and on position 4 the parameters.

The cache file name will contain the name of the original file. We then add the commands and parameters to it, so we get an unique name for each version of the image.

// concatenate commands for use in cache file name
$cache = $_SERVER['PATH_INFO'];
foreach ($matches as $match) {
    $cache .= '%'.$match[2].':'.$match[4];
}
$cache = str_replace('/','_',$cache);
$cache = $cache_path.'/'.$cache;
$cache = escapeshellcmd($cache);

Run convert

Now that we have the cache file name, we can look if we already have a cached version of the requested image. If we do, we can just send that to the browser. If we don't, we will ask convert to create it.

We will add each command to the string $commands. We will send that string to convert to generate the image.

if (!file_exists($cache)) {
    // there is no cached image yet, so we'll need to create it first

    // convert query string to an imagemagick command string
    $commands = '';
    foreach ($matches as $match) {
        // $match[2] is the command name
        // $match[4] the parameter
    
        // check input
        if (!preg_match('/^[a-z]+$/',$match[2])) {
            die('ERROR: Invalid command.');
        }
        if (!preg_match('/^[a-z0-9\/{}+-!@%]+$/',$match[4])) {
            die('ERROR: Invalid parameter.');
        }
    
        // replace } with >, { with <
        // > and < could give problems when using html
        $match[4] = str_replace('}','>',$match[4]);
        $match[4] = str_replace('{','<',$match[4]);

After we've checked the input and converted { to < and } to >, we will add this command to the $convert string. But, since we used our own, special commands, we will have to check if this command is one of them. If it is, we will have to do a bit more work.

The colorizehex is quite simple. We will convert hex to decimal, and then convert the 0-255 scale to ImageMagick's 0-100.

        // check for special, scripted commands
        switch ($match[2]) {
            case 'colorizehex':
                // imagemagick's colorize, but with hex-rgb colors
                // convert to decimal rgb
                $r = round((255 - hexdec(substr($match[4], 0, 2))) / 2.55);
                $g = round((255 - hexdec(substr($match[4], 2, 2))) / 2.55);
                $b = round((255 - hexdec(substr($match[4], 4, 2))) / 2.55);
    
                // add command to list
                $commands .= ' -colorize "'."$r/$g/$b".'"';
                break;

The part command requires more work. We first get the size of the source image using the getimagesize() function. After that we let ImageMagick resize the image to match either the new width or the new height. We want one of the image's dimensions to be equal to the new size, and the other one to exceed that size. We can then crop the image to the requested size.

            case 'part':
                // crops the image to the requested size
                if (!preg_match('/^[0-9]+x[0-9]+$/',$match[4])) {
                    die('ERROR: Invalid parameter.');
                }

                list($width, $height) = explode('x', $match[4]);
    
                // get size of the original
                $imginfo = getimagesize($image);
                $orig_w = $imginfo[0];
                $orig_h = $imginfo[1];
    
                // resize image to match either the new width
                // or the new height
    
                // if original width / original height is greater
                // than new width / new height
                if ($orig_w/$orig_h > $width/$height) {
                    // then resize to the new height...
                    $commands .= ' -resize "x'.$height.'"';
    
                    // ... and get the middle part of the new image
                    // what is the resized width?
                    $resized_w = ($height/$orig_h) * $orig_w;
    
                    // crop
                    $commands .= ' -crop "'.$width.'x'.$height.
                                 '+'.round(($resized_w - $width)/2).'+0"';
                } else {
                    // or else resize to the new width
                    $commands .= ' -resize "'.$width.'"';
    
                    // ... and get the middle part of the new image
                    // what is the resized height?
                    $resized_h = ($width/$orig_w) * $orig_h;
    
                    // crop
                    $commands .= ' -crop "'.$width.'x'.$height.
                                 '+0+'.round(($resized_h - $height)/2).'"';
                }
                break;

The type command is really simple. We can just save the type name for now.

            case 'type':
                // convert the image to this file type
                if (!preg_match('/^[a-z]+$/',$match[4])) {
                    die('ERROR: Invalid parameter.');
                }
                $new_type = $match[4];
                break;

If this command isn't special, we can simply add the command and parameters to the command string.

            default:
                // nothing special, just add the command
                if ($match[4]=='') {
                    // no parameter given, eg: flip
                    $commands .= ' -'.$match[2].'';
                } else {
                    $commands .= ' -'.$match[2].' "'.$match[4].'"';
                }
        }
    }

After we've run through the array we've got a list of commands in $commands. We can now run convert. convert needs the commands, the location of the source image and the location of the output image to work. If a new file type is specified, we add that type and a colon to the output file name.

    // create the convert-command
    $convert = $convert_path.' '.$commands.' "'.$image.'" ';
    if (isset($new_type)) {
        // send file type-command to imagemagick
        $convert .= $new_type.':';
    }
    $convert .= '"'.$cache.'"';

    // execute imagemagick's convert, save output as $cache
    exec($convert);
}

Output

The $cache variable should now point to the file containing the requested image. It was already cached or it was generated by convert. If the file exists, we can retrieve some information about that image to put in the http headers.

// there should be a file named $cache now
if (!file_exists($cache)) {
	die('ERROR: Image conversion failed.');
}

// get image data for use in http-headers
$imginfo = getimagesize($cache);
$content_length = filesize($cache);
$last_modified = gmdate('D, d M Y H:i:s',filemtime($cache)).' GMT';

// array of getimagesize() mime types
$getimagesize_mime = array(1=>'image/gif',2=>'image/jpeg',3=>'image/png',
                           4=>'application/x-shockwave-flash',5=>'image/psd',
                           6=>'image/bmp',7=>'image/tiff',8=>'image/tiff',
                           9=>'image/jpeg',
                           13=>'application/x-shockwave-flash',
                           14=>'image/iff');

We can now check if the browser sent us a If-Modified-Since header. This is used to update the browser cache. If the If-Modified-Since date of the browser is equal to the date the image was last modified, we don't have to send the image again. The cache of the browser still has an updated version.

// did the browser send an if-modified-since request?
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
	// parse header
	$if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']);

	if ($if_modified_since == $last_modified) {
		// the browser's cache is still up to date
		header("HTTP/1.0 304 Not Modified");
		header("Cache-Control: max-age=86400, must-revalidate");
		exit;
	}
}

The browser does really want a (new) version of the image. We send some headers and then the image.

The Content-Type header is a bit special. We have to send a MIME content type, but the PHP getimagesize() command only gives us the number of the image type. With the $getimagesize_mime array we can find the MIME type of that number. In case there is no number we use the application/octet-stream type. I haven't tested that, but it's probably better than text/html. (Note: Starting with PHP 4.3, getimagesize() does return a MIME type. I didn't use it to make the script compatible with older versions.)

// send other headers
header('Cache-Control: max-age=86400, must-revalidate');
header('Content-Length: '.$content_length);
header('Last-Modified: '.$last_modified);
if (isset($getimagesize_mime[$imginfo[2]])) {
	header('Content-Type: '.$getimagesize_mime[$imginfo[2]]);
} else {
	// send generic header
	header('Content-Type: application/octet-stream');
}

// and finally, send the image
readfile($cache);

?>

Concluding

If you copied the parts of the script and saved it in your image directory, it's ready for use. Just enter the url to the script, a slash, then the name of your image and a query string. You should now get the image, modified to suit your needs.

For those of you who don't like to copy-paste: you can download the full script.

Tip

Maybe you don't like the ugly .php part in your url (I don't). You can edit your Apache's configuration file, or place a .htaccess file in your images directory and add the line:

DefaultType application/x-httpd-php

You can then rename the script to something without .php (ie. just magick). The url is now much nicer.

Gijs van Tulder is a Dutch student. He likes to play with all things web related and fancies himself as a part-time amateur web developer.

ugly .php part in your url

Submitted by migurski on February 27, 2003 - 18:50.

Great article, one tip: if you want to remove the ugly .php part of your URL, but you don't want to redefine the default type for every document affected by that .htaccess file, you can do so by removing the AddType directive and replacing it with Option Multiviews. This is a generally preferable method, since it applies to every kind of file, and doesn't require you to sacrifice informative file extensions on your server.

login or register to post comments

Great script - but having problems

Submitted by ddeo on March 3, 2003 - 11:18.

This is something i need for a site i built. I am currently using imageMagick to create thumbs on the fly but they are not being cached. This is slowing the sight down on every refresh! I was excited to see your code because it does the caching thing but I can't get it to work. Note: I am not a PHP pro by any means but I do have a script that is currently working and using imageMagick so I know it works. Using your script verbatim causes the Error: No image specified. I commented out the $_SERVER['PATH_INFO'] and get a new error: Warning: getimagesize: Read error! THe one difference I noticed between your script and the other script that is working on my server currently is it uses system() instead of exec(). Any help is appreciated. D

login or register to post comments

Re: ddeo

Submitted by gvtulder on March 3, 2003 - 14:41.

system() and exec() are the same, except for one thing: system() outputs the output of the executed command, while exec() doesn't. This shouldn't be the problem. I can think of a few possible causes:

  • Where exactly did you comment out the $_SERVER['PATH_INFO']? It is used to retrieve the original file and in the cache name. If you don't specify a cache file, getimagesize() obviously can't find it.
  • Did you make the cache directory writable to the web server? If it isn't, ImageMagick can't write an image to that directory.
  • As a last resort, you might want to check your Apache error log. Error messages from ImageMagick are saved in that log. That could give more information about the cause of this problem.

login or register to post comments

Re: ddeo

Submitted by ddeo on March 3, 2003 - 15:53.

First Question: I commented out the first interation of $_SERVER['PATH_INFO'] to see if I could bypass this if it was the reason it was failing.


I also created a test script with just one command - echo $_SERVER['PATH_INFO']; - this script outputted nothing. ANyway I uncommented this again and the error: No image specified - is persistent


Second Question: The cache dirsctory defaults to the same directory as the image directory hence: $image_path = '.'; and $cache_path = '.';


This directory is set to 755 and is writable by my other script. No scripts will execute in a directory set to 777 because of my hosting company's policy.


I also put the full path to the convert command in the imageMagick directory


Last Question: I did not see anything in the error logs


Here is what my working script looks like (by the way I am not the author of this script / and it does not cache as you can see - I don't remenber who wrote this script):::::::::::::::::::::::::::::::::::::::::::::::::::


#!/usr/local/bin/php  
  

login or register to post comments

Re: ddeo

Submitted by gvtulder on March 4, 2003 - 06:35.

There isn't any information in $_SERVER['PATH_INFO']: Which version of PHP do you use? If you have a version prior to 4.1.0, the $_SERVER variable doesn't exist. You'll then have to use the longer $HTTP_SERVER_VARS['PATH_INFO'] to get the information. If you don't have register_globals disabled - if you don't know what that is and you don't have PHP 4.2.0 or later, register_globals is enabled - you can also use $PATH_INFO to get the information.

If that doesn't help, you could comment all error messages and echo the ImageMagick command (place echo $convert; above exec($convert);). You can try to execute this command by hand on your server, and see what error messages ImageMagick returns.

login or register to post comments

Re: ddeo

Submitted by ddeo on March 4, 2003 - 08:18.

Gijs, $HTTP_SERVER_VARS worked! Thanks! D

login or register to post comments

Can't resize

Submitted by ddeo on March 4, 2003 - 08:39.

OK, I can get the script to show the image but only full size. The querry strings aren't working :(

login or register to post comments

Re: ddeo

Submitted by gvtulder on March 4, 2003 - 08:59.

Did you change all occurences of $_SERVER, or just the $_SERVER['PATH_INFO'] bits? You should also change the $_SERVER['QUERY STRING'] variable (e.g. $HTTP_SERVER_VARS['QUERY_STRING'] instead of $_SERVER['QUERY_STRING']) and the $_SERVER['HTTP_LAST_MODIFIED'].

login or register to post comments

Re: ddeo

Submitted by ddeo on March 4, 2003 - 09:43.

OK, now I am having some weird stuff going on...
I changed all _SERVER to HTTP_SERVER_VARS

Now, if I try "eda.jpg?resize(10)" it says "ERROR: Image conversion failed."

If I try "eda.jpg?flip" it says "ERROR: Invalid parameter."

But, unlike before if I use no query string like "magick.php/eda.jpg" I get a 500 Internal Server Error. Before it would just display the image full size.

Also, I am sure the script is writing the cache files because I see new files being created on the server like "_eda.jpg". I also found one that is named "eda.jpg%roll:90".

Also, my host is using PHP v. 4.0.6 and register_globals is ON.
Thanks again for all of your help. D

login or register to post comments

Re: ddeo

Submitted by gvtulder on March 7, 2003 - 05:04.

The error message "ERROR: Image conversion failed." is displayed if the file named $cache doesn't exist after running convert. You could include the $cache variable in the error message, and see why that image doesn't exist. Something like this:

// there should be a file named $cache now
if (!file_exists($cache)) {
	die('ERROR: Image conversion failed. (<b>'.$cache.'</b> not found)');
}

The "ERROR: Invalid parameter." when trying eda.jpg?flip is probably an error in my regular expression. It checks for a parameter of one or more characters. A command with no parameter, like flip, isn't matched by that regular expression. That's wrong. You should replace that regular expression with this:

        if (!preg_match('/^[a-z0-9\/{}+-!@%]*$/',$match[4])) {
            die('ERROR: Invalid parameter.');
        }

I don't know of PHP scripts giving 500 Internal Server Error messages. That error might be caused by your web server (maybe a .htaccess error?).

login or register to post comments

Re: ddeo

Submitted by ddeo on March 12, 2003 - 12:28.

I am sorry I couldn't get your script to work. The good news for me is that I was able to get the PHP Snippet I posted on 03/03/03 to check for cached files using the file_exists() function of PHP.

Thanks for your diligence. D

login or register to post comments

Fix for "ERROR: Image conversion failed."

Submitted by bobdobbs on June 12, 2003 - 06:38.

I was getting the same error as ddeo. I'm using an NT server and like all windows versions, you cannot have a filename with a / \ : ? " < > or | in it. So I added the following line to the code where it sets the unique name of the output file. This pretty much solved the problem for me. I've not tried all the various IM functions yet so there may be other chars that need to str_replace(d) but that's easy enough to do. Thanks alot for the otherwise really cool script........ // concatenate commands for use in cache file name $cache = $_SERVER['PATH_INFO']; foreach ($matches as $match) { $cache .= '%'.$match[2].':'.$match[4]; } $cache = str_replace('/','_',$cache); // *********** Added this one here *************>>>> $cache = str_replace(':','-',$cache); //

login or register to post comments

Problem with "%" character

Submitted by zeroweb on June 26, 2003 - 22:35.

Just a small problem i ran into:

When creating the $cache variable, the "%" character is used to join the image name to the commands.

My version of imagmagick converted this to a decimal- 2.50958 to be exact.
The original code:
// concatenate commands for use in cache file name
$cache = $_SERVER['PATH_INFO'];
foreach ($matches as $match) {
	$cache .= '%'.$match[2].':'.$match[4];
}

Modified Code:
// concatenate commands for use in cache file name
$cache = $_SERVER['PATH_INFO'];
foreach ($matches as $match) {
	$cache .= '-'.$match[2].':'.$match[4];
}
Other than that, I am grateful for the caching function. I am glad to finally see someone with intelligence attack the Imagemagick/PHP problem. thanks, -mark

login or register to post comments

Embedded Images

Submitted by zeroweb on June 27, 2003 - 00:07.

Another Issue I'm running into If I embed an image in a html or php page, imagemagick seems to hang or stall. Even something as simple as this:
<img alt="image" src="/image.php/image.jpg?size(350)"

login or register to post comments

Images not found a PHP or apache issue?

Submitted by bw on September 4, 2003 - 13:38.

Sinds I changed to apache 2 my images are not shown anymore.

/magick.php/homepage.png?...
The requested URL /magick.php/homepage.png?... was not found on this server.

Anybody any idea?
Does apache understand magick.php is a php file?

thanks

login or register to post comments

Re: Images not found a PHP or apache issue?

Submitted by gvtulder on September 4, 2003 - 23:19.

Hi bw,

There's a special Apache configuration directive that 'controls whether requests that contain trailing pathname information that follows an actual filename' will be accepted or rejected. It's the AcceptPathInfo directive.

If you set this to 'Off', any URL's like "/magick.php/homepage.png?..." will result in a 404 Not Found error. If you set it to 'On', the magick.php script should probably work.

Hope that helps, Gijs

login or register to post comments

Re: Images not found a PHP or apache issue?

Submitted by bw on September 6, 2003 - 00:31.

Perfect! That was the problem. I placed the line AcceptPathInfo on in my Virtualhost section (Vhost.conf) but it could also be httpd.conf if you like. Dank je Gijs ;-)

login or register to post comments

convert long strings

Submitted by bw on September 6, 2003 - 06:18.

Gijs,

Any tips to convert a command like this into the string?

Command:
-draw "gravity south fill black text 0,36 'Copyright 2003'"

String (?):
?draw(gravity south fill black text 0,36 'Copyright 2003')

I'm having difficulties getting text into my images

Thanks,

Bastiaan

login or register to post comments

script still not able to create cache file

Submitted by msephton on January 7, 2004 - 05:06.

Great idea for a script! For me the script runsbut fails with an error even with no parameters. What permissions do I need to verify so that it can create the cache file? http://www.ewtoo.org/~matt/iphoto/magick.php/optios.jpg Any help appreciated, matt

login or register to post comments

One down one to go!

Submitted by msephton on January 8, 2004 - 04:29.

Just to say I managed to fix my problem by changing permissions! thanks.

Works great but I am having some problems with the part command. I am trying to expand the canvas of an image but part is getting confused as I am also resizing it at the same time.

login or register to post comments

Nice Addition to this script

Submitted by shiva777 on September 27, 2004 - 13:27.

Hi. I noticed that the original unsized image is not deleted and can't be deleted so I added these two lines right at the very end:

unlink($image); copy($cache, $image);

$image being the original image $cache being the resized image.

So at the end it deletes the original large image and replaces it with a copy of the smaller resized image saving disk space. -shiva

login or register to post comments

One more little thing

Submitted by shiva777 on October 8, 2004 - 15:42.

I just realized that I could also add the line: unlink($cache); to get rid of the resized image. Can anyone tell me how to get it to ignore images that are already under my required width? -shiva

login or register to post comments

No longer works for Mozilla ?

Submitted by brianadkins on January 24, 2005 - 16:13.

This approach does not seem to work with the new Mozilla / Firefox browsers since they automatically escape special characters in the URL... Does anyone have an elegant way to fix this?

login or register to post comments

ImageMagick Windows Installation

Submitted by Gerald on October 5, 2005 - 11:28.

Hi There, have anyone used ImageMagick on a windows 2000 system with the "PHP IMAGEMAGICK FRONT END" I am struggeling to get imagemagick working

If I go to the command line and do the image test, I can see that it is working...

But as soon as I run the URL magick.php/image.jpg?resize(200) I get a internet explorer error HTTP 404 - File not found, I suspect that the problem is the trailing " *.php/image.jpg " it seems to me that windows does not like the page extension and the " / " and then the image.jpg

Does anyone know how it can be fixed, your help will be greatly appreciated

login or register to post comments

RE: ImageMagick Windows Installation

Submitted by Little Frog on November 27, 2005 - 00:24.

I had the same problem as you Gerald. But some research and exprimenting I finerly made it ImageMagick work. First off, get imagemagick installed correctly. Heres a link of how you do: http://circle.ch/blog/p533.html now to make this script work you just need to do the little modification bobdobbs posted. under "PARSE COMMANDS" there are these lines:
<?php
// concatenate commands for use in cache file name
$cache = $_SERVER['PATH_INFO'];
foreach (
$matches as $match) {
    
$cache .= '%'.$match[2].':'.$match[4];
}
$cache = str_replace('/','_',$cache);
$cache = $cache_path.'/'.$cache;
$cache = escapeshellcmd($cache);
?>
now just add this line
<?php
$cache
= str_replace(':','-',$cache);
?>
so it looks like this:
<?php
// concatenate commands for use in cache file name
$cache = $_SERVER['PATH_INFO'];
foreach (
$matches as $match) {
    
$cache .= '%'.$match[2].':'.$match[4];
}
$cache = str_replace('/','_',$cache);
$cache = str_replace(':','-',$cache);
$cache = $cache_path.'/'.$cache;
$cache = escapeshellcmd($cache);
?>
Now it should work for you... Hopefully :)

login or register to post comments

ERROR: Image conversion failed.

Submitted by mheinjr on March 26, 2006 - 17:41.

I have Apache2 and PHP 5 and the latest version of Imagemagick on my "windows" computer.

I changed the code as stated above because I was getting the same error. After changing the code I now get this:
ERROR: Image conversion failed. (<b>./_enginebay.jpg not found)

The filename is enginebay.jpg

If I try to run a file I don't have it gives this error:
ERROR: That image does not exist.

If I try to run flip: enginebay.jpg?flip I get this error:
Notice: Undefined offset: 4 in C:\Programs\Apache\Apache2\htdocs\images\magick.php on line 36

Notice: Undefined offset: 4 in C:\Programs\Apache\Apache2\htdocs\images\magick.php on line 57 ERROR: Invalid parameter.

If I try to resize to (50) I get this error:
ERROR: Image conversion failed. (<b>./_enginebay.jpg resize-50 not found)

login or register to post comments

Using other Image Magick Commands

Submitted by cephlon on April 24, 2006 - 21:09.

Great script. I have been able to use it fine for some commands, but some Image magick commands will not work. I am specifically trying to do Sepia and Black and White images. I put in a Query String like this: ?resize(500x500})+sepia-tone(80}) but all I get is ERROR: Invalid parameter. Any suggestions on how I should run this command? Thanks!

login or register to post comments

Having Trouble....

Submitted by Mute on April 25, 2006 - 14:37.

This script looks like it would be perfect for what I need but I'm having problems!

I get the "ERROR: Image conversion failed." whenever I try and do just "/magick.php/myimage.jpg".

As far as I can tell, exec($convert) does nothing. When I check my Apache logs I see this error...

"/usr/local/bin/convert: error while loading shared libraries: libMagick.so.10: cannot open shared object file: No such file or directory"

Any thoughts anyone? You're help would be greatly appreciated.

login or register to post comments

Problem...

Submitted by xzeneal on December 5, 2006 - 07:36.

I copy the script above and test it but I'm receiving this error :


Warning: preg_match_all() [function.preg-match-all]: Compilation failed: nothing to repeat at offset 0 in C:\Program Files\xampp\htdocs\test\magick1.php on line 25


Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\test\magick1.php:25) in C:\Program Files\xampp\htdocs\test\magick1.php on line 190


Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\test\magick1.php:25) in C:\Program Files\xampp\htdocs\test\magick1.php on line 191


Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\test\magick1.php:25) in C:\Program Files\xampp\htdocs\test\magick1.php on line 192


Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\test\magick1.php:25) in C:\Program Files\xampp\htdocs\test\magick1.php on line 194


Please help me out on this one...Please tell me what I'm missing to make the script work.. Thanks in advance

login or register to post comments

Great intentions - DOES NOT WORK TODAY

Submitted by fredsters_s on February 18, 2009 - 22:52.

Great idea etc. and would be perfect for what I need. However, with all the latest versions of PHP and Apache the script doesn't work. Throws errors on the second Regex and the caching behaves extremely erratically. Please update the tutorial to work with current systems - if not, you may as well delete it as it's just a time waster.

login or register to post comments

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.orgEvolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.