如何使像pdf文件一样的jpg图像下载? [英] How do I make a jpg image download like a pdf file does?

查看:115
本文介绍了如何使像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图像下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆