communication
 databases
 directories
 file handling
 html and code
 image editing
 image listing
 sessions and co
 string handling
 information
 forms
 apple stuff
 image handling
 array handling
 lixlpixel
php - snippets
google search terms detected and highlighted.
remove highlighting | keep highlighting
make snippet in "image handling" - download "base64 image encode"
 search
php - snippets

Valid CSS!

php webring logo
prev next
random list
join the Ring!

image handling - base64 image encode was created/last modified on Thu 08 Apr 2004 11:56 PM

with this you can encode any image to store it in a database or similar
   
<?php
  
// function to encode the image
  // returns the image as base64 encoded string
function encode_img($img)
{
     
$fd = fopen ($img, 'rb');
     
$size=filesize ($img);
     
$cont = fread ($fd, $size);
     
fclose ($fd);
     
$encimg = base64_encode($cont);
     return
$encimg;
}
  
  
// use this to split the code into managable pieces
  // if you want to save the string to a file
$imgcode = chunk_split($encimg,20,'\'.
\''
);
   
  
// function to display the image
function display_img($imgcode,$type)
{
     
header('Content-type: image/'.$type);
     
header('Content-length: '.size($imgcode));
     echo
base64_decode($imgcode);
}
  
  
// use like
encode_img('path/to/image.gif'); // to encode the image
display_img($imgcode,'gif'); // to show the image
   
?>

learn more about some of the PHP functions used in this snippet: base64_decode, base64_encode, chunk_split, echo, fclose, file, filesize, fopen, fread, function, header, return, save, split

view the GNU general license - view the GNU library license - search on PHPsnippets - see your code like this

 a lixlpixel.com site

PHParadise.com - PHP-classes.org - your site on safari