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

tito10047 / progressive-image-bundle / 20693585928

04 Jan 2026 01:24PM UTC coverage: 89.796%. Remained the same
20693585928

push

github

tito10047
cs fixer

704 of 784 relevant lines covered (89.8%)

252.6 hits per line

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

97.06
/src/Service/LiipImagineRuntimeConfigGenerator.php
1
<?php
2

3
/*
4
 * This file is part of the Progressive Image Bundle.
5
 *
6
 * (c) Jozef Môstka <https://github.com/tito10047/progressive-image-bundle>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
declare(strict_types=1);
12

13
namespace Tito10047\ProgressiveImageBundle\Service;
14

15
use Liip\ImagineBundle\Exception\Imagine\Filter\NonExistingFilterException;
16
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;
17

18
final class LiipImagineRuntimeConfigGenerator implements LiipImagineRuntimeConfigGeneratorInterface
19
{
20
    public function __construct(
21
        private readonly FilterConfiguration $filterConfiguration,
22
                private readonly array $imageConfigs = [],
23
    ) {
24
    }
476✔
25

26
    /**
27
     * @return array{filterName: string, config: array<string, mixed>}
28
     */
29
    public function generate(int $width, int $height, ?string $filter = null, ?string $pointInterest = null, ?int $origWidth = null, ?int $origHeight = null): array
30
    {
31
        $filterName = $filter ? sprintf('%s_%dx%d', $filter, $width, $height) : sprintf('%dx%d', $width, $height);
322✔
32

33
        if ($pointInterest) {
322✔
34
            $filterName .= '_'.$pointInterest;
56✔
35
        }
36
                if ($this->imageConfigs) {
322✔
37
                        $filterName .= '_' . substr(md5(serialize($this->imageConfigs)), 0, 5);
14✔
38
                }
39

40
        $config = [];
322✔
41
        if (null !== $filter) {
322✔
42
            try {
43
                $config = $this->filterConfiguration->get($filter);
28✔
44
            } catch (NonExistingFilterException) {
×
45
            }
46
        }
47

48
                $config = array_replace_recursive($config, $this->imageConfigs);
322✔
49

50
        if (!isset($config['filters'])) {
322✔
51
            $config['filters'] = [];
294✔
52
        }
53

54
        if ($pointInterest && $origWidth && $origHeight) {
322✔
55
            [$poiX, $poiY] = explode('x', $pointInterest);
56✔
56
            $config['filters']['crop'] = [
56✔
57
                'start' => $this->calculateCropStart((int) $poiX, (int) $poiY, $width, $height, $origWidth, $origHeight),
56✔
58
                'size' => [$width, $height],
56✔
59
            ];
56✔
60
        }
61

62
        $config['filters']['thumbnail'] = [
322✔
63
            'size' => [$width, $height],
322✔
64
            'mode' => 'outbound',
322✔
65
        ];
322✔
66

67
        return [
322✔
68
            'filterName' => $filterName,
322✔
69
            'config' => $config,
322✔
70
        ];
322✔
71
    }
72

73
    /**
74
     * @return array{int, int}
75
     */
76
    private function calculateCropStart(int $poiX, int $poiY, int $targetWidth, int $targetHeight, int $origWidth, int $origHeight): array
77
    {
78
                // 1. Calculate the center of the crop on the original image based on the percentage point of interest
79
        $centerX = (int) ($poiX / 100 * $origWidth);
56✔
80
        $centerY = (int) ($poiY / 100 * $origHeight);
56✔
81

82
                // 2. Calculate the start of the crop so that the center is at the point of interest
83
        $startX = $centerX - (int) ($targetWidth / 2);
56✔
84
        $startY = $centerY - (int) ($targetHeight / 2);
56✔
85

86
                // 3. Ensure that the crop does not go outside the original image
87
        $startX = max(0, min($startX, $origWidth - $targetWidth));
56✔
88
        $startY = max(0, min($startY, $origHeight - $targetHeight));
56✔
89

90
        return [$startX, $startY];
56✔
91
    }
92
}
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