Skip to content

Instantly share code, notes, and snippets.

@LucaRosaldi
Last active February 24, 2022 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LucaRosaldi/5676858 to your computer and use it in GitHub Desktop.
Save LucaRosaldi/5676858 to your computer and use it in GitHub Desktop.
PHP: Get data URI string from an image file.
<?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