Dimension::get()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 12
cp 0
rs 9.4888
c 0
b 0
f 0
cc 5
nc 4
nop 1
crap 30
1
<?php
2
3
namespace League\Glide\Manipulators\Helpers;
4
5
use Intervention\Image\Image;
6
7
class Dimension
8
{
9
    /**
10
     * The source image.
11
     * @var Image
12
     */
13
    protected $image;
14
15
    /**
16
     * The device pixel ratio.
17
     * @var integer
18
     */
19
    protected $dpr;
20
21
    /**
22
     * Create dimension helper instance.
23
     * @param Image   $image The source image.
24
     * @param integer $dpr   The device pixel ratio.
25
     */
26
    public function __construct(Image $image, $dpr = 1)
27
    {
28
        $this->image = $image;
29
        $this->dpr = $dpr;
30
    }
31
32
    /**
33
     * Resolve the dimension.
34
     * @param  string $value The dimension value.
35
     * @return double The resolved dimension.
36
     */
37
    public function get($value)
38
    {
39
        if (is_numeric($value) and $value > 0) {
40
            return (double) $value * $this->dpr;
41
        }
42
43
        if (preg_match('/^(\d{1,2}(?!\d)|100)(w|h)$/', $value, $matches)) {
44
            if ($matches[2] === 'h') {
45
                return (double) $this->image->height() * ($matches[1] / 100);
46
            }
47
48
            return (double) $this->image->width() * ($matches[1] / 100);
49
        }
50
    }
51
}
52