Skip to content

Instantly share code, notes, and snippets.

@ravahdati
Last active January 20, 2021 14:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ravahdati/2149b3358dbf907e2fa4118c1485dd43 to your computer and use it in GitHub Desktop.
Save ravahdati/2149b3358dbf907e2fa4118c1485dd43 to your computer and use it in GitHub Desktop.
Convert WordPress image to base64 by attach_id
<?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