Reputation: 7768
How can I read a file from the images folder(permissions 644) on the server in php and set it dynamically to the img src of html ? I have tried the following
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$name = './images/approved.jpg';
//$fp = readfile($name);
$im = imagecreatefromjpeg($name);
echo "<img src='data:image/jpg;base64,+ 'base64_encode($im), alt='Approved Videos''>";
exit;
?>
I have also tried the following
<div id="videoThumbnails">
<p>Thumbnail</p>
<a href="#" class="thumbnail">
<!-- <img src="/phpVA/thumbnails/a.mp4.jpg" alt="Denied Videos">-->
<?php
$name="/phpVA/thumbnails/a.mp4.jpg";
$im = file_get_contents($name);
echo "<img src='data:image/jpg;base64,".base64_encode($im)."' alt='Approved Videos'>";
?>
</a>
</div>
Output is
<img src="data:image/jpg;base64," alt="Approved Videos">
Upvotes: 0
Views: 4902
Reputation: 614
I would recommend using file_get_contents
, to get the file contents, and encode these contents with base64. Here is a good example I used in one of my projects:
$path = './images/approved.jpg';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
Upvotes: 1
Reputation: 664
Try file_get_contents
$im = file_get_contents($name);
echo "<img src='data:image/jpg;base64,".base64_encode($im)."' alt='Approved Videos'>";
Upvotes: 2