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);
?>

Share and Enjoy

  • Facebook
  • Twitter
  • Google Plus
  • Live
  • Delicious
  • Digg
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS
  • Instapaper
  • FriendFeed

Comments

  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.