Skip to content

Commit 1597116

Browse files
committed
Merge pull request #193 from Progi1984/issue168
#168 : Support for images in base64
2 parents 985c508 + 4ac4b73 commit 1597116

File tree

9 files changed

+159
-22
lines changed

9 files changed

+159
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- ODPresentation & PowerPoint2007 Writer : Add support for Comment - @Progi1984 GH-116
1616
- ODPresentation & PowerPoint2007 Writer : Thumbnail of the presentation - @Progi1984 GH-125
1717
- ODPresentation & PowerPoint2007 Writer : Add support for Gridlines in Chart - @Progi1984 GH-129
18+
- ODPresentation & PowerPoint2007 Writer : Support for images in base 64 - @Progi1984 GH-168
1819
- ODPresentation & PowerPoint2007 Writer : Marker of Series in Line & Scatter chart is customizable - @Progi1984 GH-169
1920
- ODPresentation & PowerPoint2007 Writer : Outline of Series in Line & Scatter chart is customizable - @Progi1984 GH-169
2021
- PowerPoint2007 & Serialized Writer : Support for Zip Adapter - @Progi1984 GH-176

samples/Sample_03_Image.php

Lines changed: 17 additions & 5 deletions
Large diffs are not rendered by default.
6.96 MB
Binary file not shown.

src/PhpPresentation/Writer/ODPresentation.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ public function save($pFilename)
247247
$imageContents = $imageZip->getFromName($imagePathSplitted[1]);
248248
$imageZip->close();
249249
unset($imageZip);
250+
} elseif (strpos($imagePath, 'data:image/') === 0) {
251+
list(, $imageContents) = explode(';', $imagePath);
252+
list(, $imageContents) = explode(',', $imageContents);
253+
$imageContents = base64_decode($imageContents);
250254
} else {
251255
$imageContents = file_get_contents($imagePath);
252256
}

src/PhpPresentation/Writer/ODPresentation/Manifest.php

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -151,29 +151,42 @@ public function writePart()
151151
* @param string $pFile Filename
152152
* @return string Mime Type
153153
* @throws \Exception
154+
* @todo PowerPoint2007\ContentTypes duplicate Code : getImageMimeType
154155
*/
155156
private function getImageMimeType($pFile = '')
156157
{
157-
if (File::fileExists($pFile)) {
158-
if (strpos($pFile, 'zip://') === 0) {
159-
$pZIPFile = str_replace('zip://', '', $pFile);
160-
$pZIPFile = substr($pZIPFile, 0, strpos($pZIPFile, '#'));
161-
$pImgFile = substr($pFile, strpos($pFile, '#') + 1);
162-
$oArchive = new \ZipArchive();
163-
$oArchive->open($pZIPFile);
164-
if (!function_exists('getimagesizefromstring')) {
165-
$uri = 'data://application/octet-stream;base64,' . base64_encode($oArchive->getFromName($pImgFile));
166-
$image = getimagesize($uri);
167-
} else {
168-
$image = getimagesizefromstring($oArchive->getFromName($pImgFile));
169-
}
158+
if (strpos($pFile, 'zip://') === 0) {
159+
$pZIPFile = str_replace('zip://', '', $pFile);
160+
$pZIPFile = substr($pZIPFile, 0, strpos($pZIPFile, '#'));
161+
if (!File::fileExists($pZIPFile)) {
162+
throw new \Exception("File $pFile does not exist");
163+
}
164+
$pImgFile = substr($pFile, strpos($pFile, '#') + 1);
165+
$oArchive = new \ZipArchive();
166+
$oArchive->open($pZIPFile);
167+
if (!function_exists('getimagesizefromstring')) {
168+
$uri = 'data://application/octet-stream;base64,' . base64_encode($oArchive->getFromName($pImgFile));
169+
$image = getimagesize($uri);
170170
} else {
171-
$image = getimagesize($pFile);
171+
$image = getimagesizefromstring($oArchive->getFromName($pImgFile));
172+
}
173+
} elseif (strpos($pFile, 'data:image/') === 0) {
174+
$sImage = $pFile;
175+
list(, $sImage) = explode(';', $sImage);
176+
list(, $sImage) = explode(',', $sImage);
177+
if (!function_exists('getimagesizefromstring')) {
178+
$uri = 'data://application/octet-stream;base64,' . base64_encode($sImage);
179+
$image = getimagesize($uri);
180+
} else {
181+
$image = getimagesizefromstring($sImage);
172182
}
173-
174-
return image_type_to_mime_type($image[2]);
175183
} else {
176-
throw new \Exception("File $pFile does not exist");
184+
if (!File::fileExists($pFile)) {
185+
throw new \Exception("File $pFile does not exist");
186+
}
187+
$image = getimagesize($pFile);
177188
}
189+
190+
return image_type_to_mime_type($image[2]);
178191
}
179192
}

src/PhpPresentation/Writer/PowerPoint2007/ContentTypes.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,16 @@ private function getImageMimeType($pFile = '')
190190
} else {
191191
$image = getimagesizefromstring($oArchive->getFromName($pImgFile));
192192
}
193+
} elseif (strpos($pFile, 'data:image/') === 0) {
194+
$sImage = $pFile;
195+
list(, $sImage) = explode(';', $sImage);
196+
list(, $sImage) = explode(',', $sImage);
197+
if (!function_exists('getimagesizefromstring')) {
198+
$uri = 'data://application/octet-stream;base64,' . base64_encode($sImage);
199+
$image = getimagesize($uri);
200+
} else {
201+
$image = getimagesizefromstring($sImage);
202+
}
193203
} else {
194204
if (!File::fileExists($pFile)) {
195205
throw new \Exception("File $pFile does not exist");

src/PhpPresentation/Writer/PowerPoint2007/PptMedia.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public function render()
2525
$imageContents = $imageZip->getFromName($imagePathSplitted[1]);
2626
$imageZip->close();
2727
unset($imageZip);
28+
} elseif (strpos($imagePath, 'data:image/') === 0) {
29+
list(, $imageContents) = explode(';', $imagePath);
30+
list(, $imageContents) = explode(',', $imageContents);
31+
$imageContents = base64_decode($imageContents);
2832
} else {
2933
$imageContents = file_get_contents($imagePath);
3034
}

tests/PhpPresentation/Tests/Writer/ODPresentation/ManifestTest.php

Lines changed: 54 additions & 0 deletions
Large diffs are not rendered by default.

tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptMediaTest.php

Lines changed: 39 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)