Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
BmpFileFaker
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
5 / 5
14
100.00% covered (success)
100.00%
1 / 1
 createOriginalFilename
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createMimeType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isSupported
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createResource
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
1 / 1
10
 randomColor
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Apie\Faker\FileFakers;
3
4use Apie\Faker\Interfaces\ApieImageFileFaker;
5use Faker\Generator;
6use Nyholm\Psr7\Stream;
7
8/**
9 * Generates an uncompressed 24-bit bmp: a four corner gradient with random circles and optional diagonal stripes.
10 */
11final class BmpFileFaker implements ApieImageFileFaker
12{
13    public function createOriginalFilename(Generator $faker): string
14    {
15        return $faker->word() . '.bmp';
16    }
17
18    public function createMimeType(): string
19    {
20        return 'image/bmp';
21    }
22
23    public static function isSupported(): bool
24    {
25        return true;
26    }
27
28    /** @return resource */
29    public function createResource(Generator $faker, string $originalFilename, string $mimeType): mixed
30    {
31        $width = $faker->numberBetween(96, 256);
32        $height = $faker->numberBetween(96, 256);
33        $corners = [];
34        for ($i = 0; $i < 4; $i++) {
35            $corners[] = $this->randomColor($faker);
36        }
37        $circles = [];
38        for ($i = $faker->numberBetween(2, 6); $i > 0; $i--) {
39            $circles[] = [
40                $faker->numberBetween(0, $width),
41                $faker->numberBetween(0, $height),
42                $faker->numberBetween(10, intdiv(min($width, $height), 2)),
43                $this->randomColor($faker),
44            ];
45        }
46        $stripes = $faker->boolean(40);
47        $padding = str_repeat("\0", (4 - ($width * 3) % 4) % 4);
48
49        $pixels = '';
50        // bmp stores the rows from bottom to top.
51        for ($y = $height - 1; $y >= 0; $y--) {
52            $v = $y / ($height - 1);
53            for ($x = 0; $x < $width; $x++) {
54                $u = $x / ($width - 1);
55                $color = [0, 0, 0];
56                foreach ([0, 1, 2] as $channel) {
57                    $top = $corners[0][$channel] * (1 - $u) + $corners[1][$channel] * $u;
58                    $bottom = $corners[2][$channel] * (1 - $u) + $corners[3][$channel] * $u;
59                    $color[$channel] = $top * (1 - $v) + $bottom * $v;
60                }
61                foreach ($circles as [$cx, $cy, $radius, $circleColor]) {
62                    if (($x - $cx) ** 2 + ($y - $cy) ** 2 <= $radius ** 2) {
63                        $color = $circleColor;
64                    }
65                }
66                if ($stripes && (($x + $y) >> 4) & 1) {
67                    $color = [$color[0] * 0.85, $color[1] * 0.85, $color[2] * 0.85];
68                }
69                $pixels .= chr((int) $color[2]) . chr((int) $color[1]) . chr((int) $color[0]);
70            }
71            $pixels .= $padding;
72        }
73
74        $header = 'BM' . pack('VvvV', 54 + strlen($pixels), 0, 0, 54)
75            . pack('VVVvvVVVVVV', 40, $width, $height, 1, 24, 0, strlen($pixels), 2835, 2835, 0, 0);
76
77        return Stream::create($header . $pixels)->detach();
78    }
79
80    /** @return array{int, int, int} */
81    private function randomColor(Generator $faker): array
82    {
83        return [$faker->numberBetween(0, 255), $faker->numberBetween(0, 255), $faker->numberBetween(0, 255)];
84    }
85}