0

我正在使用 PHP imagejpeg 创建一个像这样的简单图像..

// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);

// Set the content type header - in this case image/jpeg
header('Content-Type: image/jpeg');

// Output the image
imagejpeg($im);

// Free up memory
imagedestroy($im);

这是有效的,但我希望它自动触发浏览器内的下载而不是显示图像。

我该怎么做,我需要设置标题吗?

4

2 回答 2

0

您所要做的就是设置一个额外的标题:

// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);

// Set the content type header - in this case image/jpeg
header('Content-Type: image/jpeg');
header('Content-Disposition: attachment; filename=file-name.jpg'); // This will tell the browser to download it

// Output the image
imagejpeg($im);

// Free up memory
imagedestroy($im);

是文档

于 2018-02-04T19:31:33.617 回答
0

我相信你至少需要 3 个标题:

header("Content-Type: image/jpeg"); // Content type
header("Content-Disposition: attachment; filename=file-name.jpg"); // Content deposition
header("Content-Length: $fileSize"); // YOUR FILE SIZE

否则,下载将无法在 Firefox 中运行。

于 2018-02-04T20:17:53.053 回答