title: Symfony - image in response - speed, optimization
body: <p>I generate dynamically graphic in my controller:</p>
  
  <ol>
  <li><p>if not file exists (unique filename) then generate graphic and save it</p></li>
  <li><p>return RedirectResponse($url)</p></li>
  </ol>
  
  <p>(3. I've tried also:</p>
  
  <pre><code>    $fp = fopen($url, "rb");
      $str = stream_get_contents($fp);
      fclose($fp);
  
      $response = new Response($str, 200);
      $response-&gt;headers-&gt;set('Content-Type', 'image/png');
      return $response;
  </code></pre>
  
  <p>)</p>
  
  <p>I need it to generate dynamically userbars for users [on this graphic some text will be automatically updating if some changes will be done on my websites]. </p>
  
  <p>So it's important for me that this script works fast and doesn't kill my server/bandwitch. To achieve that I've made cache where all generated graphics are stored. But i've got a problem with speed, solutions which i've described above are pretty slow, when i've tested it in chrome with developer tool (F12) then it shows me that both solution (2. and 3.) takes about ~1s (when the same graphics [without controllers etc. just direct url to .png] loads 44ms). </p>
  
  <p>So if only for me it takes about ~1s per graphic then if for example 1000 users will generate at the same time this graphic it can slower my website I think.</p>
  
  <p>Please help, do you know any faster ways to do that?</p>
  
  <p>PS. Redirect isn't most desire thing for me, because I would prefer to leave url as it is, for example smth/username/userbar/ insted of smth/img/userbars/cache/blabla.png. But if it will be much faster then I can stand it :).</p>
  
  <p>EDIT: To be clear: I'm not looking for solution to cache images, my problem is that returning images via controller (already generated images, just from ftp) takes about 1s, when normally if you do for example </p>
  
  <pre><code>           &lt;img src="direct_url_to_png"&gt; 
  </code></pre>
  
  <p>it takes about 44ms.</p>