how can I display an image retrieved using file_get_contents in php?
Do i need to modify the headers and just echo it or something?
Thanks!
The function returns the read data or false on failure. This function may return Boolean false , but may also return a non-Boolean value which evaluates to false .
The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string.
The file_get_contents() function reads a file into a string. The file_put_contents() function writes data to a file.
file_get_contents in itself appears safe, as it retrieves the URL and places it into a string. As long as you're not processing the string in any script engine or using is as any execution parameter you should be safe.
You can use readfile and output the image headers which you can get from getimagesize like this:
$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.
$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 . '">';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With