Bel*_*ish 40 php file-get-contents http-headers
如何在php中显示使用file_get_contents检索的图像?
我需要修改标题,只是回应它或什么?
谢谢!
rob*_*lls 66
你可以使用readfile并输出你可以从getimagesize获得的图像标题,如下所示:
$remoteImage = "http://www.example.com/gifs/logo.gif";
$imginfo = getimagesize($remoteImage);
header("Content-type: {$imginfo['mime']}");
readfile($remoteImage);
Run Code Online (Sandbox Code Playgroud)
你应该在这里使用readfile的原因是它将文件直接输出到输出缓冲区,其中file_get_contents将文件读入内存,这在此内容中是不必要的,并且对于大文件可能是密集的.
小智 47
$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 . '">';
Run Code Online (Sandbox Code Playgroud)
Pek*_*ica 30
我需要修改标题,只是回应它或什么?
究竟.
之后发送header("content-type: image/your_image_type");和数据.
您可以这样做,或者您可以使用该readfile功能,为您输出:
header('Content-Type: image/x-png'); //or whatever
readfile('thefile.png');
die();
Run Code Online (Sandbox Code Playgroud)
编辑:Derp,修复了明显的明显错字.
你可以这样做:
<?php
$file = 'your_images.jpg';
header('Content-Type: image/jpeg');
header('Content-Length: ' . filesize($file));
echo file_get_contents($file);
?>
Run Code Online (Sandbox Code Playgroud)