本文介绍了从数据库显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表用户


image(blob type)
------------------------------
0xFFD8FFE0

myController

myController

function displayImage($id)
{

        $this->autoRender = false;


        $peoples=TableRegistry::get('peoples');
        $peoples=$peoples->getRecord([ 'image'])->first();

        header('Content-Type: image/jpeg');

         echo $peoples['image'];
}

模型

function getRecord($fields)
{
    return $this->find('all')->select($fields);
}

但设置此

<img id="imgPreview" src="displayImage/30">

当我打电话给 myController / displayImage / 30时,图片没有显示任何内容这个节目资源ID#170 给我!

image does not show any things, when i call myController/displayImage/30 this show Resource id #170 to me!

为什么?

如何显示图像?

推荐答案

你需要从那里读取资源获取实际内容(图像数据)

You need to read from that returned resource to get the actual content (image data)

public function displayImage($id)
{
    // ...
    echo stream_get_contents($peoples['image']);
}

这是您方法的修改工作示例。我假设你有一个名为 Peoples 的模型,其属性为 image (BLOB)和 $ id 指基础表中的某一行

Here is modified working example of your method. I've assumed, that you have model named Peoples with property image(BLOB) and $id refers to some row in an underlying table

public function displayImage($id) {
    $table = TableRegistry::get('Peoples');
    $entity = $table->get($id);

    $this->response->type('jpg');
    $this->response->body(stream_get_contents($entity->image));

    return $this->response;
}

这篇关于从数据库显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:25