我正在使用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);这是有效的,但我希望它能自动触发浏览器内的下载,而不是显示图像。
怎么做,需要设置头部吗?
发布于 2018-02-05 03:31:34
你所要做的就是设置一个额外的头:
// 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);Here是文档
发布于 2018-02-05 04:17:53
我认为您至少需要3个headers:
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中运行。
https://stackoverflow.com/questions/48612131
复制相似问题