How to convert an image to base64 encoding?

前端 未结 9 1395
刺人心
刺人心 2020-11-22 06:01

Can you please guide me how can I convert an image from a URL to base64 encoding?

9条回答
  •  佛祖请我去吃肉
    2020-11-22 06:27

    Very simple and to be commonly used:

    function getDataURI($imagePath) {
        $finfo = new finfo(FILEINFO_MIME_TYPE);
        $type = $finfo->file($imagePath);
        return 'data:'.$type.';base64,'.base64_encode(file_get_contents($imagePath));
    }
    
    //Use the above function like below:
    echo '';
    echo '';
    

    Note: The Mime-Type of the file will be added automatically (taking help from this PHP documentation).

提交回复
热议问题