Php, Display Image with Header()

PHP, display image with Header()

The best solution would be to read in the file, then decide which kind of image it is and send out the appropriate header

$filename = basename($file);
$file_extension = strtolower(substr(strrchr($filename,"."),1));

switch( $file_extension ) {
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpeg"; break;
case "svg": $ctype="image/svg+xml"; break;
default:
}

header('Content-type: ' . $ctype);

(Note: the correct content-type for JPG files is image/jpeg)

Output an Image in PHP

$file = '../image.jpg';
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);

Show A Base64 Image On PHP Page Using Image header() & readfile()

You can't use filesize() on a string, also the Content-Disposition: attachment header forces a download.

Change everything under the base64 to:

header( 'Pragma: public' );
header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Cache-Control: private',false );
header( 'Content-Transfer-Encoding: binary' );
header('Content-Type: image/png'); // Becasue Base64 Image Is Of PNG Type Fixed
echo $imageFile

I'm not even sure you need most of those headers, when I tested it I just used Content-Type.

Proper header php mysql blob display image

header("Content-type: image/png");

This tells your browser that you're about to pass it raw binary data that is a PNG file. So anything output after that would have to be a binary PNG. You can't then place HTML and expect that to work

echo '<img src="data:image/png;base64,' . base64_encode( $row['image1'] ) . '" />';

This works because you're base64 encoding your image (translates binary into text), outputting that to the browser and then telling your browser to interpret it as base64.

If you want to output the raw binary data you have to rearrange the order. So here's your HTML

<img src="image.php" />

Now you'll note the src points to a PHP file. That's because we're going to have that PHP file return an image. Here's what image.php would look like

//Your query here
$row = mysqli_fetch_assoc($result);
header("Content-type: image/png");
echo $row['image1'];

This works because the browser will call the PHP file, expecting an image. The header tells the browser that this is a PNG file and you can now dump your binary PNG data.

Return a PHP page as an image

The PHP Manual has this example:

<?php
// open the file in a binary mode
$name = './img/ok.png';
$fp = fopen($name, 'rb');

// send the right headers
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));

// dump the picture and stop the script
fpassthru($fp);
exit;
?>

The important points is that you must send a Content-Type header. Also, you must be careful not include any extra white space (like newlines) in your file before or after the <?php ... ?> tags.

As suggested in the comments, you can avoid the danger of extra white space at the end of your script by omitting the ?> tag:

<?php
$name = './img/ok.png';
$fp = fopen($name, 'rb');

header("Content-Type: image/png");
header("Content-Length: " . filesize($name));

fpassthru($fp);

You still need to carefully avoid white space at the top of the script. One particularly tricky form of white space is a UTF-8 BOM. To avoid that, make sure to save your script as "ANSI" (Notepad) or "ASCII" or "UTF-8 without signature" (Emacs) or similar.



Related Topics



Leave a reply



Submit