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..
.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.
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/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.
Share Improve this question asked Dec 15, 2017 at 4:52 Glenn ThorntonGlenn Thornton 12 silver badges5 bronze badges 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. – Jacob Peattie Commented Dec 15, 2017 at 5:50
- I want to base64 encode the url... I need to append that url to another service – Glenn Thornton Commented 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. – Jacob Peattie Commented Dec 15, 2017 at 6:13
- It is for a service name Cloudinary... Here is the url I am trying to create.. res.cloudinary/demo/image/upload/… – Glenn Thornton Commented Dec 15, 2017 at 20:50
- I am trying to add a users avatar to a background pic – Glenn Thornton Commented Dec 15, 2017 at 20:51
1 Answer
Reset to default 1In 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/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
.