converting php string to shell command

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
erdan

converting php string to shell command

Post by erdan »

I want to create a photo annotation using the command line convert function

If I go to the command line and enter the code it works

Code: Select all

/usr/bin/convert -fill yellow -pointsize 30 -draw 'text 10,10 "message_here"' /home/domain/public_html/files/image.jpg /home/domain/public_html/files/anno-image.jpg
when I enclose it in a php script as

Code: Select all

$string = "
\/usr\/bin\/convert -fill yellow -pointsize 30 -draw 'text 10,10 \"message_here\"' \/home\/domain\/public_html\/files\/image.jpg \/home\/domain\/public_html\/files\/anno-image.jpg"

exec($string);
(when I echo the string, it comes out ok with the escaped characters--- but doesnt process correctly)


It does not work and it is dumping core.98327 files in my html directory?
(with multiple files per operation and varying serial numbers)

Any ideas?
Do I need to enter carriage returns in the shell command
Can this be encoded into the string as escaped characters?
Is exec the proper command -- do I need shell_exec? or system(), or other command?

I am trying to implement this on multiple servers and want to limit the wholes that have to be opened and limit the dangerous php fcns that need to be used...
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: converting php string to shell command

Post by Bonzo »

I would write the code like this as you do not need to worry about so many escape characters.

Code: Select all

exec("/usr/bin/convert /home/domain/public_html/files/image.jpg -fill yellow -pointsize 30 -draw 'text 10,10 \"message_here\"' /home/domain/public_html/files/anno-image.jpg");
You can also use relative paths from the code.
erdan

Re: converting php string to shell command

Post by erdan »

Same problems...

I am able to produce the same string, but not get it to execute properly from the php script.
ridera

Re: converting php string to shell command

Post by ridera »

Looks like a paths problem. And make certain the dir has the proper permissions. Try this. See the $report; it'll help you debug.

Code: Select all

exec("/usr/bin/convert /files/image.jpg -fill yellow -pointsize 30 -draw 'text 10,10 \"message_here\"' /files/anno-image.jpg", $report);

print_r($report);

erdan

Re: converting php string to shell command

Post by erdan »

I tried to adjust the paths as indicated and it still does not work...

To make it more confusing, there are some operations that do work. I have a test page that echo's the strings that are sent to the convert application and shows the images that should/are created...

Using this methodology, I am able to resize the images, and create a border... It is just the drawing or more complex operations that are failing.... Not sure why? whether it is syntax, " ' issues, or that these operations require other apps/modules...

I initiall got gs errors because it was not installed. I installed it and pasted these strings directly into a shell window and the conversion went fine... It is failing just when executed by my php scripts and JUST for this last script

Code: Select all

/usr/bin/convert -quality 100 -antialias -resize x320 /home/websites/public_html/files/0708311807_Monsoon.jpg /home/websites/public_html/files/0708311807_Monsoon.jpg  ###WORKS###

/usr/bin/convert -quality 100 -antialias -resize x90 /home/websites/public_html/files/0708311807_Monsoon.jpg /home/websites/public_html/files/thumb/0708311807_Monsoon.jpg ###WORKS###

/usr/bin/convert -mattecolor black -frame 5x5+2+2 /home/websites/public_html/files/0708311807_Monsoon.jpg /home/websites/public_html/files/0708311807_Monsoon.jpg ###WORKS###


report:
Array ( )

/usr/bin/convert -fill yellow -pointsize 30 -draw 'text 10,10 "message_here"' /files/0708311807_Monsoon.jpg /files/anno-0708311807_Monsoon.jpg ###FAILS###

/usr/bin/convert /files/0708311815_Monsoon.jpg -fill yellow -pointsize 30 -draw 'text 10,10 "message_here"' /files/anno-0708311815_Monsoon.jpg ###FAILS###

/usr/bin/convert /home/websites/public_html/files/0708311817_Monsoon.jpg -fill yellow -pointsize 30 -draw 'text 10,10 "message_here"' /home/websites/public_html/files/anno-0708311817_Monsoon.jpg ###FAILS###

###################################
The php script that generates this echoed string is:

$imstring="/usr/bin/convert $from -fill yellow -pointsize 30 -draw 'text 10,35 \"message\"' $to_anno";
exec($imstring, $report);
echo $imstring;

Not sure if it means anything but it is copying over the image, it is just not applying the annotation to it. ie it is loading a exact copy of the initial file as the processed file...
erdan

Re: converting php string to shell command

Post by erdan »

If I am right, this appears to be a server permission issue.

I am running the php command from a domain account of a server that I manage.
I enabled phpsuexec and suexec to prevent stuff running as nobody.

When I run the shell commands, I am logged in as root and everything works fine
When I check the owner of the successful php executed commands, the owner is the domain user (websites)

I did check the perms of /usr/bin/convert and /usr/bin/gs and they are identical.

I am in way over my head....

if anyone familiar with these issues, ie which applications (gs, etc) are being called in these draw/annotation scripts that are not called when just resizing using convert... How do I reset permissions so the domain user can perform needed operations using convert.... I would love some help...

thanks
dan
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: converting php string to shell command

Post by anthony »

Add 2>&1 to the command so that the error channel is redirected to stdout.
You can then see the error that the command is reporting!!!!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: converting php string to shell command

Post by Bonzo »

Expanding on Anthonys post try this and see what the output is.

Code: Select all

exec("/usr/bin/convert $from -fill yellow -pointsize 30 -draw 'text 10,35 \"message\"' $to_anno 2>&1", $array); 
print_r($array); 
 
Post Reply