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?
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.
The answer is given by Marc B. This is just a comment. If code formatting really is that important to you that you can't tolerate long lines (why?), you could always format the PHP thus:
class Service
{
function getLogo()
{
$image = 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c';
$image .= 'QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwA';
return 'data:image/png;base64,' . $image;
}
}