如何使用php输出图像并下载

jmi*_*ler 0 php image download

是否可以使用 php 渲染/输出图像并强制下载,而不将图像保存到服务器?

我查看了http://php.net/manual/en/function.imagejpeg.php并可以渲染图像并使用保存图像imagejpeg();

我尝试创建指向服务器上保存的图像的超链接,并使用 强制下载download

只是想知道是否有更简单/更简单的方法?

rol*_*rer 6

您可以使用imagejpeg()将图像输出到浏览器。(文档

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

// Output the image
imagejpeg($image);
Run Code Online (Sandbox Code Playgroud)

当您省略第二个参数imagejpeg()或将其设置为null图像时,图像将被发送到浏览器(以二进制形式)。

要在浏览器中触发下载,您需要设置另一个标头值:

header('Content-Disposition: attachment; filename="YourFilenameHere.jpg"');
Run Code Online (Sandbox Code Playgroud)

这告诉浏览器应该将文件下载为YourFilenameHere.jpg.