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}.
public function foto($id)
{
$usuario = $this->Usuario->get($id);
$foto = $usuario['foto'];
if (strpos($foto, "base64")===false) {
$content = file_get_contents($foto,FILE_BINARY);
$format= getimagesize($foto);
$this->autoRender = false;
$this->response->type($format['mime']);
$this->response->body($content);
}
else{
$array = explode(":",$foto);
$type = explode(";",$array[1])[0];
$type = explode("/",$type)[1];
$data = explode( ',', $foto );
$this->autoRender = false;
$this->response->type($type);
$source = ( base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $foto)));
$this->response->body($source);
}
//
return $this->response;
}
Today, I have two cases of uploads: one by external url, and the other by base64 encoded image
First case:
$content = file_get_contents($foto,FILE_BINARY);
$format= getimagesize($foto);
$this->autoRender = false;
$this->response->type($format['mime']);
$this->response->body($content);
Second Case:
$array = explode(":",$foto);
$type = explode(";",$array[1])[0];
$type = explode("/",$type)[1];
$data = explode( ',', $foto );
$this->autoRender = false;
$this->response->type($type);
$source = ( base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $foto)));
$this->response->body($source);
The problem is that none of them works. Both return a empty image. How do I get the images to load correctly?