dongrao9454 2011-11-23 14:57 采纳率: 100%
浏览 692
已采纳

使用json_encode和PHP处理base64编码图像

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;
  }

}

Returning the image using json_encode will add 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?

  • 写回答

2条回答 默认 最新

  • dongye1942 2011-11-23 15:12
    关注

    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;
    }
    

    with no line breaks.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?