Help With BASE64 Encode Images?

Discussion in 'PHP' started by CLMMafia, Apr 6, 2012.

  1. #1
    Hello,

    I need a simple php code where you uploaded and image and it will convert it to a base64 string, and also echo the mimetype.

    I realise this will be easy for some, but I've never really done much with uploading images.

    Thanks,
    CLM
     
    Solved! View solution.
    CLMMafia, Apr 6, 2012 IP
  2. #2
    How about:

    
    <?php
        function base64_encode_image ($imagefile) {
            $imgtype = array('jpg', 'gif', 'png');
            $filename = file_exists($imagefile) ? htmlentities($imagefile) : die('Image file name does not exist');
            $filetype = pathinfo($filename, PATHINFO_EXTENSION);
            if (in_array($filetype, $imgtype)){
                $imgbinary = fread(fopen($filename, "r"), filesize($filename));
            } else {
                die ('Invalid image type, jpg, gif, and png is only allowed');
            }
            return 'data:image/' . $filetype . ';base64,' . base64_encode($imgbinary);
        }
    
    ?>
    
    PHP:
    source


    might do the trick.





    ROOFIS
     
    ROOFIS, Apr 6, 2012 IP