title: Cakephp3: Return image content from controller
body: <p>I have an application made in cakephp3 and I want to load the images directly from the controller, with a url like user/photo/{id}.</p>
  
  <pre><code>  public function foto($id)
  {
    $usuario = $this-&gt;Usuario-&gt;get($id);
    $foto = $usuario['foto'];
    if (strpos($foto, "base64")===false) {
        $content = file_get_contents($foto,FILE_BINARY);
      $format=  getimagesize($foto);
      $this-&gt;autoRender = false;
      $this-&gt;response-&gt;type($format['mime']);
  
      $this-&gt;response-&gt;body($content);
    }
    else{
      $array = explode(":",$foto);
      $type = explode(";",$array[1])[0];
      $type = explode("/",$type)[1];
  
      $data = explode( ',', $foto );
  
      $this-&gt;autoRender = false;
      $this-&gt;response-&gt;type($type);
      $source = ( base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $foto)));
      $this-&gt;response-&gt;body($source);
    }
    //
    return $this-&gt;response;
  
  }
  </code></pre>
  
  <p>Today, I have two cases of uploads: one by external url, and the other by base64 encoded image
  First case:</p>
  
  <pre><code>  $content = file_get_contents($foto,FILE_BINARY);
      $format=  getimagesize($foto);
      $this-&gt;autoRender = false;
      $this-&gt;response-&gt;type($format['mime']);
  
      $this-&gt;response-&gt;body($content);
  </code></pre>
  
  <p>Second Case: </p>
  
  <pre><code>$array = explode(":",$foto);
      $type = explode(";",$array[1])[0];
      $type = explode("/",$type)[1];
  
      $data = explode( ',', $foto );
  
      $this-&gt;autoRender = false;
      $this-&gt;response-&gt;type($type);
      $source = ( base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $foto)));
      $this-&gt;response-&gt;body($source);
  </code></pre>
  
  <p>The problem is that none of them works. Both return a empty image. How do I get the images to load correctly?</p>