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

tito10047 / progressive-image-bundle / 21109351256

18 Jan 2026 09:16AM UTC coverage: 90.436% (+0.4%) from 90.011%
21109351256

push

github

Jozef Mostka
fix tests

851 of 941 relevant lines covered (90.44%)

322.6 hits per line

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

100.0
/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
     * @param array<string, mixed> $imageConfigs
22
     */
23
    public function __construct(
24
        private readonly FilterConfiguration $filterConfiguration,
25
        private readonly array $imageConfigs = [],
26
    ) {
27
    }
644✔
28

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

36
        if ($pointInterest) {
490✔
37
            $filterName .= '_'.$pointInterest;
56✔
38
        }
39

40
        if ($context) {
490✔
41
            $filterName .= '_'.substr(md5(serialize($context)), 0, 5);
56✔
42
        }
43

44
        if ($this->imageConfigs) {
490✔
45
            $filterName .= '_'.substr(md5(serialize($this->imageConfigs)), 0, 5);
28✔
46
        }
47

48
        $config = [];
490✔
49
        if (null !== $filter) {
490✔
50
            try {
51
                $config = $this->filterConfiguration->get($filter);
84✔
52
            } catch (NonExistingFilterException) {
28✔
53
            }
54
        }
55

56
        $config = array_replace_recursive($config, $this->imageConfigs, $context);
490✔
57

58
        if (!isset($config['filters'])) {
490✔
59
            $config['filters'] = [];
434✔
60
        }
61

62
        if ($pointInterest && $origWidth && $origHeight) {
490✔
63
            [$poiX, $poiY] = explode('x', $pointInterest);
56✔
64
            $config['filters']['crop'] = [
56✔
65
                'start' => $this->calculateCropStart((int) $poiX, (int) $poiY, $width, $height, $origWidth, $origHeight),
56✔
66
                'size' => [$width, $height],
56✔
67
            ];
56✔
68
        }
69

70
        $config['filters']['thumbnail'] = [
490✔
71
            'size' => [$width, $height],
490✔
72
            'mode' => 'outbound',
490✔
73
        ];
490✔
74

75
        return [
490✔
76
            'filterName' => $filterName,
490✔
77
            'config' => $config,
490✔
78
        ];
490✔
79
    }
80

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

90
        // 2. Calculate the start of the crop so that the center is at the point of interest
91
        $startX = $centerX - (int) ($targetWidth / 2);
56✔
92
        $startY = $centerY - (int) ($targetHeight / 2);
56✔
93

94
        // 3. Ensure that the crop does not go outside the original image
95
        $startX = max(0, min($startX, $origWidth - $targetWidth));
56✔
96
        $startY = max(0, min($startY, $origHeight - $targetHeight));
56✔
97

98
        return [$startX, $startY];
56✔
99
    }
100
}
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