-1

Possible Duplicate:
php: recreate and display an image from binary data

I want to display in my page the image I got from this code:

$contents = fread($handle, filesize($filename));

How do I display it?

Community
  • 1
  • 1
pheromix
  • 18,213
  • 29
  • 88
  • 158

2 Answers2

2

You have to print out image's content and set proper header depends on the filetype.

Example:

<?php

$contents = fread($handle, filesize($filename));

header('Content-Type: image/jpeg');
echo $contents;
hsz
  • 148,279
  • 62
  • 259
  • 315
0

If you already have a file that is outputting HTML, and within that page you would like to show an image, you need to use the <img> tag. You can call another file that does what the other poster suggested to output the contents. But the Content-Type you would output depends on the image MIME type. If it's a PNG file, then you would use a different header parameter:

<?php

$contents = fread($handle, filesize($filename));

header('Content-Type: image/png'); // or image/gif, depending on what $filename is.
echo $contents;

Your other option is to output this image inline using data URI:

Here is an example:

<?php
// take note that this is for PNG files - just change it to JPG or GIF depending on whatever your image is
$filename = 'image.png';
echo 'The following image is a PNG file... <img src="data:image/png;base64,'.base64_encode(file_get_contents($filename)).'" />';
?>
rationalboss
  • 5,330
  • 3
  • 30
  • 50