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?

4n70wa
  • 337
  • 4
  • 19

2 Answers2

5

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

4n70wa
  • 337
  • 4
  • 19
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);
    }),
Saumini Navaratnam
  • 8,439
  • 3
  • 42
  • 70
  • 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.... – 4n70wa Apr 26 '19 at 09:31