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