1

I'm trying to convert an image on my wordpress blog to base64.

When I use this website to do it manually it works.

But when I try it in PHp it's not working:

$thePostThumb = get_the_post_thumbnail($postId, array(150,150));
background-image: url(\'data:image/jpg;base64,' . base64_encode($thePostThumb) . '\');

I have also tried this method:

$type = pathinfo(get_the_post_thumbnail_url($postId, array(150,150)), PATHINFO_EXTENSION);
$data = file_get_contents(get_the_post_thumbnail_url($postId, array(150,150));
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data)

I know the thumb img is being retrieved, just can't get it working. Anyone know what might be going on. I'm using xampp and developing on local machine

Web Dev Guy
  • 1,693
  • 3
  • 18
  • 42
  • once try to provide hard-coded path in second code here:- `$data = file_get_contents(get_the_post_thumbnail_url($postId, array(150,150));` and then check it's working or not? May be folder permission restricts to get the image using file_get_contents (check folder have 077 permission or not) – Alive to die - Anant Aug 03 '17 at 06:13
  • 1
    Possible duplicates of:- https://stackoverflow.com/a/13758760/4248328 – Alive to die - Anant Aug 03 '17 at 06:14
  • @AlivetoDie Hi thanks for help, I'm using xampp and developing on local machine – Web Dev Guy Aug 03 '17 at 06:14
  • what i said can you do that. also trun your debug mode on to see the possible errors. it's in `config.php`, trun debug mode on while doing what i suggested – Alive to die - Anant Aug 03 '17 at 06:15

1 Answers1

2

This must be work, just tested locally:

$image = get_the_post_thumbnail_url($postID, array(150,150));
$ext = pathinfo($image, PATHINFO_EXTENSION);
echo 'data:image/' . $ext . ';base64,' . base64_encode(file_get_contents($image));
justkidding96
  • 498
  • 3
  • 10