phpsymfonyfosrestbundlesymfony-2.6

How to output 1x1 pixel from base64 using FOSRestBundle


I need to return this:

return header('Content-Type: image/png'); 
echo base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=');

by using FOSRestBundle but I don't know how to handle the echo in there. I have this code:

public function getTrackingEmailAction(ParamFetcher $paramFetcher, Request $request)
{
    $view = View::create();
    $response = [];
    $view->setData($response)->setHeader('Content-Type', 'image/png')->setStatusCode(200);

    return $view;
}

How do I send the base64 for display the 1x1 pixel? Do I need a view? (twig view)


Solution

  • According to the upper code snippet, you don’t have to return Base64. Instead, the opposite is true: you have to return the raw image data, which is merely Base64-encoded as having binary stuff inside PHP code is not so nice ;-)

    In your controller action, you therefore only have to return a proper Response with the image data as content

    return new Response(base64_decode('...'), 200, ['Content-Type' => 'image/png']);