我正在一起使用imagemagik和php来处理一些单词。我首先把一个句子分成单词,然后准备这些单词作为一个长命令行参数,我计划通过PHP的exec()命令调用这个参数。我仔细核对了这个论点;据我所知,所有字符都被正确转义,包括单引号和双引号。exec()函数不起作用,表示"The filename, directory name, or volume label syntax is incorrect"
。但是,当我回显$escaped variable
并将回显的字符串分配给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
使用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);