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

tito10047 / progressive-image-bundle / 20675149867

03 Jan 2026 09:03AM UTC coverage: 88.244%. Remained the same
20675149867

push

github

tito10047
add PHP CS Fixer workflow: integrate PHP CS Fixer for code style checks, add configuration files, and update README with badge

608 of 689 relevant lines covered (88.24%)

177.68 hits per line

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

90.91
/src/Analyzer/GdImageAnalyzer.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

12
namespace Tito10047\ProgressiveImageBundle\Analyzer;
13

14
use kornrunner\Blurhash\Blurhash;
15
use Tito10047\ProgressiveImageBundle\DTO\ImageMetadata;
16
use Tito10047\ProgressiveImageBundle\Exception\ImageProcessingException;
17
use Tito10047\ProgressiveImageBundle\Loader\LoaderInterface;
18

19
final class GdImageAnalyzer implements ImageAnalyzerInterface
20
{
21
    public function __construct(
22
        private readonly int $componentsX = 4,
23
        private readonly int $componentsY = 3,
24
    ) {
25
    }
347✔
26

27
    public function analyze(LoaderInterface $loader, string $path): ImageMetadata
28
    {
29
        $stream = $loader->load($path);
171✔
30
        $data = stream_get_contents($stream);
171✔
31

32
        if (false === $data) {
171✔
33
            throw new ImageProcessingException('Nepodarilo sa načítať dáta z loadera pre cestu: '.$path);
×
34
        }
35

36
        $image = @imagecreatefromstring($data);
171✔
37
        if (false === $image) {
171✔
38
            throw new ImageProcessingException('GD nedokázal načítať obrázok z dát pre cestu: '.$path);
×
39
        }
40

41
        $width = imagesx($image);
171✔
42
        $height = imagesy($image);
171✔
43

44
        // Zmenšenie na max 64x64 pre Blurhash (zachovanie pomeru strán)
45
        $targetWidth = 64;
171✔
46
        $targetHeight = 64;
171✔
47

48
        if ($width > $height) {
171✔
49
            $targetHeight = (int) ($height * (64 / $width));
×
50
        } else {
51
            $targetWidth = (int) ($width * (64 / $height));
171✔
52
        }
53

54
        $resizedImage = imagecreatetruecolor($targetWidth, $targetHeight);
171✔
55
        imagecopyresampled($resizedImage, $image, 0, 0, 0, 0, $targetWidth, $targetHeight, $width, $height);
171✔
56

57
        $pixels = [];
171✔
58
        for ($y = 0; $y < $targetHeight; ++$y) {
171✔
59
            $row = [];
171✔
60
            for ($x = 0; $x < $targetWidth; ++$x) {
171✔
61
                $rgb = imagecolorat($resizedImage, $x, $y);
171✔
62
                $r = ($rgb >> 16) & 0xFF;
171✔
63
                $g = ($rgb >> 8) & 0xFF;
171✔
64
                $b = $rgb & 0xFF;
171✔
65
                $row[] = [$r, $g, $b];
171✔
66
            }
67
            $pixels[] = $row;
171✔
68
        }
69

70
        $hash = Blurhash::encode($pixels, $this->componentsX, $this->componentsY);
171✔
71

72
        return new ImageMetadata(
171✔
73
            $hash,
171✔
74
            $width,
171✔
75
            $height
171✔
76
        );
171✔
77
    }
78
}
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