Last active
February 2, 2022 01:17
-
-
Save rheinardkorf/7e40e37e292e8a80d4fe87f2149e477a to your computer and use it in GitHub Desktop.
Quick and dirty GDImage to Base64 URL.
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 | |
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