Page 1 of 1
Problem escaping parentheses
Posted: 2013-10-15T03:24:25-07:00
by laurens_at_hol
Hi,
I'm having some problems with parentheses in a convert command string.
In a php script I build a string with imagemagick convert commands. This string is used in a php exec() call.
The string contains parentheses to group some commands. I know I have to escape the parentheses with a backslash to make it work for Linux environments.
In my PHP script I check the OS, and for Linux type systems I add backslashes to escape the parentheses. For Windows type systems I don't add the escape backslashes.
My script runs correct on both Windows and Mac, but not on a CentOS system.
In the php script I add the backslash like this:
Code: Select all
// if Linux/CentOS
$str .= " \\" . $str;
...
// run convert commands
exec($str, $output, $retval);
While debugging in CentOs I noticed that the double backslash before the parentheses are not stripped. The commandstring that is run in exec still contains double backslashes, while in Mac the double backslashes are stripped and recognized as escape characters.
Does someone know how I could handle the parentheses and escaping in CentOS 6.3?
Thanks!
Re: Problem escaping parentheses
Posted: 2013-10-15T04:12:16-07:00
by laurens_at_hol
I can't find why it is not working in the CentOS environment.
I did a trivial test on CentOS:
Code: Select all
$str = "\\";
error_log($str);
// output in error log is "\\"
Code: Select all
$str = "\\";
error_log(stripslashes($str));
// output in error log is "" (empty string)
I thought it could have something to do with magic quotes, but magic quotes is Off on both the Mac and CentOS systems.
Does anyone has an idea why this is happening?
Re: Problem escaping parentheses
Posted: 2013-10-15T04:35:28-07:00
by Bonzo
Can you post the output of $str
Re: Problem escaping parentheses
Posted: 2013-10-15T04:48:56-07:00
by laurens_at_hol
I'm not sure what you mean Bonzo.
When I run this on CentOS
Code: Select all
$str = "\\";
error_log($str);
// output in error log is "\\"
When I run this on Max OSX
Code: Select all
$str = "\\";
error_log($str);
// output in error log is "\"
When I run this on CentOS
Code: Select all
$str = "\\";
error_log(var_export($str, true));
// output in error log is '\\\\'
When I run this on Mac OSX
Code: Select all
$str = "\\";
error_log(var_export($str, true));
// output in error log is '\\'
The result in Mac OSX is like I am expecting.
Re: Problem escaping parentheses
Posted: 2013-10-15T04:56:07-07:00
by Bonzo
You are building your command up into the variable $str what does $str contain?
It looks like you are using \\ instead of \
Code: Select all
// What is the print out of $str here?
echo $str;
exec($str, $output, $retval);
I have just noticed you have , between the variables in the exec( ) command. These should not be required and I do not know if they are having an effect.
Re: Problem escaping parentheses
Posted: 2013-10-15T05:09:50-07:00
by laurens_at_hol
Yes Bonzo, I build the commandstring that is used in the exec() call in the variable $str.
But in my debugging process I did some very simple tests where exec() is not used at all. The tests I described with logging a simple string. Nothing else is done.
The string I use is with double backslashes because a single backslash is a escape character in php string variables.
For example this:
Code: Select all
<?php
$str = "\";
error_log($str);
?>
results in an error: "PHP Parse error: syntax error, unexpected $end in path/to/file on line 4"
Re: Problem escaping parentheses
Posted: 2013-10-15T05:13:13-07:00
by Bonzo
I have no problem with my code using \( \) in exec( ).
Your test are of no help as what you need to know is the contents of $str as that is what Imagemagick is using.
Re: Problem escaping parentheses
Posted: 2013-10-15T05:37:03-07:00
by laurens_at_hol
Log
Code: Select all
/usr/bin/convert "/var/www/Tools/data/uploads/e3v3rui746hd55dhtskbcn2592525d35b947437.jpg" -profile '/var/www/Tools/public/sRGB.icm' -type 'TrueColor' -thumbnail '2048x1251' -density '36.8x36.8' -units 'PixelsPerCentimeter' -crop 1598x1230+225+11 -write mpr:big +delete +repage -crop 1472x1230+63+0 mpr:big -write mpr:center_lr +delete +repage -crop 63x1230+0+0 mpr:big -write mpr:left +delete +repage mpr:left -resize 147x1230! -write mpr:left_stretched +delete +repage -crop 63x1230+1535+0 mpr:big -write mpr:right +delete +repage mpr:right -resize 147x1230! -write mpr:right_stretched +delete +repage -gravity Center +append mpr:left_stretched mpr:center_lr mpr:right_stretched -write mpr:lcr +delete +repage -gravity North -crop 1766x1104+0+63 mpr:lcr -write mpr:center_tb +delete +repage -crop 1766x63+0+0 mpr:lcr -write mpr:top +delete +repage mpr:top -resize 1766x147! -write mpr:top_stretched +delete +repage -crop 1766x63+0+1167 mpr:lcr -write mpr:bottom +delete +repage mpr:bottom -resize 1766x147! -write mpr:bottom_stretched +delete +repage -gravity Center -append mpr:top_stretched mpr:center_tb mpr:bottom_stretched -gravity North +repage -resample '28.3464567' -density '72x72' -units 'PixelsPerInch' \\( -stroke '#848484' -strokewidth '0' -draw 'line 0,0 1359,0' -stroke '#848484' -strokewidth '0' -draw 'line 0,0 0,1075' -stroke '#848484' -strokewidth '0' -draw 'line 1359,0 1359,1075' -stroke '#848484' -strokewidth '0' -draw 'line 0,1075 1359,1075' -gravity 'Center' -extent '1445x1161' -stroke '#848484' -strokewidth '1' -draw 'line 0,0 1444,0' -stroke '#848484' -strokewidth '1' -draw 'line 0,0 0,1160' -stroke '#848484' -strokewidth '1' -draw 'line 1444,0 1444,1160' -stroke '#848484' -strokewidth '1' -draw 'line 0,1160 1444,1160' -stroke '#848484' -strokewidth '1' -draw 'line 722,0 722,10' -stroke '#848484' -strokewidth '1' -draw 'line 722,1161 722,1149' -stroke '#848484' -strokewidth '1' -draw 'line 0,580 10,580' -stroke '#848484' -strokewidth '1' -draw 'line 1433,580 1445,580' \\) "cache/99adca5d75beef59938b2053ebe2e8f185e7c2b3be7b11bc8acbc25f65afecef.jpg"
The parentheses are preceded by double backslashes, and that is why the convert process is breaking. It seams to me that the double backslashes are not recognized as escape characters in the CentOS environment, but I don't know why.
Re: Problem escaping parentheses
Posted: 2013-10-15T10:35:53-07:00
by Bonzo
I still can not see why you want double backslashes; I use Imagemagick with php and have never had to do that apart from when you are carring on a long line.
Have you tried it with one only?
Re: Problem escaping parentheses
Posted: 2013-10-16T00:03:25-07:00
by laurens_at_hol
It is not that I want to use double backslashes, but in order to add a single backslash to a php string variable it must be escaped with another backslash.
When the backslash is not escaped with a second backslash this produces a syntax error.