使用Image header() & readfile()在PHP页面上显示一个Base64图像。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Image header() & readfile()在PHP页面上显示一个Base64图像。相关的知识,希望对你有一定的参考价值。

我正在做一个脚本,在对php函数进行一些操作后,我产生了一个 base64 图像数据 $base64Image 变量。现在我想表明 base64 图像数据在网络浏览器上以全图的形式出现,就像在浏览器上以全图的形式出现一样 image.jpeg. 为此,我写了一些代码,如下图所示......。

screenshot.php

<?php
/*------------------------
// Basic PHP Operations //
------------------------*/
// Get URL Parameters
$key1 = $_GET["key1"];
$key2 = $_GET["key2"];

// Some PHP Operations...........................................................................

// An Example base64 Image Else This Variable Will Be Filed After Some PHP Operations
$base64Image = "iVBORw0KGgoAAAANSUhEUgAAAJYAAABzCAYAAAB6iPvTAAAM4klEQVR4Xu2af6wU1RXHv2dm34KIv6IoELVaqFgtKGhVNFXaQDT+qEYFY9P6I6koVlpBK4/dmdnZmdl9SGmtptaCtZg2rcZXW9NGQOuPRqpYRcuPItbSEoECxapFqz7YnTnNHWZfluU93uJjwt7k3L/e27lz5tzv+cw99547BGmiQAoKUAo2xaQoAAFLIEhFAQErFVnFqIAlDKSigICViqxiVMASBlJRQMBKRVYxKmAJA6koIGClIqsYFbCEgVQUELBSkVWMCljCQCoKCFipyCpGBSxhIBUFBKxUZBWjApYwkIoCAlYqsopRAUsYSEUBASsVWcWogCUMpKKAgJWKrGJUwBIGUlFAwEpFVjEqYAkDqSggYKUiqxgVsISBVBQQsFKRVYwKWMJAKgoIWKnIKkYFLGEgFQUErFRkFaMCljCQigICViqyilEBSxhIRQEBKxVZxaiAJQykooCAlYqsYlTAEgZSUUDASkVWMSpgCQOpKCBgpSKrGBWwhIFUFBCwUpFVjApYwkAqCghYqcgqRgUsYSAVBQSsVGQVoy0BVkdHx0iuVp9gIlYhIWbq7e8I2GLZ9qRa6FzXPbzNMO6GYZzbfS/zsrzj3LS38BYKheOzbW1P1p7FzGwA9+dse379fSXfLxNwKQ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s+rp2cxMxWLRVPZeA7BgwYLev5TtxXk1Cw/bvDn+gHDL8OHcKl8zNLrbKmA1y4D000QBAUuTQOnmpoClW8Q08VfA0iRQurkpYOkWMU38FbA0CZRubgpYukVME38FLE0CpZubApZuEdPEXwFLk0Dp5qaApVvENPFXwNIkULq5KWDpFjFN/BWwNAmUbm4KWLpFTBN/BSxNAqWbmwKWbhHTxF8BS5NA6eamgKVbxDTxV8DSJFC6uSlg6RYxTfwVsDQJlG5uCli6RUwTfwUsTQKlm5sClm4R08RfAUuTQOnmpoClW8Q08VfA0iRQurkpYOkWMU38FbA0CZRubgpYukVME38FLE0CpZubApZuEdPEXwFLk0Dp5qaApVvENPFXwNIkULq5KWDpFjFN/BWwNAmUbm4KWLpFTBN/BSxNAqWbmwKWbhHTxF8BS5NA6eamgKVbxDTxV8DSJFC6uSlg6RYxTfwVsDQJlG5u/h8dgaq/bCkZugAAAABJRU5ErkJggg==";

// Convert base64 Image to Real Image
$imageFile = base64_decode($base64Image);

// Get Image filesize For Header
$filesize = filesize($imageFile);

/*-------------------------------------------------
// Image Output (Convert PHP File In Image File) //
-------------------------------------------------*/
header( 'Pragma: public' );
header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Cache-Control: private',false );
header( 'Content-Disposition: attachment; filename="screenshot.png"' ); // Default File Name Will Be "screenshot.png"
header( 'Content-Transfer-Encoding: binary' );
header('Content-Type: image/png');      // Becasue Base64 Image Is Of PNG Type Fixed
header( 'Content-Length: '.$filesize );
readfile( $imageFile );
?>

我试图在html文件中显示上图,如下图所示。

screenshot.html

<img src="screenshot.php?key1=value1&key2=value2" alt="screenshot"/>

经过这些尝试,我仍然不能直接看到我的图片在 .php 网页或 .html 页面。总之先谢谢了。

请注意。 我不想强迫下载图片。即使有人试图打开 screenshot.php浏览器页面上应该显示图片而不是下载。

答案

你不能在字符串上使用 filesize(),也不能使用 Content-Disposition: attachment header 强制下载。

把base64下的所有内容都改成。

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

我甚至不确定你是否需要这些头,当我测试时,我只用了Content -Type。

以上是关于使用Image header() & readfile()在PHP页面上显示一个Base64图像。的主要内容,如果未能解决你的问题,请参考以下文章

如何直接设置 <Image /> 组件的边距? [复制]

在 PHP 中输​​出图像

header_image()不显示在自定义主题中

PE文件结构体-IMAGE_SECTION_HEADER

rotate-image

PE文件结构体-IMAGE_FILE_HEADER