使用file_get_contents显示图像

时间:2022-10-18 20:31:16

how can I display an image retrieved using file_get_contents in php?

如何在php中显示使用file_get_contents检索的图像?

Do i need to modify the headers and just echo it or something?

我是否需要修改标题并回显它或什么?

Thanks!

谢谢!

6 个解决方案

#1


29  

Do i need to modify the headers and just echo it or something?

我是否需要修改标题并回显它或什么?

exactly.

究竟。

Send a header("content-type: image/your_image_type"); and the data afterwards.

发送标题(“content-type:image / your_image_type”);以及之后的数据。

#2


56  

You can use readfile and output the image headers which you can get from getimagesize like this:

你可以使用readfile并输出你可以从getimagesize获得的图像标题,如下所示:

$remoteImage = "http://www.example.com/gifs/logo.gif";
$imginfo = getimagesize($remoteImage);
header("Content-type: {$imginfo['mime']}");
readfile($remoteImage);

The reason you should use readfile here is that it outputs the file directly to the output buffer where as file_get_contents will read the file into memory which is unnecessary in this content and potentially intensive for large files.

你应该在这里使用readfile的原因是它将文件直接输出到输出缓冲区,其中file_get_contents将文件读入内存,这在此内容中是不必要的,并且对于大文件可能是密集的。

#3


36  

$image = 'http://images.itracki.com/2011/06/favicon.png';
// Read image path, convert to base64 encoding
$imageData = base64_encode(file_get_contents($image));

// Format the image SRC:  data:{mime};base64,{data};
$src = 'data: '.mime_content_type($image).';base64,'.$imageData;

// Echo out a sample image
echo '<img src="' . $src . '">';

#4


9  

You can do that, or you can use the readfile function, which outputs it for you:

你可以这样做,或者你可以使用readfile函数,它为你输出:

header('Content-Type: image/x-png'); //or whatever
readfile('thefile.png');
die();

Edit: Derp, fixed obvious glaring typo.

编辑:Derp,修复了明显的明显错字。

#5


7  

you can do like this :

你可以这样做:

<?php
    $file = 'your_images.jpg';

    header('Content-Type: image/jpeg');
    header('Content-Length: ' . filesize($file));
    echo file_get_contents($file);
?>

#6


0  

Small edit to @seengee answer: In order to work, you need curly braces around the variable, otherwise you'll get an error.

小编辑@seengee回答:为了工作,你需要围绕变量的花括号,否则你会得到一个错误。

header("Content-type: {$imginfo['mime']}");

header(“Content-type:{$ imginfo ['mime']}”);

#1


29  

Do i need to modify the headers and just echo it or something?

我是否需要修改标题并回显它或什么?

exactly.

究竟。

Send a header("content-type: image/your_image_type"); and the data afterwards.

发送标题(“content-type:image / your_image_type”);以及之后的数据。

#2


56  

You can use readfile and output the image headers which you can get from getimagesize like this:

你可以使用readfile并输出你可以从getimagesize获得的图像标题,如下所示:

$remoteImage = "http://www.example.com/gifs/logo.gif";
$imginfo = getimagesize($remoteImage);
header("Content-type: {$imginfo['mime']}");
readfile($remoteImage);

The reason you should use readfile here is that it outputs the file directly to the output buffer where as file_get_contents will read the file into memory which is unnecessary in this content and potentially intensive for large files.

你应该在这里使用readfile的原因是它将文件直接输出到输出缓冲区,其中file_get_contents将文件读入内存,这在此内容中是不必要的,并且对于大文件可能是密集的。

#3


36  

$image = 'http://images.itracki.com/2011/06/favicon.png';
// Read image path, convert to base64 encoding
$imageData = base64_encode(file_get_contents($image));

// Format the image SRC:  data:{mime};base64,{data};
$src = 'data: '.mime_content_type($image).';base64,'.$imageData;

// Echo out a sample image
echo '<img src="' . $src . '">';

#4


9  

You can do that, or you can use the readfile function, which outputs it for you:

你可以这样做,或者你可以使用readfile函数,它为你输出:

header('Content-Type: image/x-png'); //or whatever
readfile('thefile.png');
die();

Edit: Derp, fixed obvious glaring typo.

编辑:Derp,修复了明显的明显错字。

#5


7  

you can do like this :

你可以这样做:

<?php
    $file = 'your_images.jpg';

    header('Content-Type: image/jpeg');
    header('Content-Length: ' . filesize($file));
    echo file_get_contents($file);
?>

#6


0  

Small edit to @seengee answer: In order to work, you need curly braces around the variable, otherwise you'll get an error.

小编辑@seengee回答:为了工作,你需要围绕变量的花括号,否则你会得到一个错误。

header("Content-type: {$imginfo['mime']}");

header(“Content-type:{$ imginfo ['mime']}”);