Last active
January 20, 2021 14:09
-
-
Save ravahdati/2149b3358dbf907e2fa4118c1485dd43 to your computer and use it in GitHub Desktop.
Convert WordPress image to base64 by attach_id
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 tidaweb_image_url_to_base64( $attach_id ) | |
{ | |
// get image src -> $image_info[0] | |
$image_info = wp_get_attachment_image_src( $attach_id, 'full' ); | |
$image_file = file_get_contents( $image_info[0] ); | |
// get filename from url | |
$filename = basename( get_attached_file( $attach_id ) ); | |
$image_file_type = wp_check_filetype( $filename ); | |
return 'data:image/'.$image_file_type['ext'].';base64,'.base64_encode( $image_file ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment