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

六眼飞鱼酱① 提交于 2021-02-19 08:27:26

问题


I am working on a script where after some operation of PHP functions, I generate a base64 image data in $base64Image variable. Now I want to show that base64 image data as a full image on the web browser same as an image.jpeg. For this purpose, I wrote some codes as shown below...

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+dse379fSXfLxNwKQPDARwBwEiuRwRsY6JlIFL3rMjlctuIiCdPnmyeftppjwMYsbex9DRGZl5pmOZ3KIquiZingeIQdRHz3Tnb/nVvYyp7XgeILlM2mfk9k/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U38FbA0CZRubgpYukVME38FLE0CpZubApZuEdPEXwFLk0Dp5qaApVvENPFXwNIkULq5KWDpFjFN/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 );
?>

And I am trying to display the upper image in an HTML file as shown below.

screenshot.html

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

After all of these tries, I am still not able to to see my image directly in .php the page or in .html page. Anyway thanks in advance.

Note: I do not want to force the downloading of images. Even if someone tries to open screenshot.php, it should show the image on browser page not to download.


回答1:


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.



来源:https://stackoverflow.com/questions/62047610/show-a-base64-image-on-php-page-using-image-header-readfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!