Skip to content

Instantly share code, notes, and snippets.

@rheinardkorf
Last active February 2, 2022 01:17
Embed
What would you like to do?
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