Following example explains how to add encoded url for image
1) Create a function to get the path in block class
public function getEncodedPath()
{
$ext = pathinfo($this->getPath(), PATHINFO_EXTENSION);
if ($ext) {
$ext = strtolower($ext);
}
$imageUrl = $this->getBaseUrl() . $this->getPath();
$imageData = base64_encode(file_get_contents($imageUrl));
switch ($ext) {
case "jpeg":
return 'data: image/jpeg;base64,'.$imageData;
case "jpg":
return 'data: image/jpg;base64,'.$imageData;
case "png":
return 'data: image/png;base64,'.$imageData;
default:
return '';
}
}
2) Call the path function in phtml
<img src="<?= echo $block->getEncodedPath() ?>" />
Posted by vasan to vasan's deck (2021-02-20 15:07)