2015-11-02 46 views
0

我一起使用imagemagick和php来处理一些单词。我首先将一个句子分解成单词,然后准备这些单词作为长命令行参数,我打算通过PHP的exec()命令调用这个参数。我已经仔细检查了这个论点。根据我的知识,所有的角色都可以正确转义,包括单引号和双引号。 exec()函数不起作用,说"The filename, directory name, or volume label syntax is incorrect"。但是,当我回显$escaped variable并将回显的字符串分配给php的exec()时,它可以正常工作。PHP的exec()命令不接受执行的有效变量

这里是回馈字符串

exec("convert -background DeepSkyBlue -fill black -font Ultima-Alt-Bold.ttf -pointsize 90 -gravity center -density 90 label:\"SALIVA \" -fill black -font Ultima-Alt-Bold.ttf -pointsize 90 -gravity center -density 90 label:\"USED \" -fill black -font Ultima-Alt-Bold.ttf -pointsize 90 -gravity center -density 90 label:\"AS! \" +append Ulti.png"); // It works 

我使用的代码:

$file = 'theboldfont.ttf'; 
$name = substr($file, 0, 4); 

$s = "SALIVA USED AS!"; 

$words = explode(' ',$s); 
$string = ''; 

foreach ($words as $word) 
{ 
    $string .= " " . '-fill black -font ' . $file . ' -pointsize 90 -gravity center -density 90 label:"' . $word . ' "'; 
} 

$command = 'convert -background DeepSkyBlue ' . $string . ' +append ' . $name. '.png'; 

function w32escapeshellarg($s) 
{ return '"' . addcslashes($s, '\\"') . '"'; } 

$escaped = w32escapeshellarg($command); 
exec($escaped); // It is not working 
+0

你看着使用[PHP iMagick图书馆](http://php.net/manual/en/book.imagick.php)?调用本地PHP函数应该比运行'exec()'命令更可靠 – samlev

+0

检查你的php.ini文件是否安装了正确的imagemagick –

+0

@Saurabh,是的,它安装正确。 –

回答

1

使用escapeshellarg逃脱你的字符串:

<?php 
$file = 'theboldfont.ttf'; 
$name = substr($file, 0, 4); 

$s = "SALIVA USED AS!"; 

$words = explode(' ', $s); 
$string = ''; 

foreach ($words as $word) { 
    $string .= ' -fill black -font ' . escapeshellarg($file) . ' -pointsize 90 -gravity center -density 90 label:' . escapeshellarg($word); 
} 

$command = 'convert -background DeepSkyBlue ' . $string . ' +append ' . escapeshellarg($name . '.png'); 

exec($command);