处理的base64带json_en code和PHP codeD映像 [英] Handling base64 encoded images with json_encode and PHP

查看:143
本文介绍了处理的base64带json_en code和PHP codeD映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的PHP类返回小的base64连接codeD映像,该链接:

My PHP class returns a small base64 encoded image, link this:

class Service
{

  function getLogo()
  {
    $image = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c
             QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwA";

    return 'data:image/png;base64,' . $image;
  }

}

使用 json_en code 将添加返回图像 \\ n 的每一行后 $图片

Returning the image using json_encode will add \n after each line of $image:

$service = new Service();
$response = array('name' => $service->getName(), 'logo' => $service->getLogo());
header('Content-type: application/json');
echo json_encode($response);

如何正确处理呢?

How to handle it correctly?

推荐答案

您已经通过在两行分裂之错位您的base64数据。它应该是

You've mangled your base64 data by splitting it across two lines. it should be

function getLogo() {
    $image = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4cQAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwA";

    return 'data:image/png;base64,' . $image;
}

不带换行。

这篇关于处理的base64带json_en code和PHP codeD映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆