Reputation: 726
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" />
Upvotes: 2
Views: 625
Reputation: 530
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" />
Upvotes: 0
Reputation: 46728
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
Upvotes: 1