PHP:如何从磁盘输出png图像到浏览器?

ou6hu8tu  于 2023-04-19  发布在  PHP
关注(0)|答案(5)|浏览(117)

我有一个文件在磁盘上的一个文件夹中,我不想从Web浏览器可见
所以我创建了一个image.php文件,它需要一个文件名,并且必须在图像的浏览器中“做echo”。
示例:
mysite.com/image.php?name=selection.png
必须在浏览器中向用户显示图像。
实际上浏览器要求我保存。我想显示它,而不是要求下载...
我正在使用这个片段

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

如果保存并打开,img是完美的.如何不要求下载它?

编辑

  • 是的,我需要一些东西来使用到图像标签
  • 你们大多数人都在回答我的问题,告诉我关于图像创建,但我的问题是所有关于不同的模式告诉浏览器显示的东西,并要求用户保存的东西.没有更多的关于图像创建的回复-你偏离主题!*
  • 可能我的问题,我的标题,不清楚 *,但我的问题很简单:
    -如何告诉浏览器显示图像,而不是要求用户保存它

图像创建正在工作
通过imag src的图像使用正在工作

**但是当用户点击图片时,会打开一个新的标签页,其中包含图像源作为链接,因为我需要在浏览器中显示全尺寸图像。在这种情况下,浏览器要求我保存!

编辑:
移除

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

不会改变Firefox的行为

ukxgm1gy

ukxgm1gy1#

尝试:

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

如果您使用的是用户输入,请非常小心使用passthru命令,因为此函数将执行您传递给它的任何内容。
https://www.php.net/manual/en/function.passthru.php
允许将用户提供的数据传递给此函数时,请使用escapeshellarg()或escapeshellcmd()确保用户无法欺骗系统执行任意命令。

cngwdvgl

cngwdvgl2#

您可以使用PHP的GD库。
in your image.php.

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

q5iwbnjs3#

要做到这一点,你必须对图像进行base64编码,然后输出结果。你可以使用这样的函数:

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

然后输出结果作为图像的源,如:

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

kyks70gy4#

你也可以使用PHP的imagepng
imagepng -将PNG图像输出到浏览器或文件

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

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

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

dl5txlt95#

如果你的图片路径存储在数据库中,这个方法是可以的。(我用MySQLi做了个例子,让它适应你的数据库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>";

如果您使用URL参数选择图像,则上述方法是最佳方法
否则:

$Picture = $_Get['Name'];

$Image = imagecreatefrompng($Picture);

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

imagepng($Image);

可能是解决办法
手册在这里:
http://php.net/manual/en/function.imagepng.php

相关问题