Passed
Push — master ( 8f7a40...e78bca )
by
unknown
16:52 queued 09:09
created

MemoryDrawing::__destruct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Worksheet;
4
5
use GdImage;
6
use PhpOffice\PhpSpreadsheet\Exception;
7
use PhpOffice\PhpSpreadsheet\Shared\File;
8
9
class MemoryDrawing extends BaseDrawing
10
{
11
    // Rendering functions
12
    const RENDERING_DEFAULT = 'imagepng';
13
    const RENDERING_PNG = 'imagepng';
14
    const RENDERING_GIF = 'imagegif';
15
    const RENDERING_JPEG = 'imagejpeg';
16
17
    // MIME types
18
    const MIMETYPE_DEFAULT = 'image/png';
19
    const MIMETYPE_PNG = 'image/png';
20
    const MIMETYPE_GIF = 'image/gif';
21
    const MIMETYPE_JPEG = 'image/jpeg';
22
23
    const SUPPORTED_MIME_TYPES = [
24
        self::MIMETYPE_GIF,
25
        self::MIMETYPE_JPEG,
26
        self::MIMETYPE_PNG,
27
    ];
28
29
    /**
30
     * Image resource.
31
     */
32
    private null|GdImage $imageResource = null;
33
34
    /**
35
     * Rendering function.
36
     *
37
     * @var callable-string
38
     */
39
    private string $renderingFunction;
40
41
    /**
42
     * Mime type.
43
     */
44
    private string $mimeType;
45
46
    /**
47
     * Unique name.
48
     */
49
    private string $uniqueName;
50
51
    /**
52
     * Create a new MemoryDrawing.
53
     */
54 25
    public function __construct()
55
    {
56
        // Initialise values
57 25
        $this->renderingFunction = self::RENDERING_DEFAULT;
58 25
        $this->mimeType = self::MIMETYPE_DEFAULT;
59 25
        $this->uniqueName = md5(mt_rand(0, 9999) . time() . mt_rand(0, 9999));
60
61
        // Initialize parent
62 25
        parent::__construct();
63
    }
64
65 9
    public function __destruct()
66
    {
67 9
        if ($this->imageResource) {
68 9
            @imagedestroy($this->imageResource);
69 9
            $this->imageResource = null;
70
        }
71 9
        $this->worksheet = null;
72
    }
73
74 1
    public function __clone()
75
    {
76 1
        parent::__clone();
77 1
        $this->cloneResource();
78
    }
79
80 1
    private function cloneResource(): void
81
    {
82 1
        if (!$this->imageResource) {
83
            return;
84
        }
85
86 1
        $width = (int) imagesx($this->imageResource);
87 1
        $height = (int) imagesy($this->imageResource);
88
89 1
        if (imageistruecolor($this->imageResource)) {
90 1
            $clone = imagecreatetruecolor($width, $height);
91 1
            if (!$clone) {
92
                throw new Exception('Could not clone image resource');
93
            }
94
95 1
            imagealphablending($clone, false);
96 1
            imagesavealpha($clone, true);
97
        } else {
98
            $clone = imagecreate($width, $height);
99
            if (!$clone) {
100
                throw new Exception('Could not clone image resource');
101
            }
102
103
            // If the image has transparency...
104
            $transparent = imagecolortransparent($this->imageResource);
105
            if ($transparent >= 0) {
106
                // Starting with Php8.0, next function throws rather than return false
107
                $rgb = imagecolorsforindex($this->imageResource, $transparent);
108
109
                imagesavealpha($clone, true);
110
                $color = imagecolorallocatealpha($clone, $rgb['red'], $rgb['green'], $rgb['blue'], $rgb['alpha']);
111
                if ($color === false) {
112
                    throw new Exception('Could not get image alpha color');
113
                }
114
115
                imagefill($clone, 0, 0, $color);
116
            }
117
        }
118
119
        //Create the Clone!!
120 1
        imagecopy($clone, $this->imageResource, 0, 0, 0, 0, $width, $height);
121
122 1
        $this->imageResource = $clone;
123
    }
124
125
    /**
126
     * @param resource $imageStream Stream data to be converted to a Memory Drawing
127
     *
128
     * @throws Exception
129
     */
130 1
    public static function fromStream($imageStream): self
131
    {
132 1
        $streamValue = stream_get_contents($imageStream);
133
134 1
        return self::fromString($streamValue);
135
    }
136
137
    /**
138
     * @param string $imageString String data to be converted to a Memory Drawing
139
     *
140
     * @throws Exception
141
     */
142 5
    public static function fromString(string $imageString): self
143
    {
144 5
        $gdImage = @imagecreatefromstring($imageString);
145 5
        if ($gdImage === false) {
146 1
            throw new Exception('Value cannot be converted to an image');
147
        }
148
149 4
        $mimeType = self::identifyMimeType($imageString);
150 4
        if (imageistruecolor($gdImage) || imagecolortransparent($gdImage) >= 0) {
151 4
            imagesavealpha($gdImage, true);
152
        }
153 4
        $renderingFunction = self::identifyRenderingFunction($mimeType);
154
155 4
        $drawing = new self();
156 4
        $drawing->setImageResource($gdImage);
157 4
        $drawing->setRenderingFunction($renderingFunction);
158 4
        $drawing->setMimeType($mimeType);
159
160 4
        return $drawing;
161
    }
162
163
    /** @return callable-string */
164 4
    private static function identifyRenderingFunction(string $mimeType): string
165
    {
166 4
        return match ($mimeType) {
167 2
            self::MIMETYPE_PNG => self::RENDERING_PNG,
168 2
            self::MIMETYPE_JPEG => self::RENDERING_JPEG,
169
            self::MIMETYPE_GIF => self::RENDERING_GIF,
170 4
            default => self::RENDERING_DEFAULT,
171 4
        };
172
    }
173
174
    /**
175
     * @throws Exception
176
     */
177 4
    private static function identifyMimeType(string $imageString): string
178
    {
179 4
        $temporaryFileName = File::temporaryFilename();
180 4
        file_put_contents($temporaryFileName, $imageString);
181
182 4
        $mimeType = self::identifyMimeTypeUsingExif($temporaryFileName);
183 4
        if ($mimeType !== null) {
184 4
            unlink($temporaryFileName);
185
186 4
            return $mimeType;
187
        }
188
189
        $mimeType = self::identifyMimeTypeUsingGd($temporaryFileName);
190
        if ($mimeType !== null) {
191
            unlink($temporaryFileName);
192
193
            return $mimeType;
194
        }
195
196
        unlink($temporaryFileName);
197
198
        return self::MIMETYPE_DEFAULT;
199
    }
200
201 4
    private static function identifyMimeTypeUsingExif(string $temporaryFileName): ?string
202
    {
203 4
        if (function_exists('exif_imagetype')) {
204 4
            $imageType = @exif_imagetype($temporaryFileName);
205 4
            $mimeType = ($imageType) ? image_type_to_mime_type($imageType) : null;
206
207 4
            return self::supportedMimeTypes($mimeType);
208
        }
209
210
        return null;
211
    }
212
213
    private static function identifyMimeTypeUsingGd(string $temporaryFileName): ?string
214
    {
215
        if (function_exists('getimagesize')) {
216
            $imageSize = @getimagesize($temporaryFileName);
217
            if (is_array($imageSize)) {
218
                $mimeType = $imageSize['mime'];
219
220
                return self::supportedMimeTypes($mimeType);
221
            }
222
        }
223
224
        return null;
225
    }
226
227 4
    private static function supportedMimeTypes(?string $mimeType = null): ?string
228
    {
229 4
        if (in_array($mimeType, self::SUPPORTED_MIME_TYPES, true)) {
230 4
            return $mimeType;
231
        }
232
233
        return null;
234
    }
235
236
    /**
237
     * Get image resource.
238
     */
239 20
    public function getImageResource(): ?GdImage
240
    {
241 20
        return $this->imageResource;
242
    }
243
244
    /**
245
     * Set image resource.
246
     *
247
     * @return $this
248
     */
249 25
    public function setImageResource(?GdImage $value): static
250
    {
251 25
        $this->imageResource = $value;
252
253 25
        if ($this->imageResource !== null) {
254
            // Get width/height
255 25
            $this->width = (int) imagesx($this->imageResource);
256 25
            $this->height = (int) imagesy($this->imageResource);
257
        }
258
259 25
        return $this;
260
    }
261
262
    /**
263
     * Get rendering function.
264
     *
265
     * @return callable-string
266
     */
267 14
    public function getRenderingFunction(): string
268
    {
269 14
        return $this->renderingFunction;
270
    }
271
272
    /**
273
     * Set rendering function.
274
     *
275
     * @param callable-string $value see self::RENDERING_*
276
     *
277
     * @return $this
278
     */
279 22
    public function setRenderingFunction(string $value): static
280
    {
281 22
        $this->renderingFunction = $value;
282
283 22
        return $this;
284
    }
285
286
    /**
287
     * Get mime type.
288
     */
289 17
    public function getMimeType(): string
290
    {
291 17
        return $this->mimeType;
292
    }
293
294
    /**
295
     * Set mime type.
296
     *
297
     * @param string $value see self::MIMETYPE_*
298
     *
299
     * @return $this
300
     */
301 22
    public function setMimeType(string $value): static
302
    {
303 22
        $this->mimeType = $value;
304
305 22
        return $this;
306
    }
307
308
    /**
309
     * Get indexed filename (using image index).
310
     */
311 11
    public function getIndexedFilename(): string
312
    {
313 11
        $extension = strtolower($this->getMimeType());
314 11
        $extension = explode('/', $extension);
315 11
        $extension = $extension[1];
316
317 11
        return $this->uniqueName . $this->getImageIndex() . '.' . $extension;
318
    }
319
320
    /**
321
     * Get hash code.
322
     *
323
     * @return string Hash code
324
     */
325 11
    public function getHashCode(): string
326
    {
327 11
        return md5(
328 11
            $this->renderingFunction
329 11
            . $this->mimeType
330 11
            . $this->uniqueName
331 11
            . parent::getHashCode()
332 11
            . __CLASS__
333 11
        );
334
    }
335
}
336