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

Gd::setRenderingFunction()   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 1
1
<?php
2
3
namespace PhpOffice\PhpPresentation\Shape\Drawing;
4
5
class Gd extends AbstractDrawingAdapter
6
{
7
    /* Rendering functions */
8
    const RENDERING_DEFAULT = 'imagepng';
9
    const RENDERING_PNG = 'imagepng';
10
    const RENDERING_GIF = 'imagegif';
11
    const RENDERING_JPEG = 'imagejpeg';
12
13
    /* MIME types */
14
    const MIMETYPE_DEFAULT = 'image/png';
15
    const MIMETYPE_PNG = 'image/png';
16
    const MIMETYPE_GIF = 'image/gif';
17
    const MIMETYPE_JPEG = 'image/jpeg';
18
19
    /**
20
     * Image resource
21
     *
22
     * @var resource
23
     */
24
    protected $imageResource;
25
26
    /**
27
     * Rendering function
28
     *
29
     * @var string
30
     */
31
    protected $renderingFunction = self::RENDERING_DEFAULT;
32
33
    /**
34
     * Mime type
35
     *
36
     * @var string
37
     */
38
    protected $mimeType = self::MIMETYPE_DEFAULT;
39
40
    /**
41
     * Unique name
42
     *
43
     * @var string
44
     */
45
    protected $uniqueName;
46
47
    /**
48
     * Gd constructor
49
     */
50
    public function __construct()
51
    {
52
        parent::__construct();
53
        $this->uniqueName = md5(rand(0, 9999) . time() . rand(0, 9999));
54
    }
55
56
    /**
57
     * Get image resource
58
     *
59
     * @return resource
60
     */
61
    public function getImageResource()
62
    {
63
        return $this->imageResource;
64
    }
65
66
    /**
67
     * Set image resource
68
     *
69
     * @param $value resource
70
     * @return $this
71
     */
72
    public function setImageResource($value = null)
73
    {
74
        $this->imageResource = $value;
75
76
        if (!is_null($this->imageResource)) {
77
            // Get width/height
78
            $this->width  = imagesx($this->imageResource);
79
            $this->height = imagesy($this->imageResource);
80
        }
81
82
        return $this;
83
    }
84
85
    /**
86
     * Get rendering function
87
     *
88
     * @return string
89
     */
90
    public function getRenderingFunction()
91
    {
92
        return $this->renderingFunction;
93
    }
94
95
    /**
96
     * Set rendering function
97
     *
98
     * @param  string                            $value
99
     * @return $this
100
     */
101
    public function setRenderingFunction($value = self::RENDERING_DEFAULT)
102
    {
103
        $this->renderingFunction = $value;
104
        return $this;
105
    }
106
107
    /**
108
     * Get mime type
109
     *
110
     * @return string
111
     */
112
    public function getMimeType()
113
    {
114
        return $this->mimeType;
115
    }
116
117
    /**
118
     * Set mime type
119
     *
120
     * @param  string $value
121
     * @return $this
122
     */
123
    public function setMimeType($value = self::MIMETYPE_DEFAULT)
124
    {
125
        $this->mimeType = $value;
126
        return $this;
127
    }
128
129
    /**
130
     * @return string
131
     */
132
    public function getContents()
133
    {
134
        ob_start();
135
        call_user_func($this->getRenderingFunction(), $this->getImageResource());
136
        $imageContents = ob_get_contents();
137
        ob_end_clean();
138
        return $imageContents;
139
    }
140
141
    /**
142
     * @return string
143
     */
144
    public function getExtension()
145
    {
146
        $extension = strtolower($this->getMimeType());
147
        $extension = explode('/', $extension);
148
        $extension = $extension[1];
149
        return $extension;
150
    }
151
152
    /**
153
     * @return string
154
     */
155
    public function getIndexedFilename()
156
    {
157
        return $this->uniqueName . $this->getImageIndex() . '.' . $this->getExtension();
158
    }
159
}