Skip to content

Instantly share code, notes, and snippets.

@rheinardkorf
Last active February 2, 2022 01:17
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rheinardkorf/7e40e37e292e8a80d4fe87f2149e477a to your computer and use it in GitHub Desktop.
Quick and dirty GDImage to Base64 URL.
<?php
function image_to_base64( $image, $type = 'image/jpeg' ) {
// Use new buffer to grab the output.
ob_start();
switch ( $type ) {
case 'image/jpeg':
imagejpeg( $image );
break;
case 'image/png':
imagepng( $image );
break;
}
// Get buffer contents.
$image_data = ob_get_contents();
// Drop the buffer.
ob_end_clean();
if ( ! empty( $image_data ) ) {
return 'data:' . $type . ';base64,' . base64_encode( $image_data );
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment