How to convert an image to base64 encoding?

前端 未结 9 1400
刺人心
刺人心 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:28

    Use also this way to represent image in base64 encode format... find PHP function file_get_content and next to use function base64_encode

    and get result to prepare str as data:" . file_mime_type . " base64_encoded string. Use it in img src attribute. see following code can I help for you.

    // A few settings
    $img_file = 'raju.jpg';
    
    // Read image path, convert to base64 encoding
    $imgData = base64_encode(file_get_contents($img_file));
    
    // Format the image SRC:  data:{mime};base64,{data};
    $src = 'data: '.mime_content_type($img_file).';base64,'.$imgData;
    
    // Echo out a sample image
    echo '';
    

提交回复
热议问题