将输出图像保存到另一个目录中,并使用新的文件名和扩展名


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

目录->/root/template/images

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

文件名参数
保存文件的路径。如果未设置或为NULL,则原始图像流将直接输出

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

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