top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to convert an jpg image to base64 with php in Wordpress?

0 votes

I am trying to convert an jpg image on my Wordpress blog to base64. I use this php code but 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 image is being retrieved, just can't get it working. Anyone know what might be going on.

posted Aug 3, 2017 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Use this code 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));
answer Aug 21, 2017 by Deepa
Similar Questions
0 votes

I want to parse xml for getting element with same attribute and display it into my wordpress post using shortcode by php.
I create a xml file called emp_test.xml

<?xml version="1.0" encoding="utf-8"?>
    <emp emp_name="john"><img>john_1.jpg</img></emp>
    <emp emp_name="john"><img>john_2.jpg</img></emp>
    <emp emp_name="john"><img>john_3.jpg</img></emp>
    <emp emp_name="marry"><img>marry_1.jpg</img></emp>
    <emp emp_name="marry"><img>marry_2.jpg</img></emp>
    <emp emp_name="david"><img>david_1.jpg</img></emp>

I create shortcode in functions.php to get all img has attribute is john:

function create_shortcode_empimg() {
$url = 'https://.../emp_test.xml';
$xml = simplexml_load_file("$url") or die("Error: Cannot create object");
foreach ($xml->xpath("//*[@emp_name='john']/img") as $node)
    $img = (string) $node;
    return $img;
add_shortcode( 'empimg_shortcode', 'create_shortcode_empimg' );

I use this shortcode into my wordpress post.

But, the reult is comming like this:


How to get all img has attribute is john like?


Anyone's help is appreciated.

0 votes

In my wordpress site I have a picture in header section. On a full screen it looks ok but on mobile and when minimized the picture is on the left of the page. I would like it to be on the right. I thought that maybe I had to use CSS to reposition it so I did the following:
.widget-header img { float: left; } to .widget-header img { float: right; }
in hopes that it would work. However nothing seems to have changed. Can anybody help me how I fix this problem?

+2 votes

I have moderate level PHP programming skills. I am also interested in merging those skills inside of the WordPress app. With their advanced responsive design themes and many other functions it seems like a good idea. I have tried loading some PHP plug-ins in WP – but I don't seem to get them to do more than basics.

Can anybody help me on how I can get started working with PHP/MySQL inside of the WordPress app?

0 votes

I am trying to export certain data from my PHP form to CSV. I can echo out to screen during testing and I can also export to CSV the static test data (stored in the $contents array) you see below. But I am stuck trying to export the certain fields that I only need to export.
This is my code

// How do I get this info into the CSV?
/*foreach ( $entries as $entry ) :  
    echo $entry['2'];
    echo $entry['3'];
    echo $entry['6'];

$csv_headers = [
    'Organisation Name',
    'Registered Charity Number',

$contents = [
  [2014, 6, '1st half', '', 0, 0],
  [2014, 6, '1st half', '', 0, 0],
  [2014, 6, '1st half', '', 0, 0],
  [2014, 6, '1st half', 'tim', 0, 0]

fputcsv($output_handle, $csv_headers);

foreach ( $contents as $content) :
    fputcsv($output_handle, $content);
0 votes

I need to add a variable inside an array of an embedding gravity forms to populate 2 hidden fields: email and jobname.

Here is my code:


    $email = get_field( "email_application" );
    $jobtitle = get_the_title();

    gravity_form( 5, false, false, false, array('email'=>'echo "$email";','jobname'=>'print "$jobtitle";'), false); 

I know that echo, print or just the variable doesn't work as it show like this in my code:

<input name="input_6" id="input_5_6" type="hidden" value="print $jobtitle" class="gform_hidden" aria-invalid="false">


<input name="input_5" id="input_5_5" type="hidden" value="echo "$email";" class="gform_hidden" aria-invalid="false">

Instead I should get:

<input name="input_6" id="input_5_6" type="hidden" value="My job title" class="gform_hidden" aria-invalid="false">


<input name="input_5" id="input_5_5" type="hidden" value="" class="gform_hidden" aria-invalid="false">

Can someone could point me in the right direction.

Thank you.