简体   繁体   English

将PHP数组中URL上的图像编码为Base64

[英]Encode Images at URL in PHP Array as Base64

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. 这是我的代码,我试图获取图像内容并将其编码为base64,但是我一直以URL作为base64字符串结尾。

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. 最后,我需要从API以数组形式获取图像,我需要将它们转码为Base64以存储在本地数据库中。

This is based on the Gravity Forms API, Wordpress, PHP, mySQL, (LAMP) etc. 这基于Gravity Forms API,Wordpress,PHP,mySQL,(LAMP)等。

<?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. 旁注,您的代码中不需要array_push() ,通常仅当您想一次推送多个项目时才需要。 So, you can change it to just: 因此,您可以将其更改为:

$body[] = $data;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM