Completed
Pull Request — develop (#197)
by Franck
08:11
created

MetaInfManifest::getImageMimeType()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 36
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 7.4822

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 36
ccs 22
cts 28
cp 0.7856
rs 6.7272
cc 7
eloc 28
nc 7
nop 1
crap 7.4822
1
<?php
2
3
namespace PhpOffice\PhpPresentation\Writer\ODPresentation;
4
5
use PhpOffice\Common\Adapter\Zip\ZipInterface;
6
use PhpOffice\Common\File;
7
use PhpOffice\Common\XMLWriter;
8
use PhpOffice\PhpPresentation\Shape\Drawing as ShapeDrawing;
9
use PhpOffice\PhpPresentation\Shape\MemoryDrawing;
10
use PhpOffice\PhpPresentation\Slide\Background\Image;
11
use PhpOffice\PhpPresentation\Writer\ODPresentation;
12
13
class MetaInfManifest extends AbstractDecoratorWriter
14
{
15
    /**
16
     * @return ZipInterface
17
     */
18 59
    public function render()
19
    {
20
        // Create XML writer
21 59
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
22
23
        // XML header
24 59
        $objWriter->startDocument('1.0', 'UTF-8');
25
26
        // manifest:manifest
27 59
        $objWriter->startElement('manifest:manifest');
28 59
        $objWriter->writeAttribute('xmlns:manifest', 'urn:oasis:names:tc:opendocument:xmlns:manifest:1.0');
29 59
        $objWriter->writeAttribute('manifest:version', '1.2');
30
31
        // manifest:file-entry
32 59
        $objWriter->startElement('manifest:file-entry');
33 59
        $objWriter->writeAttribute('manifest:media-type', 'application/vnd.oasis.opendocument.presentation');
34 59
        $objWriter->writeAttribute('manifest:full-path', '/');
35 59
        $objWriter->writeAttribute('manifest:version', '1.2');
36 59
        $objWriter->endElement();
37
        // manifest:file-entry
38 59
        $objWriter->startElement('manifest:file-entry');
39 59
        $objWriter->writeAttribute('manifest:media-type', 'text/xml');
40 59
        $objWriter->writeAttribute('manifest:full-path', 'content.xml');
41 59
        $objWriter->endElement();
42
        // manifest:file-entry
43 59
        $objWriter->startElement('manifest:file-entry');
44 59
        $objWriter->writeAttribute('manifest:media-type', 'text/xml');
45 59
        $objWriter->writeAttribute('manifest:full-path', 'meta.xml');
46 59
        $objWriter->endElement();
47
        // manifest:file-entry
48 59
        $objWriter->startElement('manifest:file-entry');
49 59
        $objWriter->writeAttribute('manifest:media-type', 'text/xml');
50 59
        $objWriter->writeAttribute('manifest:full-path', 'styles.xml');
51 59
        $objWriter->endElement();
52
53
        // Charts
54 59
        foreach ($this->getArrayChart() as $key => $shape) {
55 21
            $objWriter->startElement('manifest:file-entry');
56 21
            $objWriter->writeAttribute('manifest:media-type', 'application/vnd.oasis.opendocument.chart');
57 21
            $objWriter->writeAttribute('manifest:full-path', 'Object '.$key.'/');
58 21
            $objWriter->endElement();
59 21
            $objWriter->startElement('manifest:file-entry');
60 21
            $objWriter->writeAttribute('manifest:media-type', 'text/xml');
61 21
            $objWriter->writeAttribute('manifest:full-path', 'Object '.$key.'/content.xml');
62 21
            $objWriter->endElement();
63 59
        }
64
65 59
        $arrMedia = array();
66 59
        for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) {
67 30
            $shape = $this->getDrawingHashTable()->getByIndex($i);
68 30
            if ($shape instanceof ShapeDrawing) {
69 9
                if (!in_array(md5($shape->getPath()), $arrMedia)) {
70 9
                    $arrMedia[] = md5($shape->getPath());
71 9
                    $mimeType   = $this->getImageMimeType($shape->getPath());
72
73 7
                    $objWriter->startElement('manifest:file-entry');
74 7
                    $objWriter->writeAttribute('manifest:media-type', $mimeType);
75 7
                    $objWriter->writeAttribute('manifest:full-path', 'Pictures/' . md5($shape->getPath()) . '.' . $shape->getExtension());
76 7
                    $objWriter->endElement();
77 7
                }
78 28
            } elseif ($shape instanceof MemoryDrawing) {
79 1
                if (!in_array(str_replace(' ', '_', $shape->getIndexedFilename()), $arrMedia)) {
80 1
                    $arrMedia[] = str_replace(' ', '_', $shape->getIndexedFilename());
81 1
                    $mimeType = $shape->getMimeType();
82
83 1
                    $objWriter->startElement('manifest:file-entry');
84 1
                    $objWriter->writeAttribute('manifest:media-type', $mimeType);
85 1
                    $objWriter->writeAttribute('manifest:full-path', 'Pictures/' . str_replace(' ', '_', $shape->getIndexedFilename()));
86 1
                    $objWriter->endElement();
87 1
                }
88 1
            }
89 28
        }
90
91 57
        foreach ($this->getPresentation()->getAllSlides() as $numSlide => $oSlide) {
92 57
            $oBkgImage = $oSlide->getBackground();
93 57
            if ($oBkgImage instanceof Image) {
94 1
                $mimeType   = $this->getImageMimeType($oBkgImage->getPath());
95
96 1
                $objWriter->startElement('manifest:file-entry');
97 1
                $objWriter->writeAttribute('manifest:media-type', $mimeType);
98 1
                $objWriter->writeAttribute('manifest:full-path', 'Pictures/' . str_replace(' ', '_', $oBkgImage->getIndexedFilename($numSlide)));
99 1
                $objWriter->endElement();
100 1
            }
101 57
        }
102
103 57
        if ($this->getPresentation()->getPresentationProperties()->getThumbnailPath()) {
104 1
            $pathThumbnail = $this->getPresentation()->getPresentationProperties()->getThumbnailPath();
105
            // Size : 128x128 pixel
106
            // PNG : 8bit, non-interlaced with full alpha transparency
107 1
            $gdImage = imagecreatefromstring(file_get_contents($pathThumbnail));
108 1
            if ($gdImage) {
109 1
                imagedestroy($gdImage);
110 1
                $objWriter->startElement('manifest:file-entry');
111 1
                $objWriter->writeAttribute('manifest:media-type', 'image/png');
112 1
                $objWriter->writeAttribute('manifest:full-path', 'Thumbnails/thumbnail.png');
113 1
                $objWriter->endElement();
114 1
            }
115 1
        }
116
117 57
        $objWriter->endElement();
118
119 57
        $this->getZip()->addFromString('META-INF/manifest.xml', $objWriter->getData());
120 57
        return $this->getZip();
121
    }
122
123
    /**
124
     * Get image mime type
125
     *
126
     * @param  string    $pFile Filename
127
     * @return string    Mime Type
128
     * @throws \Exception
129
     * @todo PowerPoint2007\ContentTypes duplicate Code : getImageMimeType
130
     */
131 10
    private function getImageMimeType($pFile = '')
132
    {
133 10
        if (strpos($pFile, 'zip://') === 0) {
134 2
            $pZIPFile = str_replace('zip://', '', $pFile);
135 2
            $pZIPFile = substr($pZIPFile, 0, strpos($pZIPFile, '#'));
136 2
            if (!File::fileExists($pZIPFile)) {
137 1
                throw new \Exception("File $pFile does not exist");
138
            }
139 1
            $pImgFile = substr($pFile, strpos($pFile, '#') + 1);
140 1
            $oArchive = new \ZipArchive();
141 1
            $oArchive->open($pZIPFile);
142 1
            if (!function_exists('getimagesizefromstring')) {
143
                $uri = 'data://application/octet-stream;base64,' . base64_encode($oArchive->getFromName($pImgFile));
144
                $image = getimagesize($uri);
145
            } else {
146 1
                $image = getimagesizefromstring($oArchive->getFromName($pImgFile));
147
            }
148 9
        } elseif (strpos($pFile, 'data:image/') === 0) {
149 1
            $sImage = $pFile;
150 1
            list(, $sImage) = explode(';', $sImage);
151 1
            list(, $sImage) = explode(',', $sImage);
152 1
            if (!function_exists('getimagesizefromstring')) {
153
                $uri = 'data://application/octet-stream;base64,' . base64_encode($sImage);
154
                $image = getimagesize($uri);
155
            } else {
156 1
                $image = getimagesizefromstring($sImage);
157
            }
158 1
        } else {
159 7
            if (!File::fileExists($pFile)) {
160 1
                throw new \Exception("File $pFile does not exist");
161
            }
162 6
            $image = getimagesize($pFile);
163
        }
164
165 8
        return image_type_to_mime_type($image[2]);
166
    }
167
}
168