Last active
February 24, 2022 10:30
-
-
Save LucaRosaldi/5676858 to your computer and use it in GitHub Desktop.
PHP: Get data URI string from an image file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get data URI string from an image file. | |
* | |
* @param string $path_to_file | |
* @return string | |
*/ | |
function get_data_uri( string $path_to_file ) : string | |
{ | |
$contents = file_get_contents( $path_to_file ); | |
$base64 = base64_encode( $contents ); | |
$imagetype = exif_imagetype( $file ); | |
$mime = image_type_to_mime_type( $imagetype ); | |
return "data:$mime;base64,$base64"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment