file_get_contents displaying image incorrectly

…衆ロ難τιáo~ 提交于 2019-12-04 14:38:34

Try this:

<?php
  $img = 'http://images.itracki.com/2011/06/favicon.png';
  $info = getimagesize($img);
  header('Content-type: ' . $info['mime']);
  readfile($img);
?>

You should use readfile() instead of file_get_contents() in this situation because readfile() will send the file contents directly to the browser rather than storing it in memory for a later retrieval, which is what file_get_contents() does.

Also note that the content-type is retrieved dynamically so that you don't have to worry about specifying it incorrectly in the header.

Some images may have an extension that doesn't match its content-type or mime-type, so this corrects for those type of issues.

try this one

$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,'">';
echo 'data:;base64,'.base64_encode(file_get_contents('http://images.itracki.com/2011/06/favicon.png'));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!