Skip to content

Commit e27128c

Browse files
committed
Support for Drawing (SVG format)
1 parent 6ecc77c commit e27128c

File tree

26 files changed

+3513
-307
lines changed

26 files changed

+3513
-307
lines changed

docs/changes/1.0.0.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@
6868
- Support for RadarChart - [@Progi1984](https://github.com/Progi1984) GH-253
6969
- ODPresentation Writer
7070
- PowerPoint2007 Writer
71+
- Support for Drawing (SVG format) - [@Aggiekev](https://github.com/Aggiekev) GH-531 & [@Progi1984](https://github.com/Progi1984) GH-666
72+
- ODPresentation Reader
73+
- ODPresentation Writer
74+
- PowerPoint2007 Reader
75+
- PowerPoint2007 Writer
7176

7277
## Project Management
7378
- Migrated from Travis CI to Github Actions - [@Progi1984](https://github.com/Progi1984) GH-635

docs/usage/shapes/drawing.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
# Drawing
22

3-
To create a drawing, you have four sources : File, GD, Base64 and ZipFile.
3+
To create a drawing, you have multiples sources :
4+
5+
- Base64
6+
- File
7+
- GD
8+
- ZipFile
9+
10+
You can add multiples formats of image :
11+
12+
- GIF
13+
- JPEG
14+
- PNG
15+
- SVG
416

517
## File
618

samples/Sample_03_Image.php

Lines changed: 38 additions & 25 deletions
Large diffs are not rendered by default.

samples/resources/base64.txt

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

samples/resources/tiger.svg

Lines changed: 726 additions & 0 deletions
Loading

src/PhpPresentation/Reader/ODPresentation.php

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use PhpOffice\PhpPresentation\DocumentProperties;
2626
use PhpOffice\PhpPresentation\PhpPresentation;
2727
use PhpOffice\PhpPresentation\PresentationProperties;
28+
use PhpOffice\PhpPresentation\Shape\Drawing\Base64;
2829
use PhpOffice\PhpPresentation\Shape\Drawing\Gd;
2930
use PhpOffice\PhpPresentation\Shape\RichText;
3031
use PhpOffice\PhpPresentation\Shape\RichText\Paragraph;
@@ -532,42 +533,55 @@ protected function loadSlide(DOMElement $nodeSlide): bool
532533
protected function loadShapeDrawing(DOMElement $oNodeFrame): void
533534
{
534535
// Core
535-
$oShape = new Gd();
536-
$oShape->getShadow()->setVisible(false);
536+
$mimetype = '';
537537

538538
$oNodeImage = $this->oXMLReader->getElement('draw:image', $oNodeFrame);
539539
if ($oNodeImage instanceof DOMElement) {
540+
if ($oNodeImage->hasAttribute('loext:mime-type')) {
541+
$mimetype = $oNodeImage->getAttribute('loext:mime-type');
542+
}
540543
if ($oNodeImage->hasAttribute('xlink:href')) {
541544
$sFilename = $oNodeImage->getAttribute('xlink:href');
542545
// svm = StarView Metafile
543546
if ('svm' == pathinfo($sFilename, PATHINFO_EXTENSION)) {
544547
return;
545548
}
546549
$imageFile = $this->oZip->getFromName($sFilename);
547-
if (!empty($imageFile)) {
548-
$oShape->setImageResource(imagecreatefromstring($imageFile));
549-
}
550550
}
551551
}
552552

553-
$oShape->setName($oNodeFrame->hasAttribute('draw:name') ? $oNodeFrame->getAttribute('draw:name') : '');
554-
$oShape->setDescription($oNodeFrame->hasAttribute('draw:name') ? $oNodeFrame->getAttribute('draw:name') : '');
555-
$oShape->setResizeProportional(false);
556-
$oShape->setWidth($oNodeFrame->hasAttribute('svg:width') ? CommonDrawing::centimetersToPixels((float) substr($oNodeFrame->getAttribute('svg:width'), 0, -2)) : 0);
557-
$oShape->setHeight($oNodeFrame->hasAttribute('svg:height') ? CommonDrawing::centimetersToPixels((float) substr($oNodeFrame->getAttribute('svg:height'), 0, -2)) : 0);
558-
$oShape->setResizeProportional(true);
559-
$oShape->setOffsetX($oNodeFrame->hasAttribute('svg:x') ? CommonDrawing::centimetersToPixels((float) substr($oNodeFrame->getAttribute('svg:x'), 0, -2)) : 0);
560-
$oShape->setOffsetY($oNodeFrame->hasAttribute('svg:y') ? CommonDrawing::centimetersToPixels((float) substr($oNodeFrame->getAttribute('svg:y'), 0, -2)) : 0);
553+
if (empty($imageFile)) {
554+
return;
555+
}
556+
557+
// Contents of file
558+
if (empty($mimetype)) {
559+
$shape = new Gd();
560+
$shape->setImageResource(imagecreatefromstring($imageFile));
561+
} else {
562+
$shape = new Base64();
563+
$shape->setData('data:' . $mimetype . ';base64,' . base64_encode($imageFile));
564+
}
565+
566+
$shape->getShadow()->setVisible(false);
567+
$shape->setName($oNodeFrame->hasAttribute('draw:name') ? $oNodeFrame->getAttribute('draw:name') : '');
568+
$shape->setDescription($oNodeFrame->hasAttribute('draw:name') ? $oNodeFrame->getAttribute('draw:name') : '');
569+
$shape->setResizeProportional(false);
570+
$shape->setWidth($oNodeFrame->hasAttribute('svg:width') ? CommonDrawing::centimetersToPixels((float) substr($oNodeFrame->getAttribute('svg:width'), 0, -2)) : 0);
571+
$shape->setHeight($oNodeFrame->hasAttribute('svg:height') ? CommonDrawing::centimetersToPixels((float) substr($oNodeFrame->getAttribute('svg:height'), 0, -2)) : 0);
572+
$shape->setResizeProportional(true);
573+
$shape->setOffsetX($oNodeFrame->hasAttribute('svg:x') ? CommonDrawing::centimetersToPixels((float) substr($oNodeFrame->getAttribute('svg:x'), 0, -2)) : 0);
574+
$shape->setOffsetY($oNodeFrame->hasAttribute('svg:y') ? CommonDrawing::centimetersToPixels((float) substr($oNodeFrame->getAttribute('svg:y'), 0, -2)) : 0);
561575

562576
if ($oNodeFrame->hasAttribute('draw:style-name')) {
563577
$keyStyle = $oNodeFrame->getAttribute('draw:style-name');
564578
if (isset($this->arrayStyles[$keyStyle])) {
565-
$oShape->setShadow($this->arrayStyles[$keyStyle]['shadow']);
566-
$oShape->setFill($this->arrayStyles[$keyStyle]['fill']);
579+
$shape->setShadow($this->arrayStyles[$keyStyle]['shadow']);
580+
$shape->setFill($this->arrayStyles[$keyStyle]['fill']);
567581
}
568582
}
569583

570-
$this->oPhpPresentation->getActiveSlide()->addShape($oShape);
584+
$this->oPhpPresentation->getActiveSlide()->addShape($shape);
571585
}
572586

573587
/**

src/PhpPresentation/Reader/PowerPoint2007.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use PhpOffice\PhpPresentation\DocumentProperties;
2929
use PhpOffice\PhpPresentation\PhpPresentation;
3030
use PhpOffice\PhpPresentation\PresentationProperties;
31+
use PhpOffice\PhpPresentation\Shape\Drawing\Base64;
3132
use PhpOffice\PhpPresentation\Shape\Drawing\Gd;
3233
use PhpOffice\PhpPresentation\Shape\Placeholder;
3334
use PhpOffice\PhpPresentation\Shape\RichText;
@@ -777,7 +778,12 @@ protected function loadSlideNote(string $baseFile, Slide $oSlide): void
777778
protected function loadShapeDrawing(XMLReader $document, DOMElement $node, AbstractSlide $oSlide): void
778779
{
779780
// Core
780-
$oShape = new Gd();
781+
$document->registerNamespace('asvg', 'http://schemas.microsoft.com/office/drawing/2016/SVG/main');
782+
if ($document->getElement('p:blipFill/a:blip/a:extLst/a:ext/asvg:svgBlip', $node)) {
783+
$oShape = new Base64();
784+
} else {
785+
$oShape = new Gd();
786+
}
781787
$oShape->getShadow()->setVisible(false);
782788
// Variables
783789
$fileRels = $oSlide->getRelsIndex();
@@ -813,10 +819,14 @@ protected function loadShapeDrawing(XMLReader $document, DOMElement $node, Abstr
813819
$pathImage = implode('/', $pathImage);
814820
$imageFile = $this->oZip->getFromName($pathImage);
815821
if (!empty($imageFile)) {
816-
$info = getimagesizefromstring($imageFile);
817-
$oShape->setMimeType($info['mime']);
818-
$oShape->setRenderingFunction(str_replace('/', '', $info['mime']));
819-
$oShape->setImageResource(imagecreatefromstring($imageFile));
822+
if ($oShape instanceof Gd) {
823+
$info = getimagesizefromstring($imageFile);
824+
$oShape->setMimeType($info['mime']);
825+
$oShape->setRenderingFunction(str_replace('/', '', $info['mime']));
826+
$oShape->setImageResource(imagecreatefromstring($imageFile));
827+
} elseif ($oShape instanceof Base64) {
828+
$oShape->setData('data:image/svg+xml;base64,' . base64_encode($imageFile));
829+
}
820830
}
821831
}
822832
}

src/PhpPresentation/Shape/Drawing/Base64.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Base64 extends AbstractDrawingAdapter
2323
'image/jpeg' => 'jpg',
2424
'image/png' => 'png',
2525
'image/gif' => 'gif',
26+
'image/svg+xml' => 'svg',
2627
];
2728

2829
/**
@@ -85,6 +86,13 @@ public function getIndexedFilename(): string
8586

8687
public function getMimeType(): string
8788
{
89+
list($data) = explode(';', $this->getData());
90+
list(, $mime) = explode(':', $data);
91+
92+
if (!empty($mime)) {
93+
return $mime;
94+
}
95+
8896
$sImage = $this->getContents();
8997
if (!function_exists('getimagesizefromstring')) {
9098
$uri = 'data://application/octet-stream;base64,' . base64_encode($sImage);

src/PhpPresentation/Shape/Drawing/File.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ public function getMimeType(): string
6767
}
6868
$image = getimagesizefromstring(CommonFile::fileGetContents($this->getPath()));
6969

70-
return image_type_to_mime_type($image[2]);
70+
if (is_array($image)) {
71+
return image_type_to_mime_type($image[2]);
72+
}
73+
74+
return mime_content_type($this->getPath());
7175
}
7276

7377
public function getIndexedFilename(): string

src/PhpPresentation/Slide/Background/Image.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function getFilename()
8080
*
8181
* @return string
8282
*/
83-
public function getExtension()
83+
public function getExtension(): string
8484
{
8585
$exploded = explode('.', basename($this->path));
8686

0 commit comments

Comments
 (0)