Return image from symfony2 controller

I would like to know how I can return an image from a controller without any template. I would like to use it to track pixels 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 in the navigator I have a broken link (file not found ...)

+8
symfony controller response
source share
6 answers

Now you return the file name as the response body and write the content file to the filename Content-Disposition property.

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

it should be:

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

... and ...

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

it should be...

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

right?

Further consider this question .

+9
source share

According to the Symfony Docs docs, when serving files you can use BinaryFileResponse :

 use Symfony\Component\HttpFoundation\BinaryFileResponse; $file = 'path/to/file.txt'; $response = new BinaryFileResponse($file); // you can modify headers here, before returning return $response; 

This BinaryFileResponse automatically processes some HTTP request headers and saves you from using readfile() or other file functions.

+16
source share

This works great for me.

 $filepath = "/path/to/my/image/chart.png"; $filename = "chart.png"; $response = new Response(); $disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_INLINE, $filename); $response->headers->set('Content-Disposition', $disposition); $response->headers->set('Content-Type', 'image/png'); $response->setContent(file_get_contents($filepath)); return $response; 
+2
source share

In addition, I had to change:

 $file = readfile("/path/to/my/image/1px.png"); 

:

 $file = file_get_contents("/path/to/my/image/1px.png"); 

The readfile seemed to echo when it reads that it forces the headers to output early and negates the forced Content-Type header.

+1
source share

file_get_contents is a bad idea . Reading a large list of images via file_get_contents killed my small server. I had to find another solution, and now it works perfectly and very quickly for me.

The key is to use readfile ($ sFileName) instead of file_get_contents. Symfony Stream Response can perform a callback function that will be executed upon sending ($ oResponse-> send ()). So this is a good place to use readfile ().

As a small benefit, I recorded a caching method.

 use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\StreamedResponse; class ImageController { public function indexAction(Request $oRequest, Response $oResponse) { // Get the filename from the request // eg $oRequest->get("imagename") $sFileName = "/images_directory/demoimage.jpg"; if( ! is_file($sFileName)){ $oResponse->setStatusCode(404); return $oResponse; } // Caching... $sLastModified = filemtime($sFileName); $sEtag = md5_file($sFileName); $sFileSize = filesize($sFileName); $aInfo = getimagesize($sFileName); if(in_array($sEtag, $oRequest->getETags()) || $oRequest->headers->get('If-Modified-Since') === gmdate("D, d MYH:i:s", $sLastModified)." GMT" ){ $oResponse->headers->set("Content-Type", $aInfo['mime']); $oResponse->headers->set("Last-Modified", gmdate("D, d MYH:i:s", $sLastModified)." GMT"); $oResponse->setETag($sEtag); $oResponse->setPublic(); $oResponse->setStatusCode(304); return $oResponse; } $oStreamResponse = new StreamedResponse(); $oStreamResponse->headers->set("Content-Type", $aInfo['mime']); $oStreamResponse->headers->set("Content-Length", $sFileSize); $oStreamResponse->headers->set("ETag", $sEtag); $oStreamResponse->headers->set("Last-Modified", gmdate("D, d MYH:i:s", $sLastModified)." GMT"); $oStreamResponse->setCallback(function() use ($sFileName) { readfile($sFileName); }); return $oStreamResponse; } } 
0
source share

Fast anders

 use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\ResponseHeaderBag; class AnotherController extends Controller { public function imagesAction($img = 'my_image.jpg'){ $filepath = '/path/to/images/'.$img; if(file_exists($filepath)){ $response = new Response(); $disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_INLINE, $img); $response->headers->set('Content-Disposition', $disposition); $response->headers->set('Content-Type', 'image/jpeg'); $response->setContent(file_get_contents($filepath)); return $response; } else{ return $this->redirect($this->generateUrl('my_url_to_site_index')); } } } 
0
source share

All Articles