2

I am attempting to read an image file, as a base64 encoded string and output a data string when the connection uses HTTPS/SSL, and otherwise put out the URL in the IMG src attribute if it is only on HTTP. Here is my current code, however it does not work.

<?php 
function base64_encode_image($filename, $filetype) {
    if (($_SERVER["HTTPS"] == "on") && $filename) {
        $file = "/home/content/61/9295861/html/resource/image$filename";
        $imgbinary = fread(fopen($file, "r"), filesize($file));
        return "data:image/$filetype;base64," . base64_encode($imgbinary);
    } else {
        return $filename;
    }
}
?>
<img src="<?php echo base64_encode_image('/resource/image/logo-96x72.png', 'png'); ?>" width="96" height="72" />

It outputs:

<img src="<br />
<b>Warning</b>:  fopen(/home/content/61/9295861/html/resource/image/resource/image/logo-96x72.png) [<a href='function.fopen'>function.fopen</a>]: failed to open stream: No such file or directory in <b>/home/content/61/9295861/html/theme/latest/index.php</b> on line <b>5</b><br />
<br />
<b>Warning</b>:  filesize() [<a href='function.filesize'>function.filesize</a>]: stat failed for /home/content/61/9295861/html/resource/image/resource/image/logo-96x72.png in <b>/home/content/61/9295861/html/theme/latest/index.php</b> on line <b>5</b><br />
<br />
<b>Warning</b>:  fread() expects parameter 1 to be resource, boolean given in <b>/home/content/61/9295861/html/theme/latest/index.php</b> on line <b>5</b><br />
data:image/png;base64," width="96" height="72" />
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Solo
  • 726
  • 1
  • 9
  • 18

2 Answers2

1

You have added the /image/resource directory twice to your URL which is resulting in a file not found error.

$file = "/home/content/61/9295861/html/resource/image$filename";
$filename = "/resource/image/logo-96x72.png"

So your File URL is /home/content/61/9295861/html/resource/image/resource/image/logo-96x72.png

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
0

Change

<img src="<?php echo base64_encode_image('/resource/image/logo-96x72.png', 'png'); ?>" width="96" height="72" />

to

<img src="<?php echo base64_encode_image('logo-96x72.png', 'png'); ?>" width="96" height="72" />
pho
  • 520
  • 3
  • 9