Search code examples
symfonycontrollerresponse

Return Image from Controller symfony2


I would like to know how can i return an image from the controller without any template. I would like to use it for pixel tracking in a newsletter.

I start with this code

    $image = "1px.png";
    $file =    readfile("/path/to/my/image/1px.png");
    $headers = array(
        'Content-Type'     => 'image/png',
        'Content-Disposition' => 'inline; filename="'.$file.'"');
    return new Response($image, 200, $headers);

But on the navigator i have a broken link (file not found...)


Solution

  • Right now you return the filename as response body and write the file-content to the filename property of Content-Disposition.

    return new Response($image, 200, $headers);
    

    should be:

    return new Response($file, 200, $headers);
    

    ... and ...

    'Content-Disposition' => 'inline; filename="'.$file.'"');
    

    should be ...

    'Content-Disposition' => 'inline; filename="'.$image.'"');
    

    right?

    Further take a look at this question.