Skip to content

WIP: SVG Image Type Support #2778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions samples/Sample_47_SVG.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\PhpWord;

include_once 'Sample_Header.php';

// New Word document
echo date('H:i:s'), ' Create new PhpWord object', EOL;
$phpWord = new PhpWord();

$section = $phpWord->addSection();
$section->addText('SVG image without any styles:');
$svg = $section->addImage(__DIR__ . '/resources/sample.svg');

printSeparator($section);

$section->addText('SVG image with styles:');
$svg = $section->addImage(
__DIR__ . '/resources/sample.svg',
[
'width' => 200,
'height' => 200,
'align' => 'center',
'wrappingStyle' => PhpOffice\PhpWord\Style\Image::WRAPPING_STYLE_BEHIND,
]
);

function printSeparator(Section $section): void
{
$section->addTextBreak();
$lineStyle = ['weight' => 0.2, 'width' => 150, 'height' => 0, 'align' => 'center'];
$section->addLine($lineStyle);
$section->addTextBreak(2);
}

// Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers);
if (!CLI) {
include_once 'Sample_Footer.php';
}
96 changes: 96 additions & 0 deletions samples/resources/sample.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 49 additions & 0 deletions src/PhpWord/Element/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace PhpOffice\PhpWord\Element;

use DOMDocument;
use PhpOffice\PhpWord\Exception\CreateTemporaryFileException;
use PhpOffice\PhpWord\Exception\InvalidImageException;
use PhpOffice\PhpWord\Exception\UnsupportedImageTypeException;
Expand Down Expand Up @@ -432,6 +433,20 @@ private function checkImage(): void
{
$this->setSourceType();

$ext = strtolower(pathinfo($this->source, PATHINFO_EXTENSION));
if ($ext === 'svg') {
[$actualWidth, $actualHeight] = $this->getSvgDimensions($this->source);
$this->imageType = 'image/svg+xml';
$this->imageExtension = 'svg';
$this->imageFunc = null;
$this->imageQuality = null;
$this->memoryImage = false;
$this->sourceType = self::SOURCE_LOCAL;
$this->setProportionalSize($actualWidth, $actualHeight);

return;
}

// Check image data
if ($this->sourceType == self::SOURCE_ARCHIVE) {
$imageData = $this->getArchiveImageSize($this->source);
Expand Down Expand Up @@ -598,4 +613,38 @@ private function setProportionalSize($actualWidth, $actualHeight): void
}
}
}

public function getSvgDimensions(string $file): array
{
$xml = @file_get_contents($file);
if ($xml === false) {
throw new InvalidImageException("Impossible de lire le fichier SVG: $file");
}
libxml_use_internal_errors(true);
$dom = new DOMDocument();
if (!$dom->loadXML($xml)) {
throw new InvalidImageException('SVG invalide ou mal formé');
}
$svg = $dom->documentElement;

$wAttr = round((float) $svg->getAttribute('width'));
$hAttr = round((float) $svg->getAttribute('height'));

$w = (int) filter_var($wAttr, FILTER_SANITIZE_NUMBER_INT);
$h = (int) filter_var($hAttr, FILTER_SANITIZE_NUMBER_INT);

if ($w <= 0 || $h <= 0) {
$vb = $svg->getAttribute('viewBox');
if (preg_match('/^\s*[\d.+-]+[\s,]+[\d.+-]+[\s,]+([\d.+-]+)[\s,]+([\d.+-]+)\s*$/', $vb, $m)) {
$w = (int) round((float) $m[1]);
$h = (int) round((float) $m[2]);
}
}

if ($w <= 0 || $h <= 0) {
throw new InvalidImageException('Impossible de déterminer width/height ou viewBox valides pour le SVG');
}

return [$w, $h];
}
}
7 changes: 7 additions & 0 deletions src/PhpWord/Shared/Microsoft/PasswordEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

namespace PhpOffice\PhpWord\Shared\Microsoft;

use InvalidArgumentException;

/**
* Password encoder for microsoft office applications.
*/
Expand Down Expand Up @@ -119,6 +121,11 @@ public static function hashPassword($password, $algorithmName = self::ALGORITHM_
// Get the single-byte values by iterating through the Unicode characters of the truncated password.
// For each character, if the low byte is not equal to 0, take it. Otherwise, take the high byte.
$passUtf8 = mb_convert_encoding($password, 'UCS-2LE', 'UTF-8');

if (!is_string($passUtf8)) {
throw new InvalidArgumentException('mb_convert_encoding() failed to convert password to UCS-2LE.');
}

$byteChars = [];

for ($i = 0; $i < mb_strlen($password); ++$i) {
Expand Down
3 changes: 3 additions & 0 deletions src/PhpWord/Shared/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ public static function toUTF8($value = '')
if (null !== $value && !self::isUTF8($value)) {
// PHP8.2 : utf8_encode is deprecated, but mb_convert_encoding always usable
$value = (function_exists('mb_convert_encoding')) ? mb_convert_encoding($value, 'UTF-8', 'ISO-8859-1') : utf8_encode($value);
if (is_bool($value)) {
$value = null;
}
}

return $value;
Expand Down
3 changes: 1 addition & 2 deletions src/PhpWord/Style/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,7 @@ public function getValueLabelPosition()
* "low" - sets labels are below the graph
* "high" - sets labels above the graph.
*
* @param string
* @param mixed $labelPosition
* @param string $labelPosition
*/
public function setValueLabelPosition($labelPosition)
{
Expand Down
Loading
Loading