Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to convert Image into base64 encoding using PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 108
    Comment on it

    Hello Reader's! If you want to to encode an image with base64 then by using PHP you can do such. You can use the code as below:-

    This method is very simple and 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));
    }
    

    Now you have declared the function and you can use the above function like below:

    echo '<img src="'.getDataURI('./images/my-file.svg').'" alt="">';
    echo '<img src="'.getDataURI('./images/my-file.png').'" alt="">';
    

    Note: The Mime-Type of the file will be added automatically.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: