2016-01-20 21 views
0

我正在努力使用cakephp来实现具有图像的CRUD。我设法上传图像,保存它(在maria/mysql数据库中),现在我可以在phpmyadmin中看到上传的图像。不能使用cakephp显示数据库中的图像

现在我试图在浏览器中显示一些图像。在我MapasController我有以下方法:

public function mostrarImagem($id){ 
     $file = $this->Mapas->get($id); 
     $this->response->body($file->arquivo->uri); 
     $this->response->type('image/png'); 
     //$this->response->download('filename_for_download.png'); 
     return $this->response; 
    } 

但在浏览器中呼吁这给我的信息“图像‘http://localhost:8765/Mapas/mostrarImagem/5’无法显示,因为它包含错误”。如果我取消$ this-> response->下载行的注释,我会下载一个无效文件。 转储文件$变量,我得到以下内容:

Mapa {#147 ▼ 
    +"id": 5 
    +"titulo": "blablabla" 
    +"arquivo": stream resource @8 ▶} 
    +"created": Time {#145 ▶} 
    +"modified": Time {#146 ▶} 
    +"sistema_id": 5 
    +"unidade_id": null 
    +"[new]": false 
    +"[accessible]": array:1 [▶] 
    +"[dirty]": [] 
    +"[original]": [] 
    +"[virtual]": [] 
    +"[errors]": [] 
    +"[repository]": "Mapas" 
} 

为$文件 - 内容> arquivo是:

+"arquivo": stream resource @6 ▼ 
    wrapper_type: "RFC2397" 
    stream_type: "RFC2397" 
    mode: "rb" 
    unread_bytes: 0 
    seekable: true 
    uri:  "data:text/plain;base64,iVBORw0KGgoAAAANSUhEUgAAA08AAAGnCAYAAABvtmzcAAAABmJLR0Q(....) 
mediatype: "text/plain" 
base64: true 
options: [] 

我在做什么错?

回答

2

数据URI不是一个有效的PNG图像源,并且您不会以任何方式获取数据URI,因为在流上没有这样的属性,您在转储中查看的内容只是调试信息元数据,所以这是预期的行为。

您想要取回原始数据。

$this->response->body(stream_get_contents($file->arquivo, -1, 0)); 

...例如。

又见