|
|
@@ -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);
|
|
|
+ }
|
|
|
}
|