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

tito10047 / progressive-image-bundle / 20694208732

04 Jan 2026 02:16PM UTC coverage: 89.796%. Remained the same
20694208732

push

github

tito10047
wip. try End-to-Test

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

21
        /**
22
         * @param array<string, mixed> $imageConfigs
23
         */
24
    public function __construct(
25
        private readonly FilterConfiguration $filterConfiguration,
26
                private readonly array $imageConfigs = [],
27
    ) {
28
    }
476✔
29

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

37
        if ($pointInterest) {
322✔
38
            $filterName .= '_'.$pointInterest;
56✔
39
        }
40
                if ($this->imageConfigs) {
322✔
41
                        $filterName .= '_' . substr(md5(serialize($this->imageConfigs)), 0, 5);
14✔
42
                }
43

44
        $config = [];
322✔
45
        if (null !== $filter) {
322✔
46
            try {
47
                $config = $this->filterConfiguration->get($filter);
28✔
48
            } catch (NonExistingFilterException) {
×
49
            }
50
        }
51

52
                $config = array_replace_recursive($config, $this->imageConfigs);
322✔
53

54
        if (!isset($config['filters'])) {
322✔
55
            $config['filters'] = [];
294✔
56
        }
57

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

66
        $config['filters']['thumbnail'] = [
322✔
67
            'size' => [$width, $height],
322✔
68
            'mode' => 'outbound',
322✔
69
        ];
322✔
70

71
        return [
322✔
72
            'filterName' => $filterName,
322✔
73
            'config' => $config,
322✔
74
        ];
322✔
75
    }
76

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

86
                // 2. Calculate the start of the crop so that the center is at the point of interest
87
        $startX = $centerX - (int) ($targetWidth / 2);
56✔
88
        $startY = $centerY - (int) ($targetHeight / 2);
56✔
89

90
                // 3. Ensure that the crop does not go outside the original image
91
        $startX = max(0, min($startX, $origWidth - $targetWidth));
56✔
92
        $startY = max(0, min($startY, $origHeight - $targetHeight));
56✔
93

94
        return [$startX, $startY];
56✔
95
    }
96
}
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