Bläddra i källkod

Embeds assets as base64 strings in Twig

Adds Twig functions to generate base64 encoded data URIs for assets.
This allows embedding images directly into HTML, reducing the number of HTTP requests and improving page load times.

Also fixes an issue where the `FileStatusEnum` value was not being correctly accessed in the `DownloadProvider`.
Olivier Massot 5 månader sedan
förälder
incheckning
c8c845452a

+ 25 - 0
src/Service/Twig/AssetsExtension.php

@@ -19,9 +19,13 @@ use Twig\TwigFunction;
  */
 class AssetsExtension extends AbstractExtension
 {
+    private string $publicDir;
+
     public function __construct(
+        string $projectDir,
         readonly private FileManager $fileManager,
     ) {
+        $this->publicDir = $projectDir . '/public';
     }
 
     public function getFunctions(): array
@@ -29,6 +33,8 @@ class AssetsExtension extends AbstractExtension
         return [
             new TwigFunction('absPath', [$this, 'absPath']),
             new TwigFunction('fileImagePath', [$this, 'fileImagePath']),
+            new TwigFunction('asset_absolute', [$this, 'getAssetAbsolutePath']),
+            new TwigFunction('asset_base64', [$this, 'getAssetBase64']),
         ];
     }
 
@@ -59,4 +65,23 @@ class AssetsExtension extends AbstractExtension
     {
         return ltrim($this->fileManager->getImageUrl($file, $size, true), '/');
     }
+
+    public function getAssetAbsolutePath(string $path): string
+    {
+        return $this->publicDir . '/' . ltrim($path, '/');
+    }
+
+    public function getAssetBase64(string $path): string
+    {
+        $absolutePath = $this->getAssetAbsolutePath($path);
+
+        if (!file_exists($absolutePath)) {
+            throw new \RuntimeException("Asset not found: {$absolutePath}");
+        }
+
+        $imageData = file_get_contents($absolutePath);
+        $mimeType = mime_content_type($absolutePath);
+
+        return 'data:' . $mimeType . ';base64,' . base64_encode($imageData);
+    }
 }

+ 1 - 1
src/State/Provider/Core/DownloadProvider.php

@@ -52,7 +52,7 @@ final class DownloadProvider implements ProviderInterface
             throw new \RuntimeException('File '.$fileId.' does not exist; abort.');
         }
         if ($file->getStatus() !== FileStatusEnum::READY) {
-            throw new \RuntimeException('File '.$fileId.' has '.$file->getStatus().' status; abort.');
+            throw new \RuntimeException('File '.$fileId.' has '.$file->getStatus()->value.' status; abort.');
         }
 
         $content = $this->fileManager->read($file);

+ 5 - 5
templates/export/licence_cmf.html.twig

@@ -203,7 +203,7 @@
                             <tbody>
                             <tr>
                                 <td width="250" class="relative">
-                                    <img src="{{ 'images/cmf_licence.png' }}"
+                                    <img src="{{ asset_base64('images/cmf_licence.png') }}"
                                             width="170" height="86"/>
                                     <span id="year_head">
                                         {{ licence.year }}
@@ -211,7 +211,7 @@
                                 </td>
                                 <td width="250">
                                     <div align="right">
-                                        <img src="{{ 'images/cmf-reseau.png' }}"
+                                        <img src="{{ asset_base64('images/cmf-reseau.png') }}"
                                                 width="200" height="86"/>
                                     </div>
                                 </td>
@@ -268,7 +268,7 @@
                                 <td height="62" width="62" id="avatar">
                                     <div align="center">
                                         {% if(licence.logo is null) %}
-                                            <img src="{{ 'images/picto_face.png' }}"
+                                            <img src="{{ asset_base64('images/picto_face.png') }}"
                                                  height="62"/>
                                         {% else %}
                                             <img src="{{ fileImagePath(licence.logo, 'sm') }}"
@@ -290,7 +290,7 @@
                                     <div align="center">
                                         {% if(licence.personAvatar is null) %}
                                             <img
-                                                    src="{{ 'images/picto_face.png' }}"
+                                                    src="{{ asset_base64('images/picto_face.png') }}"
                                                     height="62"/>
                                         {% else %}
                                             <img class="avatar"
@@ -314,7 +314,7 @@
                             <td height="54" width="84" valign="middle"
                                 style="vertical-align: bottom;">
                                 <div align="center">
-                                    <img src="{{ 'images/cmf_licence.png' }}"
+                                    <img src="{{ asset_base64('images/cmf_licence.png') }}"
                                          height="35"/>
                                     <span id="year_card">{{ licence.year }}</span>
                                 </div>