Php ImageMagick创建唯一文件名

Php ImageMagick创建唯一文件名,php,html,imagemagick,Php,Html,Imagemagick,我正在尝试使用ImageMagick生成图像,请检查以下代码: <? $label=$_POST["label"]; $image =$_POST["image"]; $cmd = " -background transparent label:\"$label\" -stroke red -strokewidth 0 ". " \( -clone 0 -tile \"" . $image . "[0]\" -stroke red -strokewidth 0 -gravity cent

我正在尝试使用ImageMagick生成图像,请检查以下代码:

<?
$label=$_POST["label"];
$image =$_POST["image"];
$cmd = " -background transparent label:\"$label\" -stroke red -strokewidth 0 ".
"  \( -clone 0 -tile \"" . $image . "[0]\" -stroke red -strokewidth 0 -gravity center  -annotate +0+0 \"$label\" \) ".
"  \( -clone 0 -tile \"" . $image . "[1]\" -stroke red -strokewidth 0 -gravity center -annotate +0+0 \"$label\" \) ".
"  \( -clone 0 -tile \"" . $image . "[2]\" -stroke red -strokewidth 0 -gravity center -annotate +0+0 \"$label\" \)  ".
"  \( -clone 0 -tile \"" . $image . "[3]\" -stroke red -strokewidth 0 -gravity center -annotate +0+0 \"$label\" \)  ".


 "  -delete 0 -set delay 20 -loop 0 -trim +repage -layers Optimize ";

$array=array();
echo "<pre>";
exec("convert $cmd masked.gif 2>&1", $array);
echo "<br>".print_r($array)."<br>";
echo "</pre>";
echo "<img src='masked.gif'>";
?>

通过生成名为
masked.gif
的图像,上述代码可以正常工作,但我现在需要做两件事:

  • 生成的图像应具有唯一的文件名{每次生成不同的文件名}
  • 生成的图像应该保存在不同的文件夹中,而不是根目录,比如说目录名为“coolpics”
  • 请帮我把这个修好。感谢您的回复

  • 对于唯一文件名,请检查函数
  • 要保存目录,只需将其与文件名连接,如:
  • //你的命令在这里 $directory=“/data/images/coolpics/” $name=uniqid(); exec(escapeshellcmd(“转换$cmd$directory$name 2>&1”),$array);
    请注意,您将POST值直接传递给系统命令,这可能是一个非常严重的安全失败。用于确保系统安全。

    获取唯一文件名的一个简单解决方案是使用时间戳(
    microtime()
    ())。要将图像保存在另一个文件夹中,只需将路径添加到exec参数

    整个exec命令如下所示:


    对于一个独特的名称,如md5($some_data),您可以做很多事情,也可以使用microtime。至于在另一个文件夹中保存,您是否尝试在
    exec
    中提供所需的路径,是否有效? // your command here $directory = "/data/images/coolpics/" $name = uniqid(); exec(escapeshellcmd("convert $cmd $directory$name 2>&1"), $array);
    exec("convert $cmd /your/path/".str_replace(".","-",microtime(true)).".gif 2>&1", $array);