Completed
Pull Request — develop (#207)
by Franck
16:14 queued 07:46
created

Base64::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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