2015-11-02 125 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);