1

I would like to get a hold of the feature image to perform image manipulation on it.

First I want to load it, then manipulate it and turn the final image into base64 for displaying it in the HTML.

Any idea how can I get a hold of the image?

1

You can write a function to grab the featured image by post's ID, then manipulate it and return it as whatever format you wish.

function covert_thumbnail_to_base64($post->ID){
    // Get the thumbnail's URL
    $thumbnail = get_the_post_thumbnail_url( $post->ID,'thumbnail' );

    // Now, process it the way you want

    // Return the processed image
    return $data;
}

Now you can convert the post's thumbnail to base64 by calling covert_thumbnail_to_base64();. if you use this inside a loop, you don't have to pass the post's ID to the function. Otherwise, you should feed the function with a post ID.

If you need the absolute path to the image, you can use get_attached_file() in conjunction with get_post_thumbnail_id():

$file_path = get_attached_file( get_post_thumbnail_id($post->ID));
| improve this answer | |
  • How can I get the thumbnail in a specific size? – Yaron Jul 2 '17 at 10:58
  • 1
    @Yaron Hi. You can change the thumbnail in the answer to any size you want. – Jack Johansson Jul 2 '17 at 11:27
  • But this is a url, not a gd image – Yaron Jul 2 '17 at 11:32
  • @Yaron If you want to get the absolute path to the other sizes, you have to build it. Check this question (also the one in the first comment). – Jack Johansson Jul 2 '17 at 11:33
  • 1
    Oh, It was my bad! I eventually used the returning url from get_the_post_thumbnail_url with imagecreatefromjpeg and it worked like a charm! Thank you so much! – Yaron Jul 2 '17 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.