Yii2 Display image stored in BLOB database field

2019-04-13 03:49发布

Greetings

I need to display images stored in BLOB database fields in a LISTVIEW or similar widget from Yii2

I have the actionCreate that saves the image in table named honra as a BLOB

public function actionCreate()
{
    $model = new Honra();

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        $file = UploadedFile::getInstance($model,'binaryfile');
        $model->binaryfile=$file;
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

I also have the model named Honra

class Honra extends \yii\db\ActiveRecord{

public static function tableName()
{
    return 'honra';
}

/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['nome', 'texto'], 'required'],
        [['texto', 'binaryfile'], 'string'],
        [['ativo'], 'integer'],
        [['nome'], 'string', 'max' => 255],
        [['fileName'], 'string', 'max' => 100],
        [['fileType'], 'string', 'max' => 50]
    ];
}

/**
 * @inheritdoc
 */
public function attributeLabels()
{
    return [
        'id' => Yii::t('app', 'ID'),
        'nome' => Yii::t('app', 'Nome'),
        'texto' => Yii::t('app', 'Texto'),
        'fileName' => Yii::t('app', 'File Name'),
        'fileType' => Yii::t('app', 'File Type'),
        'binaryfile' => Yii::t('app', 'Binaryfile'),
        'ativo' => Yii::t('app', 'Ativo'),
    ];
}

}

The images are successfully stored in the table field named binaryfile

And i need to display each blob image stored in database in a view named view2 inside a LISTVIEW or similar widget

Anyone knows what block of code i need to put in view2 and the HonraController to achieve this ??

EDIT

SOLVED

HERE IS THE CODE THAT SOLVED THE ISSUE, NOW EVERYTHING IS WORKING WELL

public function actionCreate()
{
    $model = new Honra();

     if (Yii::$app->request->isPost) {

            $model->file = UploadedFile::getInstance($model, 'file');
            $filePath = 'uploadsimg/'  . $model->file->baseName . '.' . $model->file->extension;
            $model->fileName = $filePath;
            $model->save();

            $model->file->saveAs('uploadsimg/' . $model->file->baseName . '.' . $model->file->extension);
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
                return $this->redirect(Url::toRoute(['create']));
            }
        }
        return $this->render('create', ['model' => $model]);
}

MANY THANKS FOR THE HELP MIHAI

2条回答
Fickle 薄情
2楼-- · 2019-04-13 04:23

Here is the official documentation on Yii2 https://github.com/yiisoft/yii2/blob/master/docs/guide/input-file-upload.md It is quite simple.

some comments:
1) the pics might be private and it might be a big problem if somebody guesses the path of others. In this case you can put the file someplace NOT in the web folder. If you do this then you have to use the assets to show them assetManager->getPublishedUrl('@app/folder')?>/fav.ico
2) if the pics are not so sensitive then you can just save them someplace in the web folder.

As you can see you might have problems with uploading the same file name 2 times, as 1 will override the other. You can always rename the file, I would actually change their code to something like this

        if ($model->file && $model->validate()) {
$model->file = UploadedFile::getInstance($model, 'file');
            $filePath = 'uploads/' . hash('ripemd160', microtime()) . $model->file->baseName . '.' . $model->file->extension;
            $model->fileName = $filePath;
            $model->save();

            $model->file->saveAs('uploadsimg/' . $model->file->baseName . '.' . $model->file->extension);
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
                return $this->redirect(Url::toRoute(['create']));
            }
        }
查看更多
祖国的老花朵
3楼-- · 2019-04-13 04:34

The shortest way I found to display a image from a BLOB field (in a MySQL database) was using this code (in my view):

$sql = "SELECT FieldName FROM TableName"; //optional where statements etc.
$connection = Yii::$app->getDb();
$command = $connection->createCommand($sql);
$imgLogo = $command->queryScalar();
echo '<img src="data:image/jpeg;base64,'.base64_encode($imgLogo).'"/>';

Note that in this example I have only one row in the table 'TableName' as this example was used to retrieve the logo from the company. And there is only one company stored in that table.

查看更多
登录 后发表回答