html - 强制 anchor href 将八位字节流视为图像?

标签 html image mime-types

我有一些图像错误地存储在 S3 上,MIME 类型为 application/octet-stream 。当我将这些图像放在带有 anchor 的网站上时,即 <a href="imgurl" ,图像会自动下载,而不是在浏览器中查看。

是否有办法解决此问题并强制浏览器将链接视为图像?

最佳答案

我不知道为什么 MightyPork 删除了他的答案,但基本想法是在 PHP 中创建一个代理,它将为您提供具有正确 MIME 类型的文件。

<?php

function error403() {
  header('HTTP/1.0 403 Forbidden');
  die('You are not allowed to access this file.');     
}

function serveFile($file, $mimeType) {
  header('Content-Type: ', $mimeType);
  header('Content-Length: ', filesize($file));

  readfile($file);
}

function determineMimeType($file) {
  $mimeMap = array(
    'png' => 'image/png',
    'jpg' => 'image/jpeg',
    'jpeg' => 'image/jpeg',
    'gif' => 'image/gif'
  );
  $fileExt = strtolower(pathinfo($path, PATHINFO_EXTENSION));
  if (isset($mimeMap[$fileExt])) {
    return $mimeMap[$fileExt];
  }
  return false;
}

// entry point
$whitelist = array('test1.png', 'test2.png');
$filePath = isset($_GET['file']) ? $_GET['file'] : error403();

if (!in_array($filePath, $whitelist)) {
  error403();
}

$mime = determineMimeType($filePath);
if ($mime === false) {
  // error handling, logging, etc.
}
serveFile($filePath, $mime);

关于html - 强制 anchor href 将八位字节流视为图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18088405/

相关文章:

android - Gmail 附件和自定义扩展

html - Div 向左溢出

html - 他们有什么方法可以更改 html5 中的输入日期格式

jquery - 如何使用 jQuery 在 URL 获取上确定像 pinterest 一样的图像大小

c# - 在 Repeater 中更新图像的 ImageUrl

Android:通过 Gmail 共享 zip 文件

html - 如何使用 css 在没有为 "select"选择任何内容时设置默认提示

html - 所见即所得编辑器 JavaScript : semantic and machine understanding

html - 如何: auto resize featured image,博主?

php - 在 PHP 中检测 MIME 类型