Completed
Pull Request — develop (#209)
by Franck
07:03
created

ThumbnailsThumbnail::render()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
ccs 0
cts 25
cp 0
rs 8.8571
cc 3
eloc 21
nc 3
nop 0
crap 12
1
<?php
2
3
namespace PhpOffice\PhpPresentation\Writer\ODPresentation;
4
5
class ThumbnailsThumbnail extends AbstractDecoratorWriter
6
{
7
    /**
8
     * @return ZipInterface
9
     */
10
    public function render()
11
    {
12
        $pathThumbnail = $this->getPresentation()->getPresentationProperties()->getThumbnailPath();
13
        if ($pathThumbnail) {
14
            // Size : 128x128 pixel
15
            // PNG : 8bit, non-interlaced with full alpha transparency
16
            $gdImage = imagecreatefromstring(file_get_contents($pathThumbnail));
17
            if ($gdImage) {
18
                list($width, $height) = getimagesize($pathThumbnail);
19
20
                $gdRender = imagecreatetruecolor(128, 128);
21
                $colorBgAlpha = imagecolorallocatealpha($gdRender, 0, 0, 0, 127);
22
                imagecolortransparent($gdRender, $colorBgAlpha);
23
                imagefill($gdRender, 0, 0, $colorBgAlpha);
24
                imagecopyresampled($gdRender, $gdImage, 0, 0, 0, 0, 128, 128, $width, $height);
25
                imagetruecolortopalette($gdRender, false, 255);
26
                imagesavealpha($gdRender, true);
27
28
                ob_start();
29
                imagepng($gdRender);
30
                $imageContents = ob_get_contents();
31
                ob_end_clean();
32
33
                imagedestroy($gdRender);
34
                imagedestroy($gdImage);
35
36
                $this->getZip()->addFromString('Thumbnails/thumbnail.png', $imageContents);
37
            }
38
        }
39
        return $this->getZip();
40
    }
41
}
42