0

I need to convert an image url (wordpress avatar) to a base_64 encode data using a wordpress function . I have a shortcode that has the image url for all wordpress users...

For example... [user_meta_avatar_pic] ; which will show an image like below..

https://upload.wikimedia.org/wikipedia/commons/7/79/2010-brown-bear.jpg

I need [user_meta_avatar_pic] shortcode to be converted to a base64_encode data

Base64_encode for image above = aHR0cHM6Ly91cGxvYWQud2lraW1lZGlhLm9yZy93aWtpcGVkaWEvY29tbW9ucy83Lzc5LzIwMTAtYnJvd24tYmVhci5qcGc=

I need this base64_encode url for a services I am using with a SAAS imaging product.

6
  • 1
    Do you want to base64 encode the image or the URL? That looks like the base64 encode of the URL, but I'm not sure what use that is. Dec 15, 2017 at 5:50
  • I want to base64 encode the url... I need to append that url to another service Dec 15, 2017 at 6:01
  • What service? I can understand why a service might need the image itself base64 encoded, but I can't understand why the URL itself would need to be encoded. Dec 15, 2017 at 6:13
  • It is for a service name Cloudinary... Here is the url I am trying to create.. res.cloudinary.com/demo/image/upload/… Dec 15, 2017 at 20:50
  • I am trying to add a users avatar to a background pic Dec 15, 2017 at 20:51

1 Answer 1

1

In your post you wrote : "which will show an image like below", I assume that your shortcode return an img tag and you have to find your URL here, tell me if I'm wrong.

Using DOMDocument http://php.net/manual/fr/class.domdocument.php to parse your img tag is a good idea.

$img = do_shortcode('[user_meta_avatar_pic]'); // Here your get your img tag

// Use DOMDocument to parse your img tag
$dom = new DOMDocument();
$dom->loadHTML($img);
$img_url = base64_encode($dom->getElementsByTagName('img')->item(0)->getAttribute('src')); // Encode the extracted src from your img tag

You can also use regex, I have no idea about performances comparison beetween a regex and DOMDocument.

2
  • The shortcode just shows the url of the image... Not exactly the pic.... It could be used for an image tag like <img src="[user_meta_avatar_pic]" alt="Mountain View"> Dec 15, 2017 at 20:54
  • Ok ! base64_decode(do_shortcode('[user_meta_avatar_pic]'));
    – Elex
    Dec 16, 2017 at 7:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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