Advertisement

How to Convert Image to base64 (php)


 

There are some plugins that can't accept images from url/directory, for that the image must be converted into a base64 text code.

Example of an img tag using base64.

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKEAAAA0CAYAAADi+lhGAAAABHNCSVQICAg;" />


 As for how to convert an image to base64, you can make the code with php.


<?php
$path = 'myimage.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);


//display encode
echo 'string base64: '.$base64;

//display encode to image
echo '<img src="'.$base64.'">';

Post a Comment

0 Comments