Skip to content

Instantly share code, notes, and snippets.

@wernerkrauss
Created April 15, 2021 06:43
Show Gist options
  • Save wernerkrauss/885775050224de10dc7223851a3f7c69 to your computer and use it in GitHub Desktop.
Save wernerkrauss/885775050224de10dc7223851a3f7c69 to your computer and use it in GitHub Desktop.
Silversttripe DOMPDF Image helper
<?php
namespace Netwerkstatt\Helper;
use SilverStripe\View\TemplateGlobalProvider;
class DomPDFHelper implements TemplateGlobalProvider
{
/**
* @return array
*/
public static function get_template_global_variables(): array
{
return [
'InlineBase64',
'Base64'
];
}
public static function InlineBase64(string $path): string
{
$mimeType = mime_content_type(self::getAbsolutePath($path));
$data = [
'data:',
$mimeType,
';',
'base64,',
self::Base64($path)
];
return implode('', $data);
}
public static function Base64($path): string
{
$data = file_get_contents(self::getAbsolutePath($path));
return base64_encode($data);
}
public static function getAbsolutePath($path): string
{
return implode('/', [BASE_PATH, $path]);
}
}
@wernerkrauss
Copy link
Author

In your template you need to call the image with the relative path to your $BASE folder, e.g. project root. like

<img src="$InlineBase64('public/resources/themes/myshop/dist/images/logo.png')" class="logo"/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment