realtebo
realtebo

Reputation: 25699

PHP: how to output to browser a png image from disk?

I've a file on a folder on disk I don't want to be visible from web browser

So i create an image.php file that take a file name and must 'do echo' to browser of the image.

Example:

mysite.com/image.php?name=selection.png

must show an image to the user into the browser.

Actually browser ask me to save. I want to show it, not do ask to download...

i'm using this snippet

header("Content-Type: image/png");
header('Content-Length: ' . filesize($full_file_name));
header("Content-Disposition: inline; filename='$image'");
readfile($full_file_name);
exit(0);

if saved and opened, img is perfect. How to NOT ask to download it?

EDIT

Most of you are replying to my question telling me about image creation, but my question is all about different mode to tell to browser to show something and to ask to user to save something. NO MORE REPLY ABOUT IMAGE CREATION - YOU'RE OFF-TOPIC !

Probably my question, my title, is not clear, but my question is simple:

- How to tell to browser to SHOW an image insted to ask user to save it

Image creation is working

Image usage via imag src is working

**But when user clicks on image, a new tab is opened with the images src as link, because I need to show full size image into browser. In this situation browser ask me to save !

EDIT:

Removing

header("Content-Disposition: inline; filename='$image'");

doesn't change Firefox behaviour

Upvotes: 1

Views: 9552

Answers (5)

Seer
Seer

Reputation: 5237

To do this, you'd have to base64 encode the image, then output the result. You could use a function like this:

<?php

/**
 * Base64 encodes an image..
 *
 * @param [string] $filename [The path to the image on disk]
 * @param [string] $filetype [The image file type, (jpeg, png, gif, ...)]
 */
function base64_encode_image($filename, $filetype) 
{
    if ($filename) 
    {
        $imgBinary = fread(fopen($filename, "r"), filesize($filename));

        return 'data:image/' . $filetype . ';base64,' . base64_encode($imgBinary);
    }
}

And then output the result as the source of the image, like:

$image = base64_encode_image('...name of file', '... file type');
echo "<img src='$image' />';

Upvotes: 2

splash21
splash21

Reputation: 809

try:

header("Content-type: image/png");
passthru("cat $full_file_name");

Be very careful with the passthru command if you're working with user input, since this function will execute anything you pass it.

https://www.php.net/manual/en/function.passthru.php

When allowing user-supplied data to be passed to this function, use escapeshellarg() or escapeshellcmd() to ensure that users cannot trick the system into executing arbitrary commands.

Upvotes: 3

Daryl Gill
Daryl Gill

Reputation: 5524

If your picture paths are stored in a database this method is okay. (I have exampled using MySQLi, adapt this to your Database API)

CREATE TABLE IF NOT EXISTS `PicturePath` (
  `ID` int(255) NOT NULL AUTO_INCREMENT,
  `Path` varchar(255) NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=0;

    --
INSERT INTO `PicturePath` (`ID`, `Path``) VALUES
(1, 'img/picture.png')
//End DB
// Start script


    $PictureID = $_GET['Name'];

    $Get_Picture = $Conn->prepare("SELECT PicturePath FROM pictures WHERE ID=?");
    $Get_Picture->bind_param('i', $PictureID);
    $Get_Picture->execute();
    $Get_Picture->bind_result($Path);
    $Get_Picture->close();
    echo "<img src='$Path'></img>";

The above method is best if you are selecting images using URL Parameters

Otherwise:

$Picture = $_Get['Name'];

$Image = imagecreatefrompng($Picture);

header('Content-Type: image/png');

imagepng($Image);

might be the solution

Manual here:

http://php.net/manual/en/function.imagepng.php

Upvotes: 0

ShuklaSannidhya
ShuklaSannidhya

Reputation: 8986

You can use PHP's GD library.

in your image.php.

<?php
    header('Content-type: image/png');
    $file_name = $_GET['name'];
    $gd = imagecreatefrompng($file_name);
    imagepng($gd);
    imagedestroy($gd);
?>

Upvotes: 2

cara
cara

Reputation: 1042

You could alternatively use PHP's imagepng:

imagepng — Output a PNG image to either the browser or a file

<?php
$im = imagecreatefrompng("test.png");

header('Content-Type: image/png');

imagepng($im);
imagedestroy($im);
?>

Upvotes: 1

Related Questions