Dimension   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 45
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get() 0 14 5
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