使用新的文件名和扩展名将输出图像保存到不同的目录中

Save the output image into a different directory, with a new filename and extension

假设我的 "image.php" 文件中有这段代码,它将创建一个新的 GD 图像流并输出图像:

header ('Content-Type: image/png');
$im = @imagecreatetruecolor(120, 20)
      or die('Cannot Initialize new GD image stream');
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);
imagepng($im);
imagedestroy($im);

这是我的示例图:

file -> /root/template/program/image.php

directory -> /root/template/images

如何将 "image.php" 的输出图像保存到 "/images"/images" 目录,并使用不同的文件名和扩展名 "file.png"?

filename parameter
The path to save the file to. If not set or NULL, the raw image stream will be outputted directly.

我找到了解决方案,只需在 imagepng() 函数中添加第二个参数文件名即可:

imagepng($im, '../images/file.png');