| AND OR | string words | *

image handling

|

base64 image encode


with this you can encode any image to store it in a database or similar


function encode_img( ) [ line 4 ]


function display_img( ) [ line 18 ]



full source of base64 image encode [ line 1 - 27 ] | download base64 image encode

1    <?php
2    // function to encode the image
3    // returns the image as base64 encoded string
4   function encode_img($img)
5   {
6       $fd fopen ($img'rb');
7       $size=filesize ($img);
8       $cont fread ($fd$size);
9       fclose ($fd);
10       $encimg base64_encode($cont);
11       return $encimg;
12   }
13    // use this to split the code into managable pieces
14    // if you want to save the string to a file
15   $imgcode chunk_split($encimg,20,''.
16   '');
17    // function to display the image
18   function display_img($imgcode,$type)
19   {
20       header('Content-type: image/'.$type);
21       header('Content-length: '.size($imgcode));
22       echo base64_decode($imgcode);
23   }
24    // use like
25   encode_img('path/to/image.gif'); // to encode the image
26   display_img($imgcode,'gif'); // to show the image
27   ?>



16 hits by 9 users in the last 30 minutes.