• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPOffice / PHPWord / 13025401639

29 Jan 2025 05:48AM UTC coverage: 96.825% (-0.4%) from 97.217%
13025401639

Pull #2562

github

web-flow
Merge ebe3e5dac into 2d2759585
Pull Request #2562: TemplateProcessor SetComplexBlock/Value and Sections

6 of 7 new or added lines in 1 file covered. (85.71%)

245 existing lines in 40 files now uncovered.

12227 of 12628 relevant lines covered (96.82%)

34.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.89
/src/PhpWord/Element/Image.php
1
<?php
2

3
/**
4
 * This file is part of PHPWord - A pure PHP library for reading and writing
5
 * word processing documents.
6
 *
7
 * PHPWord is free software distributed under the terms of the GNU Lesser
8
 * General Public License version 3 as published by the Free Software Foundation.
9
 *
10
 * For the full copyright and license information, please read the LICENSE
11
 * file that was distributed with this source code. For the full list of
12
 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13
 *
14
 * @see         https://github.com/PHPOffice/PHPWord
15
 *
16
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17
 */
18

19
namespace PhpOffice\PhpWord\Element;
20

21
use PhpOffice\PhpWord\Exception\CreateTemporaryFileException;
22
use PhpOffice\PhpWord\Exception\InvalidImageException;
23
use PhpOffice\PhpWord\Exception\UnsupportedImageTypeException;
24
use PhpOffice\PhpWord\Settings;
25
use PhpOffice\PhpWord\Shared\ZipArchive;
26
use PhpOffice\PhpWord\Style\Image as ImageStyle;
27

28
/**
29
 * Image element.
30
 */
31
class Image extends AbstractElement
32
{
33
    /**
34
     * Image source type constants.
35
     */
36
    const SOURCE_LOCAL = 'local'; // Local images
37
    const SOURCE_GD = 'gd'; // Generated using GD
38
    const SOURCE_ARCHIVE = 'archive'; // Image in archives zip://$archive#$image
39
    const SOURCE_STRING = 'string'; // Image from string
40

41
    /**
42
     * Image source.
43
     *
44
     * @var string
45
     */
46
    private $source;
47

48
    /**
49
     * Source type: local|gd|archive.
50
     *
51
     * @var string
52
     */
53
    private $sourceType;
54

55
    /**
56
     * Image style.
57
     *
58
     * @var ?ImageStyle
59
     */
60
    private $style;
61

62
    /**
63
     * Is watermark.
64
     *
65
     * @var bool
66
     */
67
    private $watermark;
68

69
    /**
70
     * Name of image.
71
     *
72
     * @var string
73
     */
74
    private $name;
75

76
    /**
77
     * Image type.
78
     *
79
     * @var string
80
     */
81
    private $imageType;
82

83
    /**
84
     * Image create function.
85
     *
86
     * @var string
87
     */
88
    private $imageCreateFunc;
89

90
    /**
91
     * Image function.
92
     *
93
     * @var null|callable(resource): void
94
     */
95
    private $imageFunc;
96

97
    /**
98
     * Image extension.
99
     *
100
     * @var string
101
     */
102
    private $imageExtension;
103

104
    /**
105
     * Image quality.
106
     *
107
     * Functions imagepng() and imagejpeg() have an optional parameter for
108
     * quality.
109
     *
110
     * @var null|int
111
     */
112
    private $imageQuality;
113

114
    /**
115
     * Is memory image.
116
     *
117
     * @var bool
118
     */
119
    private $memoryImage;
120

121
    /**
122
     * Image target file name.
123
     *
124
     * @var string
125
     */
126
    private $target;
127

128
    /**
129
     * Image media index.
130
     *
131
     * @var int
132
     */
133
    private $mediaIndex;
134

135
    /**
136
     * Has media relation flag; true for Link, Image, and Object.
137
     *
138
     * @var bool
139
     */
140
    protected $mediaRelation = true;
141

142
    /**
143
     * Create new image element.
144
     *
145
     * @param string $source
146
     * @param mixed $style
147
     * @param bool $watermark
148
     * @param string $name
149
     */
150
    public function __construct($source, $style = null, $watermark = false, $name = null)
151
    {
152
        $this->source = $source;
65✔
153
        $this->style = $this->setNewStyle(new ImageStyle(), $style, true);
65✔
154
        $this->setIsWatermark($watermark);
65✔
155
        $this->setName($name);
65✔
156

157
        $this->checkImage();
65✔
158
    }
159

160
    /**
161
     * Get Image style.
162
     *
163
     * @return ?ImageStyle
164
     */
165
    public function getStyle()
166
    {
167
        return $this->style;
30✔
168
    }
169

170
    /**
171
     * Get image source.
172
     *
173
     * @return string
174
     */
175
    public function getSource()
176
    {
177
        return $this->source;
48✔
178
    }
179

180
    /**
181
     * Get image source type.
182
     *
183
     * @return string
184
     */
185
    public function getSourceType()
186
    {
187
        return $this->sourceType;
1✔
188
    }
189

190
    /**
191
     * Sets the image name.
192
     *
193
     * @param string $value
194
     */
195
    public function setName($value): void
196
    {
197
        $this->name = $value;
65✔
198
    }
199

200
    /**
201
     * Get image name.
202
     *
203
     * @return null|string
204
     */
205
    public function getName()
206
    {
207
        return $this->name;
5✔
208
    }
209

210
    /**
211
     * Get image media ID.
212
     *
213
     * @return string
214
     */
215
    public function getMediaId()
216
    {
217
        return md5($this->source);
8✔
218
    }
219

220
    /**
221
     * Get is watermark.
222
     *
223
     * @return bool
224
     */
225
    public function isWatermark()
226
    {
227
        return $this->watermark;
21✔
228
    }
229

230
    /**
231
     * Set is watermark.
232
     *
233
     * @param bool $value
234
     */
235
    public function setIsWatermark($value): void
236
    {
237
        $this->watermark = $value;
65✔
238
    }
239

240
    /**
241
     * Get image type.
242
     *
243
     * @return string
244
     */
245
    public function getImageType()
246
    {
247
        return $this->imageType;
54✔
248
    }
249

250
    /**
251
     * Get image create function.
252
     *
253
     * @return string
254
     */
255
    public function getImageCreateFunction()
256
    {
257
        return $this->imageCreateFunc;
7✔
258
    }
259

260
    /**
261
     * Get image function.
262
     *
263
     * @return null|callable(resource): void
264
     */
265
    public function getImageFunction(): ?callable
266
    {
267
        return $this->imageFunc;
7✔
268
    }
269

270
    /**
271
     * Get image quality.
272
     */
273
    public function getImageQuality(): ?int
274
    {
275
        return $this->imageQuality;
7✔
276
    }
277

278
    /**
279
     * Get image extension.
280
     *
281
     * @return string
282
     */
283
    public function getImageExtension()
284
    {
285
        return $this->imageExtension;
53✔
286
    }
287

288
    /**
289
     * Get is memory image.
290
     *
291
     * @return bool
292
     */
293
    public function isMemImage()
294
    {
295
        return $this->memoryImage;
53✔
296
    }
297

298
    /**
299
     * Get target file name.
300
     *
301
     * @return string
302
     */
303
    public function getTarget()
304
    {
305
        return $this->target;
5✔
306
    }
307

308
    /**
309
     * Set target file name.
310
     *
311
     * @param string $value
312
     */
313
    public function setTarget($value): void
314
    {
315
        $this->target = $value;
46✔
316
    }
317

318
    /**
319
     * Get media index.
320
     *
321
     * @return int
322
     */
323
    public function getMediaIndex()
324
    {
325
        return $this->mediaIndex;
5✔
326
    }
327

328
    /**
329
     * Set media index.
330
     *
331
     * @param int $value
332
     */
333
    public function setMediaIndex($value): void
334
    {
335
        $this->mediaIndex = $value;
46✔
336
    }
337

338
    /**
339
     * Get image string.
340
     */
341
    public function getImageString(): ?string
342
    {
343
        $source = $this->source;
17✔
344
        $actualSource = null;
17✔
345
        $imageBinary = null;
17✔
346
        $isTemp = false;
17✔
347

348
        // Get actual source from archive image or other source
349
        // Return null if not found
350
        if ($this->sourceType == self::SOURCE_ARCHIVE) {
17✔
351
            $source = substr($source, 6);
2✔
352
            [$zipFilename, $imageFilename] = explode('#', $source);
2✔
353

354
            $zip = new ZipArchive();
2✔
355
            if ($zip->open($zipFilename) !== false) {
2✔
356
                if ($zip->locateName($imageFilename) !== false) {
2✔
357
                    $isTemp = true;
2✔
358
                    $zip->extractTo(Settings::getTempDir(), $imageFilename);
2✔
359
                    $actualSource = Settings::getTempDir() . DIRECTORY_SEPARATOR . $imageFilename;
2✔
360
                }
361
            }
362
            $zip->close();
2✔
363
        } else {
364
            $actualSource = $source;
16✔
365
        }
366

367
        // Can't find any case where $actualSource = null hasn't captured by
368
        // preceding exceptions. Please uncomment when you find the case and
369
        // put the case into Element\ImageTest.
370
        // if ($actualSource === null) {
371
        //     return null;
372
        // }
373

374
        // Read image binary data and convert to hex/base64 string
375
        if ($this->sourceType == self::SOURCE_GD) {
17✔
376
            $imageResource = call_user_func($this->imageCreateFunc, $actualSource);
8✔
377
            if ($this->imageType === 'image/png') {
8✔
378
                // PNG images need to preserve alpha channel information
379
                imagesavealpha($imageResource, true);
3✔
380
            }
381
            ob_start();
8✔
382
            $callback = $this->imageFunc;
8✔
383
            $callback($imageResource);
8✔
384
            $imageBinary = ob_get_contents();
8✔
385
            ob_end_clean();
8✔
386
        } elseif ($this->sourceType == self::SOURCE_STRING) {
10✔
387
            $imageBinary = $this->source;
1✔
388
        } else {
389
            $fileHandle = fopen($actualSource, 'rb', false);
9✔
390
            $fileSize = filesize($actualSource);
9✔
391
            if ($fileHandle !== false && $fileSize > 0) {
9✔
392
                $imageBinary = fread($fileHandle, $fileSize);
9✔
393
                fclose($fileHandle);
9✔
394
            }
395
        }
396

397
        // Delete temporary file if necessary
398
        if ($isTemp === true) {
17✔
399
            @unlink($actualSource);
2✔
400
        }
401

402
        return $imageBinary;
17✔
403
    }
404

405
    /**
406
     * Get image string data.
407
     *
408
     * @param bool $base64
409
     *
410
     * @return null|string
411
     *
412
     * @since 0.11.0
413
     */
414
    public function getImageStringData($base64 = false)
415
    {
416
        $imageBinary = $this->getImageString();
11✔
417
        if ($imageBinary === null) {
11✔
UNCOV
418
            return null;
×
419
        }
420

421
        if ($base64) {
11✔
422
            return base64_encode($imageBinary);
5✔
423
        }
424

425
        return bin2hex($imageBinary);
10✔
426
    }
427

428
    /**
429
     * Check memory image, supported type, image functions, and proportional width/height.
430
     */
431
    private function checkImage(): void
432
    {
433
        $this->setSourceType();
65✔
434

435
        // Check image data
436
        if ($this->sourceType == self::SOURCE_ARCHIVE) {
65✔
437
            $imageData = $this->getArchiveImageSize($this->source);
8✔
438
        } elseif ($this->sourceType == self::SOURCE_STRING) {
59✔
439
            $imageData = @getimagesizefromstring($this->source);
3✔
440
        } else {
441
            $imageData = @getimagesize($this->source);
56✔
442
        }
443
        if (!is_array($imageData)) {
65✔
444
            throw new InvalidImageException(sprintf('Invalid image: %s', $this->source));
4✔
445
        }
446
        [$actualWidth, $actualHeight, $imageType] = $imageData;
61✔
447

448
        // Check image type support
449
        $supportedTypes = [IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_PNG];
61✔
450
        if ($this->sourceType != self::SOURCE_GD && $this->sourceType != self::SOURCE_STRING) {
61✔
451
            $supportedTypes = array_merge($supportedTypes, [IMAGETYPE_BMP, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM]);
55✔
452
        }
453
        if (!in_array($imageType, $supportedTypes)) {
61✔
454
            throw new UnsupportedImageTypeException();
1✔
455
        }
456

457
        // Define image functions
458
        $this->imageType = image_type_to_mime_type($imageType);
60✔
459
        $this->setFunctions();
60✔
460
        $this->setProportionalSize($actualWidth, $actualHeight);
60✔
461
    }
462

463
    /**
464
     * Set source type.
465
     */
466
    private function setSourceType(): void
467
    {
468
        if (stripos(strrev($this->source), strrev('.php')) === 0) {
65✔
469
            $this->memoryImage = true;
1✔
470
            $this->sourceType = self::SOURCE_GD;
1✔
471
        } elseif (strpos($this->source, 'zip://') !== false) {
64✔
472
            $this->memoryImage = false;
8✔
473
            $this->sourceType = self::SOURCE_ARCHIVE;
8✔
474
        } elseif (filter_var($this->source, FILTER_VALIDATE_URL) !== false) {
58✔
475
            $this->memoryImage = true;
9✔
476
            if (strpos($this->source, 'https') === 0) {
9✔
477
                $fileContent = file_get_contents($this->source);
×
UNCOV
478
                $this->source = $fileContent;
×
UNCOV
479
                $this->sourceType = self::SOURCE_STRING;
×
480
            } else {
481
                $this->sourceType = self::SOURCE_GD;
9✔
482
            }
483
        } elseif ((strpos($this->source, chr(0)) === false) && @file_exists($this->source)) {
53✔
484
            $this->memoryImage = false;
50✔
485
            $this->sourceType = self::SOURCE_LOCAL;
50✔
486
        } else {
487
            $this->memoryImage = true;
3✔
488
            $this->sourceType = self::SOURCE_STRING;
3✔
489
        }
490
    }
491

492
    /**
493
     * Get image size from archive.
494
     *
495
     * @since 0.12.0 Throws CreateTemporaryFileException.
496
     *
497
     * @param string $source
498
     *
499
     * @return null|array
500
     */
501
    private function getArchiveImageSize($source)
502
    {
503
        $imageData = null;
8✔
504
        $source = substr($source, 6);
8✔
505
        [$zipFilename, $imageFilename] = explode('#', $source);
8✔
506

507
        $tempFilename = tempnam(Settings::getTempDir(), 'PHPWordImage');
8✔
508
        if (false === $tempFilename) {
8✔
509
            throw new CreateTemporaryFileException(); // @codeCoverageIgnore
510
        }
511

512
        $zip = new ZipArchive();
8✔
513
        if ($zip->open($zipFilename) !== false) {
8✔
514
            if ($zip->locateName($imageFilename) !== false) {
8✔
515
                $imageContent = $zip->getFromName($imageFilename);
8✔
516
                if ($imageContent !== false) {
8✔
517
                    file_put_contents($tempFilename, $imageContent);
8✔
518
                    $imageData = getimagesize($tempFilename);
8✔
519
                    unlink($tempFilename);
8✔
520
                }
521
            }
522
            $zip->close();
8✔
523
        }
524

525
        return $imageData;
8✔
526
    }
527

528
    /**
529
     * Set image functions and extensions.
530
     */
531
    private function setFunctions(): void
532
    {
533
        switch ($this->imageType) {
60✔
534
            case 'image/png':
60✔
535
                $this->imageCreateFunc = $this->sourceType == self::SOURCE_STRING ? 'imagecreatefromstring' : 'imagecreatefrompng';
24✔
536
                $this->imageFunc = function ($resource): void {
24✔
537
                    imagepng($resource, null, $this->imageQuality);
3✔
538
                };
24✔
539
                $this->imageExtension = 'png';
24✔
540
                $this->imageQuality = -1;
24✔
541

542
                break;
24✔
543
            case 'image/gif':
41✔
544
                $this->imageCreateFunc = $this->sourceType == self::SOURCE_STRING ? 'imagecreatefromstring' : 'imagecreatefromgif';
9✔
545
                $this->imageFunc = function ($resource): void {
9✔
546
                    imagegif($resource);
5✔
547
                };
9✔
548
                $this->imageExtension = 'gif';
9✔
549
                $this->imageQuality = null;
9✔
550

551
                break;
9✔
552
            case 'image/jpeg':
37✔
553
            case 'image/jpg':
3✔
554
                $this->imageCreateFunc = $this->sourceType == self::SOURCE_STRING ? 'imagecreatefromstring' : 'imagecreatefromjpeg';
35✔
555
                $this->imageFunc = function ($resource): void {
35✔
UNCOV
556
                    imagejpeg($resource, null, $this->imageQuality);
×
557
                };
35✔
558
                $this->imageExtension = 'jpg';
35✔
559
                $this->imageQuality = 100;
35✔
560

561
                break;
35✔
562
            case 'image/bmp':
3✔
563
            case 'image/x-ms-bmp':
2✔
564
                $this->imageType = 'image/bmp';
2✔
565
                $this->imageFunc = null;
2✔
566
                $this->imageExtension = 'bmp';
2✔
567
                $this->imageQuality = null;
2✔
568

569
                break;
2✔
570
            case 'image/tiff':
2✔
571
                $this->imageType = 'image/tiff';
2✔
572
                $this->imageFunc = null;
2✔
573
                $this->imageExtension = 'tif';
2✔
574
                $this->imageQuality = null;
2✔
575

576
                break;
2✔
577
        }
578
    }
579

580
    /**
581
     * Set proportional width/height if one dimension not available.
582
     *
583
     * @param int $actualWidth
584
     * @param int $actualHeight
585
     */
586
    private function setProportionalSize($actualWidth, $actualHeight): void
587
    {
588
        $styleWidth = $this->style->getWidth();
60✔
589
        $styleHeight = $this->style->getHeight();
60✔
590
        if (!($styleWidth && $styleHeight)) {
60✔
591
            if ($styleWidth == null && $styleHeight == null) {
54✔
592
                $this->style->setWidth($actualWidth);
52✔
593
                $this->style->setHeight($actualHeight);
52✔
594
            } elseif ($styleWidth) {
3✔
595
                $this->style->setHeight($actualHeight * ($styleWidth / $actualWidth));
2✔
596
            } else {
597
                $this->style->setWidth($actualWidth * ($styleHeight / $actualHeight));
1✔
598
            }
599
        }
600
    }
601
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc