Page 1 of 1
Script not creating a resized copy on server?
Posted: 2011-04-21T17:37:07-07:00
by prototype14
Hi All,
I've taken over the production of a site for a client of mine, which is using ImageMagick and I'm a bit stumped.
I have a php script that gets called whenever an image is loaded on the site (so database lookups and the like can be done), which then uses ImageMagick to resize and presumably save a cached version of the thumbnail. Unfortunately the saving part of things seems to be an issue. Having a read through the documentation, it appears that everything is laid out correctly.
If I make my script exit with the command that would normally resize the image, I get this:
Code: Select all
convert "prod-M600-Hunter.jpg" -resize 33.333333333333% "list/prod-M600-Hunter.jpg"
I'm wondering if anyone else has encountered this issue, or could point me in the right direction with things?
Thanks in advance
Re: Script not creating a resized copy on server?
Posted: 2011-04-21T18:27:42-07:00
by fmw42
I am not a PHP programmer of any expertise, but perhaps you need to show more code or explain where and when it stops and with what error messages. Showing only the command line is not generally much help, though it looks fine. The only thing I can think of is to escape the %. Also do your directories have correct permissions? Does PHP know where IM is located otherwise, put the full path to convert in your presumably exec() command. Also be careful of quotes in your command line and the same quotes in PHP expressions. That could cause problems.
Perhaps other PHP experts can help if you provide more details.
Re: Script not creating a resized copy on server?
Posted: 2011-04-21T18:46:08-07:00
by prototype14
Sure thing, here's the part of the script that deals with ImageMagick, assume that $image_name is set to an image that exists (which in my case it is, I can echo $image_name within an <img> and get a picture)
Code: Select all
// check if resized image already exists
$cache = "$cache_path/$image_name";
//echo "$image_name<br />$cache";
if (!file_exists($cache)) {
// there is no cached image yet, so we'll need to create it first
$source_image = $image_name;
$target_image = $cache;
list ($resized_width,$resized_height) = get_resized_width_height($image_size);
//echo "$resized_height/$resized_width<br />";
$imginfo = getimagesize($source_image);
$orig_width = $imginfo[0];
$orig_height = $imginfo[1];
if (($orig_width/$resized_width) > ($orig_height/$resized_height)){
$ratio = $orig_height/$resized_height;
}else{
$ratio = $orig_width/$resized_width;
}
//echo "$resized_height/$resized_width<br />";
$new_width = $orig_width/$ratio;
$new_height = $orig_height/$ratio;
$top = round(($new_height - $resized_height)/2);
$left = round(($new_width - $resized_width)/2);
if ($top == "-0"){
$top = 0;
}
if ($left == "-0"){
$left = 0;
}
$crop_command = ("-crop $resized_width"."x"."$resized_height+$left+$top"); // remove excess from left and right
$resize_command = ("-resize ".(100/$ratio)."%");
$command = "$resize_command $crop_command";
$command = "$resize_command";
//exit ($command);
$convert = $convert_path.' "'.$source_image.'" '.$command.' "'.$target_image.'"';
//exit($convert);
ob_start();
system($convert);
ob_end_clean();
}
// there should be a file named $cache now
//echo '<img src="'.$cache.'" />';
if (!file_exists($cache)) {
die('Image conversion failed.');
}
When this runs through, I get 'Image conversion failed', but no other errors (display_errors is set to 'on')
The directory permissions are 755 for the cache directory.
The bizarre thing is that the site already had an image.php file that seemingly worked perfectly - I took a copy of this and added in some extra database reading stuff, and this seems to be where things have gone awry.
Re: Script not creating a resized copy on server?
Posted: 2011-04-21T19:08:16-07:00
by fmw42
Do you know that you have access to system() command and have you tested to see that it can find IM
Try
<?php
system("/usr/local/bin/convert -version");
?>
replace /usr/local/bin/ with your own path to convert.
If that works, then perhaps you are having trouble with quotes. Again I am not an expert on mixing quotes in command lines with quotes for PHP. Hopefully a PHP expert will be able to see if they recognize any issue.
I am concerned about the double quotes used in these lines. Looks like quotes within quotes to me:
$crop_command = ("-crop $resized_width"."x"."$resized_height+$left+$top"); // remove excess from left and right
$resize_command = ("-resize ".(100/$ratio)."%");
$command = "$resize_command $crop_command";
Re: Script not creating a resized copy on server?
Posted: 2011-04-21T21:33:00-07:00
by anthony
This is not strictly ImageMagick, but a PHP problem however it does appear time and again.
PHP runs in a restricted environemnt. Can it actually write to the directory involved?
EG: try writing the image to /tmp and see if it appear there!
Have you redirected errors to a log file? or looked at the server logs? Perhaps there is an error message that will help!
http://www.ict.griffith.edu.au/~anthony ... /php.hints
See IM Examples, ImageMagick APIs. PHP, and go though the steps...
http://www.imagemagick.org/Usage/api/#php
Re: Script not creating a resized copy on server?
Posted: 2011-04-21T21:42:11-07:00
by anthony
prototype14 wrote:Code: Select all
....
$crop_command = ("-crop $resized_width"."x"."$resized_height+$left+$top"); // remove excess from left and right
$resize_command = ("-resize ".(100/$ratio)."%");
$command = "$resize_command $crop_command";
$command = "$resize_command";
//exit ($command);
$convert = $convert_path.' "'.$source_image.'" '.$command.' "'.$target_image.'"';
Can you actually print an example of the final "$convert" string.
It may be that you have extra spaces around the numbers, or may be a quote in a filename (you do check user input completely don't you?) or something else that the shell is miss-interpreting.
It can be a problem that PHP does not provide a nice way of running commands without going though a shell to break the command into individual 'word' arguments.
Re: Script not creating a resized copy on server?
Posted: 2011-04-21T23:31:31-07:00
by prototype14
Sure, it's what I had posted originally:
Code: Select all
convert "prod-M600-Hunter.jpg" -resize 33.333333333333% "list/prod-M600-Hunter.jpg"
Re: Script not creating a resized copy on server?
Posted: 2011-04-22T23:49:22-07:00
by anthony
Well that looks fine. Is the PHP running in the right directory?
Is the 'list' sub-directory writable by the user PHP is running as?
Do you have something like SE-Linux Enabled?
Re: Script not creating a resized copy on server?
Posted: 2011-04-23T04:03:11-07:00
by prototype14
It looks like it was a permissions issue. Everything works great now.
Re: Script not creating a resized copy on server?
Posted: 2011-04-23T15:01:44-07:00
by anthony
Good.
Like I said, be very careful with using a writable sub-directory that is directly accessible from the web server. Be sure to check all inputs throughly for any unexpected characters, and watch out of parellel execution. Good luck with your application.