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

conedevelopment / root / 13086943877

01 Feb 2025 08:35AM UTC coverage: 79.285% (+1.2%) from 78.037%
13086943877

push

github

web-flow
Merge pull request #234 from xHeaven/patch-1

Codebase health checkup

195 of 239 new or added lines in 45 files covered. (81.59%)

2 existing lines in 2 files now uncovered.

2572 of 3244 relevant lines covered (79.28%)

35.8 hits per line

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

95.38
/src/Conversion/Image.php
1
<?php
2

3
namespace Cone\Root\Conversion;
4

5
use Cone\Root\Models\Medium;
6
use Exception;
7
use GdImage;
8
use Illuminate\Support\Facades\Storage;
9
use Illuminate\Support\Str;
10

11
class Image
12
{
13
    /**
14
     * The medium instance.
15
     */
16
    protected Medium $medium;
17

18
    /**
19
     * The path of the image.
20
     */
21
    protected string $path;
22

23
    /**
24
     * The file type.
25
     */
26
    protected int $type;
27

28
    /**
29
     * The resource.
30
     */
31
    protected GdImage $resource;
32

33
    /**
34
     * The attributes.
35
     */
36
    protected array $attributes = [
37
        'height' => 0,
38
        'quality' => 70,
39
        'width' => 0,
40
    ];
41

42
    /**
43
     * Create a new image instance.
44
     */
45
    public function __construct(Medium $medium)
46
    {
47
        $this->medium = $medium;
6✔
48

49
        $this->path = Storage::disk('local')->path('root-tmp/'.Str::random(40));
6✔
50

51
        $this->type = exif_imagetype($medium->getAbsolutePath());
6✔
52

53
        $this->create();
6✔
54
    }
55

56
    /**
57
     * Get the image path.
58
     */
59
    public function getPath(): string
60
    {
61
        return $this->path;
5✔
62
    }
63

64
    /**
65
     * Set the width of the image.
66
     */
67
    public function setWidth(int $width): static
68
    {
69
        $this->attributes['width'] = $width;
4✔
70

71
        return $this;
4✔
72
    }
73

74
    /**
75
     * Set the height of the image.
76
     */
77
    public function setHeight(int $height): static
78
    {
79
        $this->attributes['height'] = $height;
4✔
80

81
        return $this;
4✔
82
    }
83

84
    /**
85
     * Set the quality of the image.
86
     */
87
    public function setQuality(int $quality): static
88
    {
89
        $this->attributes['quality'] = $quality;
5✔
90

91
        return $this;
5✔
92
    }
93

94
    /**
95
     * Crop the image.
96
     *
97
     * @return $this
98
     */
99
    public function crop(?int $width = null, ?int $height = null): static
100
    {
101
        $this->resize($width, $height, true);
4✔
102

103
        return $this;
4✔
104
    }
105

106
    /**
107
     * Resize the image.
108
     */
109
    public function resize(?int $width = null, ?int $height = null, bool $crop = false): static
110
    {
111
        $x = $y = 0;
4✔
112
        [$originalWidth, $originalHeight] = getimagesize($this->medium->getAbsolutePath());
4✔
113

114
        $width = $width ?: $this->attributes['width'];
4✔
115
        $width = $width ? min($width, $originalWidth) : $originalWidth;
4✔
116

117
        $height = $height ?: $this->attributes['height'];
4✔
118
        $height = $height ? min($height, $originalHeight) : ($crop ? $width : $originalHeight);
4✔
119

120
        if (! $crop && $width <= $height) {
4✔
121
            $height = ($width / $originalWidth) * $originalHeight;
4✔
122
        } elseif (! $crop && $height < $width) {
4✔
123
            $width = ($height / $originalHeight) * $originalWidth;
4✔
124
        } elseif ($crop && $originalWidth < $originalHeight) {
4✔
125
            $y = ($originalHeight / 2) - ($originalWidth / 2);
×
126
            $originalHeight = $originalWidth;
×
127
        } elseif ($crop && $originalHeight < $originalWidth) {
4✔
128
            $x = ($originalWidth / 2) - ($originalHeight / 2);
4✔
129
            $originalWidth = $originalHeight;
4✔
130
        }
131

132
        $resource = imagecreatetruecolor((int) $width, (int) $height);
4✔
133

134
        if (in_array($this->type, [IMAGETYPE_PNG, IMAGETYPE_WEBP])) {
4✔
135
            imagealphablending($resource, false);
2✔
136
            imagesavealpha($resource, true);
2✔
137
            imagefill($resource, 0, 0, imagecolorallocatealpha($resource, 0, 0, 0, 127));
2✔
138
        }
139

140
        imagecopyresampled(
4✔
141
            $resource,
4✔
142
            $this->resource,
4✔
143
            0,
4✔
144
            0,
4✔
145
            (int) $x,
4✔
146
            (int) $y,
4✔
147
            (int) $width,
4✔
148
            (int) $height,
4✔
149
            $originalWidth,
4✔
150
            $originalHeight
4✔
151
        );
4✔
152

153
        imagedestroy($this->resource);
4✔
154
        $this->resource = $resource;
4✔
155
        unset($resource);
4✔
156

157
        return $this;
4✔
158
    }
159

160
    /**
161
     * Save the resource.
162
     */
163
    public function save(): void
164
    {
165
        match ($this->type) {
5✔
166
            IMAGETYPE_GIF => imagegif($this->resource, $this->path),
5✔
167
            IMAGETYPE_JPEG => imagejpeg($this->resource, $this->path, $this->attributes['quality']),
4✔
168
            IMAGETYPE_PNG => imagepng($this->resource, $this->path, 1),
3✔
169
            IMAGETYPE_WEBP => imagewebp($this->resource, $this->path, $this->attributes['quality']),
1✔
NEW
170
            default => throw new Exception("The file type [{$this->type}] is not supported."),
×
171
        };
5✔
172
    }
173

174
    /**
175
     * Create the resource.
176
     */
177
    protected function create(): void
178
    {
179
        $this->resource = match ($this->type) {
6✔
180
            IMAGETYPE_GIF => imagecreatefromgif($this->medium->getAbsolutePath()),
6✔
181
            IMAGETYPE_JPEG => imagecreatefromjpeg($this->medium->getAbsolutePath()),
5✔
182
            IMAGETYPE_PNG => imagecreatefrompng($this->medium->getAbsolutePath()),
4✔
183
            IMAGETYPE_WEBP => imagecreatefromwebp($this->medium->getAbsolutePath()),
2✔
184
            default => throw new Exception("The file type [{$this->type}] is not supported."),
1✔
185
        };
6✔
186
    }
187

188
    /**
189
     * Destroy the resource.
190
     */
191
    public function destroy(): void
192
    {
193
        imagedestroy($this->resource);
1✔
194
    }
195
}
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

© 2025 Coveralls, Inc