简体   繁体   中英

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

  • Yes, i need something to use into image tag

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

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.

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' />';

EDIT: I think I got the wrong end of the stick here, but oh well!

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);
?>

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);
?>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM