Skip to content

Instantly share code, notes, and snippets.

@ravahdati
Last active January 20, 2021 14:09
Show Gist options
  • Select an option

  • Save ravahdati/2149b3358dbf907e2fa4118c1485dd43 to your computer and use it in GitHub Desktop.

Select an option

Save ravahdati/2149b3358dbf907e2fa4118c1485dd43 to your computer and use it in GitHub Desktop.

Revisions

  1. ravahdati revised this gist Nov 13, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions convert-wp-image-to-base64.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?php
    function tidaweb_image_url_to_base64( $attach_id )
    {
    // get image src -> $image_info[0]
  2. ravahdati created this gist Nov 13, 2020.
    12 changes: 12 additions & 0 deletions convert-wp-image-to-base64.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    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 );
    }