Base64 Data String To Image And Vice-Versa


<?php
function tobase64($image_path){
$file_open = fopen ($image_path, ‘rb’);
$file_size = filesize($image_path);
$read = fread ($file_open, $file_size);
fclose ($file_open);
$encode = base64_encode($read);
return $encode;
}

function toImage($base_code){
$img_file = imagecreatefromstring(base64_decode($base_code));
imagejpeg($img_file, ‘new.jpg’);
}
$encoded_image = tobase64(‘Chrysanthemum.jpg’);
echo ‘<img src=”data:image/jpeg;base64,’.$encoded_image.'” / >’;

echo toImage($encoded_image);
?>

Advertisements

2 thoughts on “Base64 Data String To Image And Vice-Versa

  1. It is in reality a nice and useful piece of information. I am satisfied that you simply shared this helpful information with us. Please stay us up to date like this. Thank you for sharing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s