ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
SVG.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25use ILIAS\ResourceStorage\Flavour\Machine\DefaultMachines\ImageSizeCalculator;
26
30class SVG implements Extractor
31{
32 use ImageSizeCalculator;
33
34 private bool $pattern = false;
35
36 public function readImage(\Imagick $img, Stream $stream, PagesToExtract $definition): \Imagick
37 {
38 $img->readImageBlob(
39 $this->prescaleSVG((string) $stream, $definition->getMaxSize())
40 );
41
42 // add pattern to background
43 if ($this->pattern) {
44 $pattern = new \Imagick();
45 $x = 10;
46 $pattern->readImageBlob(
47 '<?xml version="1.0" encoding="UTF-8"?><svg id="Ebene_2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 '
48 . ($x * 2) . ' ' . ($x * 2) . '"><defs><style>.cls-1{fill:#afafaf;}.cls-1,.cls-2{stroke-width:0px;}.cls-2{fill:#e8e8e8;}</style></defs><g id="Ebene_1-2"><rect class="cls-1" width="'
49 . $x . '" height="' . $x . '"/><rect class="cls-2" y="' . $x . '" width="' . $x . '" height="' . $x
50 . '"/><rect class="cls-1" x="' . $x . '" y="' . $x . '" width="' . $x . '" height="' . $x
51 . '"/><rect class="cls-2" x="' . $x . '" width="' . $x . '" height="' . $x . '"/></g></svg>'
52 );
53 $pattern = $img->textureImage($pattern);
54 $pattern->compositeImage(
55 $img,
56 \Imagick::COMPOSITE_OVER,
57 0,
58 0
59 );
60 $img = $pattern;
61 }
62
63 return $img;
64 }
65
66 public function getBackground(): \ImagickPixel
67 {
68 return new \ImagickPixel('none');
69 }
70
71 public function getAlphaChannel(): int
72 {
73 return \Imagick::ALPHACHANNEL_ACTIVATE;
74 }
75
76 public function getRemoveColor(): ?\ImagickPixel
77 {
78 return new \ImagickPixel('transparent');
79 }
80
81 public function getResolution(): int
82 {
83 return 96;
84 }
85
86 public function getTargetFormat(): string
87 {
88 return $this->pattern ? 'jpg' : 'png64';
89 }
90
96 private function prescaleSVG(string $svg_content, int $max_length): string
97 {
98 try {
99 $dom = new \DOMDocument();
100 $dom->loadXML($svg_content);
101 $svg = $dom->documentElement;
102
103 // Get Viewbox if available
104 $viewbox = $svg->getAttribute('viewBox');
105 if ($viewbox === '') {
106 return $svg_content;
107 }
108 $viewbox = explode(' ', $viewbox);
109 $width = (float) ($viewbox[2] ?? 0);
110 $height = (float) ($viewbox[3] ?? 0);
111
112 if ($width === 0 || $height === 0) {
113 return $svg_content;
114 }
115
116 [$new_width, $new_height] = $this->calculateWidthHeight(
117 ceil($width),
118 ceil($height),
119 $max_length
120 );
121 $svg->setAttribute('width', (string) $new_width);
122 $svg->setAttribute('height', (string) $new_height);
123
124 return $dom->saveXML($svg);
125 } catch (\Throwable) {
126 return $svg_content;
127 }
128 }
129}
prescaleSVG(string $svg_content, int $max_length)
@description SVGs usually become extremely poou in quality when converted, because they start from a ...
Definition: SVG.php:96
readImage(\Imagick $img, Stream $stream, PagesToExtract $definition)
Definition: SVG.php:36