FilesizeScaler::getVariations()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 1
dl 0
loc 23
ccs 0
cts 13
cp 0
crap 6
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
namespace Pageon\Html\Image;
4
5
use Intervention\Image\Image as ScaleableImage;
6
7
class FilesizeScaler implements Scaler
8
{
9
    private $stepModifier;
10
11
    public function __construct(float $stopModifier = 0.2)
12
    {
13
        $this->stepModifier = $stopModifier;
14
    }
15
16
    public function getVariations(ScaleableImage $scaleableImage): array
17
    {
18
        $fileSize = $scaleableImage->filesize();
19
        $width = $scaleableImage->width();
20
21
        $ratio = $scaleableImage->height() / $width;
22
        $area = $width * $width * $ratio;
23
        $pixelPrice = $fileSize / $area;
24
25
        $stepAmount = $fileSize * $this->stepModifier;
26
27
        $variations = [];
28
29
        do {
30
            $newWidth = (int) floor(sqrt(($fileSize / $pixelPrice) / $ratio));
31
32
            $variations[$newWidth] = $newWidth * $ratio;
33
34
            $fileSize -= $stepAmount;
35
        } while ($fileSize > 0);
36
37
        return $variations;
38
    }
39
}
40