【发布时间】:2010-12-23 12:53:58
【问题描述】:
我有一张图片$file(例如../image.jpg)
mime 类型为$type
如何输出到浏览器?
【问题讨论】:
-
到目前为止你尝试过什么?使用
<img>标签有什么问题吗?
我有一张图片$file(例如../image.jpg)
mime 类型为$type
如何输出到浏览器?
【问题讨论】:
<img> 标签有什么问题吗?
header("Content-type: image/png");
echo file_get_contents(".../image.png");
第一步是从特定位置检索图像,然后将其存储到一个变量中,为此我们使用函数 file_get_contents() 并将目标作为参数。 接下来我们使用头文件将输出页面的内容类型设置为图像类型。 最后我们使用 echo 打印检索到的文件。
【讨论】:
Content-type: image/png输出一张JPG图片?另外,为什么将整个文件读入一个变量? readfile不应该有更好的内存性能吗?
(在accepted answer...上展开)
我需要:
jpg 图像和 动画gif,和, 我通过在图像所在的子文件夹中创建一个“辅助”.htaccess 文件来完成此操作。
该文件仅包含一行:
AddHandler application/x-httpd-lsphp .jpg .jpeg .gif
在同一个文件夹中,我放置了两个“原始”图像文件(我们将它们称为 orig.jpg 和 orig.gif),以及下面 [simplified] 脚本的两个变体(另存为 myimage.jpg和myimage.gif)...
<?php
error_reporting(0); //hide errors (displaying one would break the image)
//get user IP and the pseudo-image's URL
if(isset($_SERVER['REMOTE_ADDR'])) {$ip =$_SERVER['REMOTE_ADDR'];}else{$ip= '(unknown)';}
if(isset($_SERVER['REQUEST_URI'])) {$url=$_SERVER['REQUEST_URI'];}else{$url='(unknown)';}
//log the visit
require_once('connect.php'); //file with db connection info
$conn = new mysqli($servername, $username, $password, $dbname);
if (!$conn->connect_error) { //if connected then save mySQL record
$conn->query("INSERT INTO imageclicks (image, ip) VALUES ('$url', '$ip');");
$conn->close(); //(datetime is auto-added to table with default of 'now')
}
//display the image
$imgfile='orig.jpg'; // or 'orig.gif'
header('Content-Type: image/jpeg'); // or 'image/gif'
header('Content-Length: '.filesize($imgfile));
header('Cache-Control: no-cache');
readfile($imgfile);
?>
图像可以正常渲染(或动画),并且可以通过图像的任何正常方式调用(如<img> 标签),并将保存访问 IP 的记录,同时对用户不可见。
【讨论】:
对于遇到这个问题的下一个男人或女孩,这对我有用:
ob_start();
header('Content-Type: '.$mimetype);
ob_end_clean();
$fp = fopen($fullyQualifiedFilepath, 'rb');
fpassthru($fp);
exit;
您需要所有这些,而且仅此而已。如果您的 mimetype 不同,请查看 PHP 的 mime_content_type($filepath)
【讨论】:
$file = '../image.jpg';
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
$img = file_get_contents($file);
echo $img;
这对我有用!我已经在代码点火器上对其进行了测试。如果我使用 readfile,图像将不会显示。有时只显示jpg,有时只显示大文件。但是在我将它更改为 "file_get_contents" 之后,我得到了味道,并且可以工作! 这是屏幕截图: Screenshot of "secure image" from database
【讨论】:
您可以使用finfo (PHP 5.3+) 来获得正确的 MIME 类型。
$filePath = 'YOUR_FILE.XYZ';
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$contentType = finfo_file($finfo, $filePath);
finfo_close($finfo);
header('Content-Type: ' . $contentType);
readfile($filePath);
PS:您不必指定Content-Length,Apache 会为您完成。
【讨论】:
$file = '../image.jpg';
if (file_exists($file))
{
$size = getimagesize($file);
$fp = fopen($file, 'rb');
if ($size and $fp)
{
// Optional never cache
// header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
// header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
// header('Pragma: no-cache');
// Optional cache if not changed
// header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($file)).' GMT');
// Optional send not modified
// if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) and
// filemtime($file) == strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']))
// {
// header('HTTP/1.1 304 Not Modified');
// }
header('Content-Type: '.$size['mime']);
header('Content-Length: '.filesize($file));
fpassthru($fp);
exit;
}
}
【讨论】:
如果您可以自己配置网络服务器,mod_xsendfile(用于 Apache)之类的工具比在 PHP 中读取和打印文件要好得多。您的 PHP 代码如下所示:
header("Content-type: $type");
header("X-Sendfile: $file"); # make sure $file is the full path, not relative
exit();
mod_xsendfile 获取 X-Sendfile 标头并将文件发送到浏览器本身。这可以对性能产生真正的影响,尤其是对于大文件。大多数建议的解决方案将整个文件读入内存,然后将其打印出来。 20kbyte 的图像文件没问题,但如果你有一个 200 MB 的 TIFF 文件,你肯定会遇到问题。
【讨论】:
试试这个:
<?php
header("Content-type: image/jpeg");
readfile("/path/to/image.jpg");
exit(0);
?>
【讨论】:
header('Content-type: image/jpeg');
readfile($image);
【讨论】:
$file = '../image.jpg';
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);
【讨论】:
<?php
header("Content-Type: $type");
readfile($file);
这是简短的版本。你可以做一些额外的小事来让事情变得更好,但这对你有用。
【讨论】: