Created
April 15, 2021 06:43
-
-
Save wernerkrauss/885775050224de10dc7223851a3f7c69 to your computer and use it in GitHub Desktop.
Silversttripe DOMPDF Image helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In your template you need to call the image with the relative path to your
$BASE
folder, e.g. project root. like