如何使像pdf文件一样的jpg图像下载?

编程入门 行业动态 更新时间:2024-10-27 17:13:01
本文介绍了如何使像pdf文件一样的jpg图像下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个请求,以与pdf文件相同的方式下载jpg图像。

I have a request to make a jpg image download in the same way a pdf file would.

目前,如果我将jpg图像添加到网页中一个链接,它将在另一个浏览器窗口中打开,而不是实际下载到用户计算机。但是,一个pdf文件将。

At present if I add a jpg image to a web page as a link, it will open in another browser window rather than actually download to the users computer. However, a pdf file will.

这是标准代码:

<a href="/images/my-image.jpg">download image</a>

感谢任何关于此的指导。

I would appreciate any guidance on this.

谢谢。

推荐答案

如果您在服务器上有PHP,可以这样做。

If you have PHP on the server you can do this.

<?php // Force download of image file specified in URL query string and which // is in the same directory as this script: if(!empty($_GET['img'])) { $filename = basename($_GET['img']); // don't accept other directories $size = @getimagesize($filename); $fp = @fopen($filename, "rb"); if ($size && $fp) { header("Content-type: {$size['mime']}"); header("Content-Length: " . filesize($filename)); header("Content-Disposition: attachment; filename=$filename"); header('Content-Transfer-Encoding: binary'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); fpassthru($fp); exit; } } header("HTTP/1.0 404 Not Found"); ?>

HTML然后看起来像这样

HTML Would then look like this

<img src="/images/download.php?img=imagename.jpg" alt="test">

更多推荐

如何使像pdf文件一样的jpg图像下载?

本文发布于:2023-07-23 07:40:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1195220.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   文件   pdf   jpg

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!