This is the code I have and I am trying to get and encode the image contents as base64 but I keep ending up with the URL as a base64 string.
In the end I get images as an array from a API I need to transcode them to Base64 to store in a local DB.
This is based on the Gravity Forms API, Wordpress, PHP, mySQL, (LAMP) etc.
<?php
$images = array();
$body = array();
$imagesDecoded = array();
$imgUrls = array(
'1' => 'bg.jpg',
'2' => 'meeting.jpg',
'3' => 'testimonial.jpg',
'4' => 'works.jpg',
);
$imgUrls = array_map(function($el) {
return 'http://orlandojoes.co.uk/rimos/images/' . $el;
}, $imgUrls);
print'<pre>';
print_r($imgUrls);
print'</pre>';
foreach ($imgUrls as $image) {
$data = file_get_contents($imgUrls);
$data = base64_encode($imgUrls);
array_push($body, $data);
}
print '<pre>';
print_r ($body);
print '<pre>';
foreach ($body as $bodyimage) {
$dataDec = base64_decode($bodyimage);
array_push($imagesDecoded, $dataDec);
}
print '<pre>';
print_r ($imagesDecoded);
print '<pre>';
This is the output from when I run this code now:
Array
(
[ptSignature] => http://orlandojoes.co.uk/rimos/images/bg.jpg
[pSignature] => http://orlandojoes.co.uk/rimos/images/meeting.jpg
[witness1Signature] => http://orlandojoes.co.uk/rimos/images/testimonial.jpg
[witness2Signature] => http://orlandojoes.co.uk/rimos/images/works.jpg
)
Array
(
[0] => aHR0cDovL29ybGFuZG9qb2VzLmNvLnVrL3JpbW9zL2ltYWdlcy9iZy5qcGc=
[1] => aHR0cDovL29ybGFuZG9qb2VzLmNvLnVrL3JpbW9zL2ltYWdlcy9tZWV0aW5nLmpwZw==
[2] => aHR0cDovL29ybGFuZG9qb2VzLmNvLnVrL3JpbW9zL2ltYWdlcy90ZXN0aW1vbmlhbC5qcGc=
[3] => aHR0cDovL29ybGFuZG9qb2VzLmNvLnVrL3JpbW9zL2ltYWdlcy93b3Jrcy5qcGc=
)
Array
(
[0] => http://orlandojoes.co.uk/rimos/images/bg.jpg
[1] => http://orlandojoes.co.uk/rimos/images/meeting.jpg
[2] => http://orlandojoes.co.uk/rimos/images/testimonial.jpg
[3] => http://orlandojoes.co.uk/rimos/images/works.jpg
)
You have two bugs in your code.
$data = file_get_contents($imgUrls); // This is an array of URLs
$data = base64_encode($imgUrls); // You encode the URLs here!
Should be:
$data = file_get_contents($image); // $image instead of $imgUrls
$data = base64_encode($data); // $data instead of $imgUrls
Or simply:
$data = base64_encode(file_get_contents($image));
Side note, you don't need array_push()
in your code, it's typically only needed if you want to push more than one item at one time. So, you can change it to just:
$body[] = $data;