Completed
Push — develop ( 312b7a...53bbae )
by Franck
16s
created

Base64::getIndexedFilename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace PhpOffice\PhpPresentation\Shape\Drawing;
4
5
class Base64 extends AbstractDrawingAdapter
6
{
7
    /**
8
     * @var string
9
     */
10
    protected $data;
11
12
    /**
13
     * Unique name
14
     *
15
     * @var string
16
     */
17
    protected $uniqueName;
18
19
    /**
20
     * @var array
21
     */
22
    protected $arrayMimeExtension = array(
23
        'image/jpeg' => 'jpg',
24
    );
25
26
    /**
27
     * Base64 constructor.
28
     */
29 3
    public function __construct()
30
    {
31 3
        parent::__construct();
32 3
        $this->uniqueName = md5(rand(0, 9999) . time() . rand(0, 9999));
33 3
    }
34
35
    /**
36
     * @return mixed
37
     */
38 3
    public function getData()
39
    {
40 3
        return $this->data;
41
    }
42
43
    /**
44
     * @param mixed $data
45
     * @return Base64
46
     */
47 3
    public function setData($data)
48
    {
49 3
        $this->data = $data;
50 3
        return $this;
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getContents()
57
    {
58
        list(, $imageContents) = explode(';', $this->getData());
59
        list(, $imageContents) = explode(',', $imageContents);
60
        return base64_decode($imageContents);
61
    }
62
63
    /**
64
     * @return string
65
     * @throws \Exception
66
     */
67 2
    public function getExtension()
68
    {
69 2
        list($data, ) = explode(';', $this->getData());
70 2
        list(, $mime) = explode(':', $data);
71
72 2
        if (!array_key_exists($mime, $this->arrayMimeExtension)) {
73 1
            throw new \Exception('Type Mime not found : "'.$mime.'"');
74
        }
75 1
        return $this->arrayMimeExtension[$mime];
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    public function getIndexedFilename()
82
    {
83
        return $this->uniqueName . $this->getImageIndex() . '.' . $this->getExtension();
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function getMimeType()
90
    {
91
        $sImage = $this->getContents();
92
        if (!function_exists('getimagesizefromstring')) {
93
            $uri = 'data://application/octet-stream;base64,' . base64_encode($sImage);
94
            $image = getimagesize($uri);
95
        } else {
96
            $image = getimagesizefromstring($sImage);
97
        }
98
        return image_type_to_mime_type($image[2]);
99
    }
100
}
101