在 PHP 中输​​出图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1851849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:03:19  来源:igfitidea点击:

Output an Image in PHP

phpimage

提问by steven

I have an image $file( eg ../image.jpg)

我有一个图像$file(例如../image.jpg

which has a mime type $type

它有一个 mime 类型 $type

How can I output it to the browser?

如何将其输出到浏览器?

回答by Emre Yazici

$file = '../image.jpg';
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);

回答by Benjamin Wohlwend

If you have the liberty to configure your webserver yourself, tools like mod_xsendfile(for Apache) are considerably better than reading and printing the file in PHP. Your PHP code would look like this:

如果您可以自由配置自己的网络服务器,像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 picks up the X-Sendfile header and sends the file to the browser itself. This can make a real difference in performance, especially for big files. Most of the proposed solutions read the whole file into memory and then print it out. That's OK for a 20kbyte image file, but if you have a 200 MByte TIFF file, you're bound to get problems.

mod_xsendfile 获取 X-Sendfile 标头并将文件发送到浏览器本身。这会对性能产生真正的影响,尤其是对于大文件。大多数建议的解决方案将整个文件读入内存,然后将其打印出来。对于 20kbyte 的图像文件来说这没问题,但是如果你有一个 200MB 的 TIFF 文件,你肯定会遇到问题。

回答by Mike

$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;
    }
}

http://php.net/manual/en/function.fpassthru.php

http://php.net/manual/en/function.fpassthru.php

回答by Carlos Lima

Try this:

尝试这个:

<?php
  header("Content-type: image/jpeg");
  readfile("/path/to/image.jpg");
  exit(0);
?>

回答by code_burgar

header('Content-type: image/jpeg');
readfile($image);

回答by Hans

For the next guy or gal hitting this problem, here's what worked for me:

对于下一个遇到这个问题的人或女孩,这对我有用:

ob_start();
header('Content-Type: '.$mimetype);
ob_end_clean();
$fp = fopen($fullyQualifiedFilepath, 'rb');
fpassthru($fp);
exit;

You need all of that, and only that. If your mimetype varies, have a look at PHP's mime_content_type($filepath)

您需要所有这些,而且仅此而已。如果您的 mimetype 有所不同,请查看 PHP 的 mime_content_type($filepath)

回答by Tomo Hadi Sutomo

    $file = '../image.jpg';
    $type = 'image/jpeg';
    header('Content-Type:'.$type);
    header('Content-Length: ' . filesize($file));
    $img = file_get_contents($file);
    echo $img;

This is works for me! I have test it on code igniter. if i use readfile, the image won't display. Sometimes only display jpg, sometimes only big file. But after i changed it to "file_get_contents" , I get the flavour, and works!! this is the screenshoot: Screenshot of "secure image" from database

这对我有用!我已经在代码点火器上对其进行了测试。如果我使用 readfile,图像将不会显示。有时只显示jpg,有时只显示大文件。但是在我将其更改为 "file_get_contents" 之后,我得到了味道,并且有效!!这是屏幕截图:来自数据库的“安全图像”的屏幕 截图

回答by Fabien Sa

You can use finfo(PHP 5.3+) to get the right MIME type.

您可以使用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: You don't have to specify Content-Length, Apache will do it for you.

PS:您不必指定Content-Length,Apache 会为您指定。

回答by Pascal MARTIN

You can use headerto send the right Content-type :

您可以使用header发送正确的 Content-type :

header('Content-Type: ' . $type);

And readfileto output the content of the image :

readfile输出图像的内容:

readfile($file);


And maybe (probably not necessary, but, just in case)you'll have to send the Content-Length header too :


也许(可能没有必要,但是,以防万一)您也必须发送 Content-Length 标头:

header('Content-Length: ' . filesize($file));


Note : make sure you don't output anything else than your image data (no white space, for instance), or it will no longer be a valid image.


注意:确保除了图像数据之外不输出任何其他内容(例如没有空格),否则它将不再是有效图像。

回答by Matthew Scharley

<?php

header("Content-Type: $type");
readfile($file);

That's the short version. There's a few extra little things you can do to make things nicer, but that'll work for you.

那是简短的版本。你可以做一些额外的小事情来让事情变得更好,但这对你有用。