refactor: simplify image base64 encoding in report export actions using MediaDataUri
This commit is contained in:
parent
382d0862b8
commit
23f32b3cf1
@ -3,6 +3,7 @@
|
||||
namespace App\Filament\Resources\Manage\Cooperations\Actions\Report;
|
||||
|
||||
use App\Enums\RoleEnum;
|
||||
use App\Filament\Support\MediaDataUri;
|
||||
use App\Settings\GeneralSettings;
|
||||
use Barryvdh\DomPDF\Facade\Pdf;
|
||||
use Filament\Actions\Action;
|
||||
@ -35,15 +36,7 @@ protected function setUp(): void
|
||||
}
|
||||
|
||||
$records->each(function ($record) {
|
||||
$image = $record->getFirstMedia('reports');
|
||||
$record->imageBase64 = null;
|
||||
if ($image) {
|
||||
$imagePath = $image->getPath();
|
||||
if (file_exists($imagePath)) {
|
||||
$imageData = base64_encode(file_get_contents($imagePath));
|
||||
$record->imageBase64 = 'data:image/'.pathinfo($imagePath, PATHINFO_EXTENSION).';base64,'.$imageData;
|
||||
}
|
||||
}
|
||||
$record->imageBase64 = MediaDataUri::fromMedia($record->getFirstMedia('reports'));
|
||||
});
|
||||
|
||||
return response()->streamDownload(function () use ($records, $settings, $logoBase64) {
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
namespace App\Filament\Resources\Manage\Cooperations\Actions\Report;
|
||||
|
||||
use App\Enums\RoleEnum;
|
||||
use App\Filament\Support\MediaDataUri;
|
||||
use App\Models\Report;
|
||||
use App\Settings\GeneralSettings;
|
||||
use Barryvdh\DomPDF\Facade\Pdf;
|
||||
@ -34,15 +35,7 @@ protected function setUp(): void
|
||||
$logoBase64 = 'data:image/'.pathinfo($logoPath, PATHINFO_EXTENSION).';base64,'.$logoData;
|
||||
}
|
||||
|
||||
$image = $record->getFirstMedia('reports');
|
||||
$imageBase64 = null;
|
||||
if ($image) {
|
||||
$imagePath = $image->getPath();
|
||||
if (file_exists($imagePath)) {
|
||||
$imageData = base64_encode(file_get_contents($imagePath));
|
||||
$imageBase64 = 'data:image/'.pathinfo($imagePath, PATHINFO_EXTENSION).';base64,'.$imageData;
|
||||
}
|
||||
}
|
||||
$imageBase64 = MediaDataUri::fromMedia($record->getFirstMedia('reports'));
|
||||
|
||||
return response()->streamDownload(function () use ($record, $settings, $logoBase64, $imageBase64) {
|
||||
echo Pdf::loadHtml(
|
||||
|
||||
27
app/Filament/Support/MediaDataUri.php
Normal file
27
app/Filament/Support/MediaDataUri.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Support;
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||
|
||||
class MediaDataUri
|
||||
{
|
||||
public static function fromMedia(?Media $media): ?string
|
||||
{
|
||||
if (! $media) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$disk = Storage::disk($media->disk);
|
||||
$path = $media->getPathRelativeToRoot();
|
||||
|
||||
if (! $disk->exists($path)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$mime = $media->mime_type ?: 'application/octet-stream';
|
||||
|
||||
return 'data:'.$mime.';base64,'.base64_encode($disk->get($path));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user