Skip to content

Commit ec46268

Browse files
committed
draft svg image tag
1 parent 0f5a293 commit ec46268

File tree

1 file changed

+61
-5
lines changed

1 file changed

+61
-5
lines changed

src/SVG.php

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,7 +2523,7 @@ protected function handleSVGTagStart(
25232523
'line' => $this->parseSVGTagSTARTline($soid, $attribs['attr'], $svgstyle),
25242524
'polyline' => $this->parseSVGTagSTARTpolygon($soid, $attribs['attr'], $svgstyle),
25252525
'polygon' => $this->parseSVGTagSTARTpolygon($soid, $attribs['attr'], $svgstyle),
2526-
'image' => $this->parseSVGTagSTARTimage($soid),
2526+
'image' => $this->parseSVGTagSTARTimage($soid, $attribs['attr'], $svgstyle),
25272527
'text' => $this->parseSVGTagSTARTtext($soid, $attribs['attr'], $svgstyle),
25282528
'tspan' => $this->parseSVGTagSTARTtspan($soid, $attribs['attr'], $svgstyle),
25292529
'use' => $this->parseSVGTagSTARTuse($soid, $attribs, $parser),
@@ -2813,7 +2813,7 @@ protected function parseSVGTagSTARTlinearGradient(int $soid, array $attr)
28132813
$this->getSVGTransformMatrix($attr['gradientTransform']);
28142814
}
28152815
$this->svgobjs[$soid]['gradients'][$gid]['coords'] = [$px1, $py1, $px2, $py2];
2816-
if (isset($attr['xlink:href']) && !empty($attr['xlink:href'])) {
2816+
if (!empty($attr['xlink:href'])) {
28172817
// gradient is defined on another place
28182818
$this->svgobjs[$soid]['gradients'][$gid]['xref'] = substr($attr['xlink:href'], 1);
28192819
}
@@ -3314,16 +3314,72 @@ protected function parseSVGTagSTARTpolygon(int $soid, array $attr, array $svgsty
33143314
* Parse the SVG Start tag 'image'.
33153315
*
33163316
* @param int $soid ID of the current SVG object.
3317+
* @param TSVGAttributes $attr SVG attributes.
3318+
* @param TSVGStyle $svgstyle Current SVG style.
33173319
*
33183320
* @return void
33193321
*/
3320-
protected function parseSVGTagSTARTimage(int $soid)
3322+
protected function parseSVGTagSTARTimage(int $soid, array $attr, array $svgstyle)
33213323
{
33223324
if (!empty($this->svgobjs[$soid]['textmode']['invisible'])) {
33233325
return;
33243326
}
3325-
$soid = $soid; // @phpstan-ignore-line
3326-
//@TODO
3327+
if ($this->svgobjs[$soid]['clipmode']) {
3328+
return;
3329+
}
3330+
if (empty($attr['xlink:href'])) {
3331+
return;
3332+
}
3333+
$img = $attr['xlink:href'];
3334+
$posx = (isset($attr['x']) ? $this->toUnit(
3335+
$this->getUnitValuePoints($attr['x'], self::REFUNITVAL, self::SVGUNIT)
3336+
) : 0.0);
3337+
$posy = (isset($attr['y']) ? $this->toUnit(
3338+
$this->getUnitValuePoints($attr['y'], self::REFUNITVAL, self::SVGUNIT)
3339+
) : 0.0);
3340+
$width = (isset($attr['width']) ? $this->toUnit(
3341+
$this->getUnitValuePoints($attr['width'], self::REFUNITVAL, self::SVGUNIT)
3342+
) : 0.0);
3343+
$height = (isset($attr['height']) ? $this->toUnit(
3344+
$this->getUnitValuePoints($attr['height'], self::REFUNITVAL, self::SVGUNIT)
3345+
) : 0.0);
3346+
$this->svgobjs[$soid]['out'] .= $this->graph->getStartTransform();
3347+
$this->svgobjs[$soid]['out'] .= $this->getOutSVGTransformation($svgstyle['transfmatrix']);
3348+
$this->parseSVGStyle(
3349+
$this->svgobjs[$soid],
3350+
$posx,
3351+
$posy,
3352+
$width,
3353+
$height,
3354+
);
3355+
if (
3356+
'svg' === strtolower(
3357+
trim(
3358+
pathinfo(
3359+
($purl = parse_url($img, PHP_URL_PATH)) ? $purl : '',
3360+
PATHINFO_EXTENSION
3361+
),
3362+
)
3363+
)
3364+
) {
3365+
// @TODO
3366+
// $this->ImageSVG($img, $posx, $posy, $width, $height);
3367+
return;
3368+
}
3369+
if (preg_match('/^data:image\/[^;]+;base64,/', $img, $match) > 0) {
3370+
// embedded image encoded as base64
3371+
$img = '@' . base64_decode(substr($img, strlen($match[0])));
3372+
}
3373+
$imgid = $this->image->add($img);
3374+
$this->svgobjs[$soid]['out'] .= $this->image->getSetImage(
3375+
$imgid,
3376+
$posx,
3377+
$posy,
3378+
$width,
3379+
$height,
3380+
$this->page->getPage()['pheight'],
3381+
);
3382+
$this->svgobjs[$soid]['out'] .= $this->graph->getStopTransform();
33273383
}
33283384

33293385
/**

0 commit comments

Comments
 (0)