Completed
Push — master ( 66d197...72430f )
by Jonathan
03:30
created

Dimension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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 39
    public function __construct(Image $image, $dpr = 1)
27
    {
28 39
        $this->image = $image;
29 39
        $this->dpr = $dpr;
30 39
    }
31
32
    /**
33
     * Resolve the dimension.
34
     * @param  string $value The dimension value.
35
     * @return double The resolved dimension.
36
     */
37 39
    public function get($value)
38
    {
39 39
        if (is_numeric($value) and $value > 0) {
40 27
            return (double) $value * $this->dpr;
41
        }
42
43 15
        if (preg_match('/^(\d{1,2}(?!\d)|100)(w|h)$/', $value, $matches)) {
44 9
            if ($matches[2] === 'h') {
45 6
                return (double) $this->image->height() * ($matches[1] / 100);
46
            }
47
48 6
            return (double) $this->image->width() * ($matches[1] / 100);
49
        }
50 9
    }
51
}
52