Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
67.86% covered (warning)
67.86%
19 / 28
66.67% covered (warning)
66.67%
6 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Base64
67.86% covered (warning)
67.86%
19 / 28
66.67% covered (warning)
66.67%
6 / 9
16.78
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getData
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setData
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getContents
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getExtension
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getIndexedFilename
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMimeType
40.00% covered (danger)
40.00%
4 / 10
0.00% covered (danger)
0.00%
0 / 1
4.94
 getPath
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setPath
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * This file is part of PHPPresentation - A pure PHP library for reading and writing
4 * presentations documents.
5 *
6 * PHPPresentation is free software distributed under the terms of the GNU Lesser
7 * General Public License version 3 as published by the Free Software Foundation.
8 *
9 * For the full copyright and license information, please read the LICENSE
10 * file that was distributed with this source code. For the full list of
11 * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
12 *
13 * @see        https://github.com/PHPOffice/PHPPresentation
14 *
15 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16 */
17
18declare(strict_types=1);
19
20namespace PhpOffice\PhpPresentation\Shape\Drawing;
21
22use PhpOffice\PhpPresentation\Exception\UnauthorizedMimetypeException;
23
24class Base64 extends AbstractDrawingAdapter
25{
26    /**
27     * @var string
28     */
29    protected $data;
30
31    /**
32     * Unique name.
33     *
34     * @var string
35     */
36    protected $uniqueName;
37
38    /**
39     * @var array<string, string>
40     */
41    protected $arrayMimeExtension = [
42        'image/jpeg' => 'jpg',
43        'image/png' => 'png',
44        'image/gif' => 'gif',
45        'image/svg+xml' => 'svg',
46    ];
47
48    /**
49     * @var string
50     */
51    protected $path;
52
53    /**
54     * Base64 constructor.
55     */
56    public function __construct()
57    {
58        parent::__construct();
59        $this->uniqueName = md5(mt_rand(0, 9999) . time() . mt_rand(0, 9999));
60        $this->data = '';
61    }
62
63    public function getData(): string
64    {
65        return $this->data;
66    }
67
68    public function setData(string $data): self
69    {
70        $this->data = $data;
71
72        return $this;
73    }
74
75    public function getContents(): string
76    {
77        [, $imageContents] = explode(';', $this->getData());
78        [, $imageContents] = explode(',', $imageContents);
79
80        return base64_decode($imageContents);
81    }
82
83    public function getExtension(): string
84    {
85        [$data] = explode(';', $this->getData());
86        [, $mime] = explode(':', $data);
87
88        if (!array_key_exists($mime, $this->arrayMimeExtension)) {
89            throw new UnauthorizedMimetypeException($mime, $this->arrayMimeExtension);
90        }
91
92        return $this->arrayMimeExtension[$mime];
93    }
94
95    public function getIndexedFilename(): string
96    {
97        return $this->uniqueName . $this->getImageIndex() . '.' . $this->getExtension();
98    }
99
100    public function getMimeType(): string
101    {
102        [$data] = explode(';', $this->getData());
103        [, $mime] = explode(':', $data);
104
105        if (!empty($mime)) {
106            return $mime;
107        }
108
109        $sImage = $this->getContents();
110        if (!function_exists('getimagesizefromstring')) {
111            $uri = 'data://application/octet-stream;base64,' . base64_encode($sImage);
112            $image = getimagesize($uri);
113        } else {
114            $image = getimagesizefromstring($sImage);
115        }
116
117        return image_type_to_mime_type($image[2]);
118    }
119
120    /**
121     * Get Path.
122     */
123    public function getPath(): string
124    {
125        return $this->path;
126    }
127
128    public function setPath(string $path): self
129    {
130        $this->path = $path;
131
132        return $this;
133    }
134}