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

codeigniter4 / CodeIgniter4 / 18261753254

05 Oct 2025 05:05PM UTC coverage: 84.343% (-0.002%) from 84.345%
18261753254

push

github

web-flow
Merge pull request #9744 from samsonasik/refactor-apply-code-quality-52

refactor: apply code quality level 52 for Rector

1 of 2 new or added lines in 1 file covered. (50.0%)

20874 of 24749 relevant lines covered (84.34%)

194.51 hits per line

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

89.8
/system/Images/Handlers/ImageMagickHandler.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Images\Handlers;
15

16
use CodeIgniter\I18n\Time;
17
use CodeIgniter\Images\Exceptions\ImageException;
18
use Config\Images;
19
use Exception;
20
use Imagick;
21

22
/**
23
 * Class ImageMagickHandler
24
 *
25
 * FIXME - This needs conversion & unit testing, to use the imagick extension
26
 */
27
class ImageMagickHandler extends BaseHandler
28
{
29
    /**
30
     * Stores image resource in memory.
31
     *
32
     * @var string|null
33
     */
34
    protected $resource;
35

36
    /**
37
     * @param Images $config
38
     *
39
     * @throws ImageException
40
     */
41
    public function __construct($config = null)
42
    {
43
        parent::__construct($config);
40✔
44

45
        if (! extension_loaded('imagick') && ! class_exists(Imagick::class)) {
40✔
46
            throw ImageException::forMissingExtension('IMAGICK'); // @codeCoverageIgnore
×
47
        }
48

49
        $cmd = $this->config->libraryPath;
40✔
50

51
        if ($cmd === '') {
40✔
52
            throw ImageException::forInvalidImageLibraryPath($cmd);
1✔
53
        }
54

55
        if (preg_match('/convert$/i', $cmd) !== 1) {
40✔
56
            $cmd = rtrim($cmd, '\/') . '/convert';
1✔
57

58
            $this->config->libraryPath = $cmd;
1✔
59
        }
60

61
        if (! is_file($cmd)) {
40✔
62
            throw ImageException::forInvalidImageLibraryPath($cmd);
2✔
63
        }
64
    }
65

66
    /**
67
     * Handles the actual resizing of the image.
68
     *
69
     * @return ImageMagickHandler
70
     *
71
     * @throws Exception
72
     */
73
    public function _resize(bool $maintainRatio = false)
74
    {
75
        $source      = empty($this->resource) ? $this->image()->getPathname() : $this->resource;
9✔
76
        $destination = $this->getResourcePath();
9✔
77

78
        $escape = '\\';
9✔
79

80
        if (PHP_OS_FAMILY === 'Windows') {
9✔
81
            $escape = '';
×
82
        }
83

84
        $action = $maintainRatio
9✔
85
            ? ' -resize ' . ($this->width ?? 0) . 'x' . ($this->height ?? 0) . ' ' . escapeshellarg($source) . ' ' . escapeshellarg($destination)
3✔
86
            : ' -resize ' . ($this->width ?? 0) . 'x' . ($this->height ?? 0) . "{$escape}! " . escapeshellarg($source) . ' ' . escapeshellarg($destination);
6✔
87

88
        $this->process($action);
9✔
89

90
        return $this;
9✔
91
    }
92

93
    /**
94
     * Crops the image.
95
     *
96
     * @return bool|ImageMagickHandler
97
     *
98
     * @throws Exception
99
     */
100
    public function _crop()
101
    {
102
        $source      = empty($this->resource) ? $this->image()->getPathname() : $this->resource;
10✔
103
        $destination = $this->getResourcePath();
10✔
104

105
        $extent = ' ';
10✔
106
        if ($this->xAxis >= $this->width || $this->yAxis > $this->height) {
10✔
107
            $extent = ' -background transparent -extent ' . ($this->width ?? 0) . 'x' . ($this->height ?? 0) . ' ';
2✔
108
        }
109

110
        $action = ' -crop ' . ($this->width ?? 0) . 'x' . ($this->height ?? 0) . '+' . ($this->xAxis ?? 0) . '+' . ($this->yAxis ?? 0) . $extent . escapeshellarg($source) . ' ' . escapeshellarg($destination);
10✔
111

112
        $this->process($action);
10✔
113

114
        return $this;
10✔
115
    }
116

117
    /**
118
     * Handles the rotation of an image resource.
119
     * Doesn't save the image, but replaces the current resource.
120
     *
121
     * @return $this
122
     *
123
     * @throws Exception
124
     */
125
    protected function _rotate(int $angle)
126
    {
127
        $angle = '-rotate ' . $angle;
3✔
128

129
        $source      = empty($this->resource) ? $this->image()->getPathname() : $this->resource;
3✔
130
        $destination = $this->getResourcePath();
3✔
131

132
        $action = ' ' . $angle . ' ' . escapeshellarg($source) . ' ' . escapeshellarg($destination);
3✔
133

134
        $this->process($action);
3✔
135

136
        return $this;
3✔
137
    }
138

139
    /**
140
     * Flattens transparencies, default white background
141
     *
142
     * @return $this
143
     *
144
     * @throws Exception
145
     */
146
    protected function _flatten(int $red = 255, int $green = 255, int $blue = 255)
147
    {
148
        $flatten = "-background 'rgb({$red},{$green},{$blue})' -flatten";
1✔
149

150
        $source      = empty($this->resource) ? $this->image()->getPathname() : $this->resource;
1✔
151
        $destination = $this->getResourcePath();
1✔
152

153
        $action = ' ' . $flatten . ' ' . escapeshellarg($source) . ' ' . escapeshellarg($destination);
1✔
154

155
        $this->process($action);
1✔
156

157
        return $this;
1✔
158
    }
159

160
    /**
161
     * Flips an image along it's vertical or horizontal axis.
162
     *
163
     * @return $this
164
     *
165
     * @throws Exception
166
     */
167
    protected function _flip(string $direction)
168
    {
169
        $angle = $direction === 'horizontal' ? '-flop' : '-flip';
5✔
170

171
        $source      = empty($this->resource) ? $this->image()->getPathname() : $this->resource;
5✔
172
        $destination = $this->getResourcePath();
5✔
173

174
        $action = ' ' . $angle . ' ' . escapeshellarg($source) . ' ' . escapeshellarg($destination);
5✔
175

176
        $this->process($action);
5✔
177

178
        return $this;
5✔
179
    }
180

181
    /**
182
     * Get driver version
183
     */
184
    public function getVersion(): string
185
    {
186
        $versionString = $this->process('-version')[0];
1✔
187
        preg_match('/ImageMagick\s(?P<version>[\S]+)/', $versionString, $matches);
1✔
188

189
        return $matches['version'];
1✔
190
    }
191

192
    /**
193
     * Handles all of the grunt work of resizing, etc.
194
     *
195
     * @return array Lines of output from shell command
196
     *
197
     * @throws Exception
198
     */
199
    protected function process(string $action, int $quality = 100): array
200
    {
201
        if ($action !== '-version') {
31✔
202
            $this->supportedFormatCheck();
30✔
203
        }
204

205
        $cmd = $this->config->libraryPath;
31✔
206
        $cmd .= $action === '-version' ? ' ' . $action : ' -quality ' . $quality . ' ' . $action;
31✔
207

208
        $retval = 1;
31✔
209
        $output = [];
31✔
210
        // exec() might be disabled
211
        if (function_usable('exec')) {
31✔
212
            @exec($cmd, $output, $retval);
31✔
213
        }
214

215
        // Did it work?
216
        if ($retval > 0) {
31✔
217
            throw ImageException::forImageProcessFailed();
×
218
        }
219

220
        return $output;
31✔
221
    }
222

223
    /**
224
     * Saves any changes that have been made to file. If no new filename is
225
     * provided, the existing image is overwritten, otherwise a copy of the
226
     * file is made at $target.
227
     *
228
     * Example:
229
     *    $image->resize(100, 200, true)
230
     *          ->save();
231
     *
232
     * @param non-empty-string|null $target
233
     */
234
    public function save(?string $target = null, int $quality = 90): bool
235
    {
236
        $original = $target;
7✔
237
        $target   = ($target === null || $target === '') ? $this->image()->getPathname() : $target;
7✔
238

239
        // If no new resource has been created, then we're
240
        // simply copy the existing one.
241
        if (empty($this->resource) && $quality === 100) {
7✔
242
            if ($original === null) {
1✔
243
                return true;
1✔
244
            }
245

246
            $name = basename($target);
×
247
            $path = pathinfo($target, PATHINFO_DIRNAME);
×
248

249
            return $this->image()->copy($path, $name);
×
250
        }
251

252
        $this->ensureResource();
6✔
253

254
        // Copy the file through ImageMagick so that it has
255
        // a chance to convert file format.
256
        $action = escapeshellarg($this->resource) . ' ' . escapeshellarg($target);
6✔
257

258
        $this->process($action, $quality);
6✔
259

260
        unlink($this->resource);
6✔
261

262
        return true;
6✔
263
    }
264

265
    /**
266
     * Get Image Resource
267
     *
268
     * This simply creates an image resource handle
269
     * based on the type of image being processed.
270
     * Since ImageMagick is used on the cli, we need to
271
     * ensure we have a temporary file on the server
272
     * that we can use.
273
     *
274
     * To ensure we can use all features, like transparency,
275
     * during the process, we'll use a PNG as the temp file type.
276
     *
277
     * @return string
278
     *
279
     * @throws Exception
280
     */
281
    protected function getResourcePath()
282
    {
283
        if ($this->resource !== null) {
30✔
284
            return $this->resource;
10✔
285
        }
286

287
        $this->resource = WRITEPATH . 'cache/' . Time::now()->getTimestamp() . '_' . bin2hex(random_bytes(10)) . '.png';
30✔
288

289
        $name = basename($this->resource);
30✔
290
        $path = pathinfo($this->resource, PATHINFO_DIRNAME);
30✔
291

292
        $this->image()->copy($path, $name);
30✔
293

294
        return $this->resource;
30✔
295
    }
296

297
    /**
298
     * Make the image resource object if needed
299
     *
300
     * @return void
301
     *
302
     * @throws Exception
303
     */
304
    protected function ensureResource()
305
    {
306
        $this->getResourcePath();
6✔
307

308
        $this->supportedFormatCheck();
6✔
309
    }
310

311
    /**
312
     * Check if given image format is supported
313
     *
314
     * @return void
315
     *
316
     * @throws ImageException
317
     */
318
    protected function supportedFormatCheck()
319
    {
320
        if ($this->image()->imageType === IMAGETYPE_WEBP && ! in_array('WEBP', Imagick::queryFormats(), true)) {
30✔
NEW
321
            throw ImageException::forInvalidImageCreate(lang('Images.webpNotSupported'));
×
322
        }
323
    }
324

325
    /**
326
     * Handler-specific method for overlaying text on an image.
327
     *
328
     * @throws Exception
329
     */
330
    protected function _text(string $text, array $options = [])
331
    {
332
        $xAxis   = 0;
4✔
333
        $yAxis   = 0;
4✔
334
        $gravity = '';
4✔
335
        $cmd     = '';
4✔
336

337
        // Reverse the vertical offset
338
        // When the image is positioned at the bottom
339
        // we don't want the vertical offset to push it
340
        // further down. We want the reverse, so we'll
341
        // invert the offset. Note: The horizontal
342
        // offset flips itself automatically
343
        if ($options['vAlign'] === 'bottom') {
4✔
344
            $options['vOffset'] *= -1;
3✔
345
        }
346

347
        if ($options['hAlign'] === 'right') {
4✔
348
            $options['hOffset'] *= -1;
1✔
349
        }
350

351
        // Font
352
        if (! empty($options['fontPath'])) {
4✔
353
            $cmd .= ' -font ' . escapeshellarg($options['fontPath']);
×
354
        }
355

356
        if (isset($options['hAlign'], $options['vAlign'])) {
4✔
357
            switch ($options['hAlign']) {
4✔
358
                case 'left':
4✔
359
                    $xAxis   = $options['hOffset'] + $options['padding'];
×
360
                    $yAxis   = $options['vOffset'] + $options['padding'];
×
361
                    $gravity = $options['vAlign'] === 'top' ? 'NorthWest' : 'West';
×
362
                    if ($options['vAlign'] === 'bottom') {
×
363
                        $gravity = 'SouthWest';
×
364
                        $yAxis   = $options['vOffset'] - $options['padding'];
×
365
                    }
366
                    break;
×
367

368
                case 'center':
4✔
369
                    $xAxis   = $options['hOffset'] + $options['padding'];
3✔
370
                    $yAxis   = $options['vOffset'] + $options['padding'];
3✔
371
                    $gravity = $options['vAlign'] === 'top' ? 'North' : 'Center';
3✔
372
                    if ($options['vAlign'] === 'bottom') {
3✔
373
                        $yAxis   = $options['vOffset'] - $options['padding'];
2✔
374
                        $gravity = 'South';
2✔
375
                    }
376
                    break;
3✔
377

378
                case 'right':
1✔
379
                    $xAxis   = $options['hOffset'] - $options['padding'];
1✔
380
                    $yAxis   = $options['vOffset'] + $options['padding'];
1✔
381
                    $gravity = $options['vAlign'] === 'top' ? 'NorthEast' : 'East';
1✔
382
                    if ($options['vAlign'] === 'bottom') {
1✔
383
                        $gravity = 'SouthEast';
1✔
384
                        $yAxis   = $options['vOffset'] - $options['padding'];
1✔
385
                    }
386
                    break;
1✔
387
            }
388

389
            $xAxis = $xAxis >= 0 ? '+' . $xAxis : $xAxis;
4✔
390
            $yAxis = $yAxis >= 0 ? '+' . $yAxis : $yAxis;
4✔
391

392
            $cmd .= ' -gravity ' . escapeshellarg($gravity) . ' -geometry ' . escapeshellarg("{$xAxis}{$yAxis}");
4✔
393
        }
394

395
        // Color
396
        if (isset($options['color'])) {
4✔
397
            [$r, $g, $b] = sscanf("#{$options['color']}", '#%02x%02x%02x');
4✔
398

399
            $cmd .= ' -fill ' . escapeshellarg("rgba({$r},{$g},{$b},{$options['opacity']})");
4✔
400
        }
401

402
        // Font Size - use points....
403
        if (isset($options['fontSize'])) {
4✔
404
            $cmd .= ' -pointsize ' . escapeshellarg((string) $options['fontSize']);
4✔
405
        }
406

407
        // Text
408
        $cmd .= ' -annotate 0 ' . escapeshellarg($text);
4✔
409

410
        $source      = empty($this->resource) ? $this->image()->getPathname() : $this->resource;
4✔
411
        $destination = $this->getResourcePath();
4✔
412

413
        $cmd = ' ' . escapeshellarg($source) . ' ' . $cmd . ' ' . escapeshellarg($destination);
4✔
414

415
        $this->process($cmd);
4✔
416
    }
417

418
    /**
419
     * Return the width of an image.
420
     *
421
     * @return int
422
     */
423
    public function _getWidth()
424
    {
425
        return imagesx(imagecreatefromstring(file_get_contents($this->resource)));
23✔
426
    }
427

428
    /**
429
     * Return the height of an image.
430
     *
431
     * @return int
432
     */
433
    public function _getHeight()
434
    {
435
        return imagesy(imagecreatefromstring(file_get_contents($this->resource)));
22✔
436
    }
437

438
    /**
439
     * Reads the EXIF information from the image and modifies the orientation
440
     * so that displays correctly in the browser. This is especially an issue
441
     * with images taken by smartphones who always store the image up-right,
442
     * but set the orientation flag to display it correctly.
443
     *
444
     * @param bool $silent If true, will ignore exceptions when PHP doesn't support EXIF.
445
     *
446
     * @return $this
447
     */
448
    public function reorient(bool $silent = false)
449
    {
450
        $orientation = $this->getEXIF('Orientation', $silent);
2✔
451

452
        return match ($orientation) {
2✔
453
            2       => $this->flip('horizontal'),
2✔
454
            3       => $this->rotate(180),
2✔
455
            4       => $this->rotate(180)->flip('horizontal'),
2✔
456
            5       => $this->rotate(90)->flip('horizontal'),
2✔
457
            6       => $this->rotate(90),
2✔
458
            7       => $this->rotate(270)->flip('horizontal'),
2✔
459
            8       => $this->rotate(270),
2✔
460
            default => $this,
2✔
461
        };
2✔
462
    }
463
}
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