4

I save PNG's binary content in database. I want display this PNG's on page without temporary save file on disk. I think need generate img tag like <img src="data:image/png;base64,......

But I do not understand how it is better to implement it and what type of field to take as a basis.

            Image::make('Image')->displayUsing(function($item) {
                $mime_type = 'image/png';
                return 'data: ' . $mime_type . ';base64,' . base64_encode($item);
            }),

But Laravel Nova generated:

<img src="http://172.18.0.3/storage/data: image/png;base64,......" class="rounded-full w-8 h-8" style="object-fit: cover;">

Added unnecessary http://172.18.0.3/storage/and rounded class.

How to prevent it adding?

2 Answers 2

6

Work code for Laravel Nova 2.0.1:

Image::make('QRCode', 'qrcode')->thumbnail(function($value, $disk) {
    return 'data: image/png;base64,' . $value;
})->preview(function($value, $disk) {
    return 'data: image/png;base64,' . $value;
})->displayUsing(function($value) {
return  base64_encode($value);})

Also need remove rounded-full from field.thumbnailUrl?t("img",{staticClass:"rounded-full w-8 h-8", in file public\vendor\nova\app.js

Sign up to request clarification or add additional context in comments.

Comments

2

Override thumbnail & preview for image url

Try below code snippet

Image::make('Image')->thumbnail(function($value, $disk) {
        return 'data: image/png;base64,' . base64_encode($value);
    })->preview(function($value, $disk) {
        return 'data: image/png;base64,' . base64_encode($value);
    }),

1 Comment

Do not work in Laravel Nova 2.0.1: { "message": "Malformed UTF-8 characters, possibly incorrectly encoded", "exception": "InvalidArgumentException", "file": "/var/www/app/vendor/laravel/framework/src/Illuminate/Http/JsonResponse.php", "line": 75, Also do not work if return '' instead 'data: image/png;base64,' . base64_encode($value). Wrong way....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.