Passed
Push — master ( f99852...46c8e4 )
by Brent
04:34 queued 02:08
created

FileSizeScaler::getImageFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
namespace Brendt\Image\Scaler;
4
5
use Intervention\Image\Image;
6
use Symfony\Component\Finder\SplFileInfo;
7
8
class FileSizeScaler extends AbstractScaler
9
{
10
11
    /**
12
     * @param SplFileInfo $sourceFile
13
     * @param Image       $imageObject
14
     *
15
     * @return array
16
     */
17
    public function scale(SplFileInfo $sourceFile, Image $imageObject) : array {
18
        $fileSize = $sourceFile->getSize();
19
        $width = $imageObject->getWidth();
20
        $height = $imageObject->getHeight();
21
        $ratio = $height / $width;
22
        $area = $width * $width * $ratio;
23
        $pixelPrice = $fileSize / $area;
24
25
        $sizes = [];
26
27
        do {
28
            // Magic formula.
29
            $newWidth = floor(sqrt(($fileSize / $pixelPrice) / $ratio));
30
31
            $sizes[(int) $newWidth] = (int) $newWidth * $ratio;
32
33
            $fileSize = $fileSize * $this->stepModifier;
34
        } while ($fileSize > $this->minFileSize && $newWidth > $this->minWidth);
35
36
        return $sizes;
37
    }
38
}
39