01 <?php
02
03 // function to encode the image
04 // returns the image as base64 encoded string
05 function encode_img($img)
06 {
07 $fd = fopen($img, 'rb');
08 $size = filesize($img);
09 $cont = fread($fd, $size);
10 fclose($fd);
11 $encimg = base64_encode($cont);
12 return $encimg;
13 }
14
15 // function to display the image
16 function display_img($imgcode,$type)
17 {
18 header('Content-type: image/'.$type);
19 header('Content-length: '.strlen($imgcode));
20 echo base64_decode($imgcode);
21 }
22
23 // use like
24 // specify the image to encode
25 $img = 'path/to/file.gif';
26
27 // encode the image
28 $encoded_img = encode_img($img);
29
30 // use this to split the code into managable pieces
31 // i.e. if you want to save the string to a file
32 $imgcodetosave = '\''.chunk_split($encoded_img,20,'\'.
33 \'').'\'';
34
35 // show the image directly
36 display_img($imgcode,'gif');
37
38 ?>