| 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 |
* |
| 12 |
* @var Image |
| 13 |
*/ |
| 14 |
protected $image; |
| 15 |
|
| 16 |
/** |
| 17 |
* The device pixel ratio. |
| 18 |
* |
| 19 |
* @var float |
| 20 |
*/ |
| 21 |
protected $dpr; |
| 22 |
|
| 23 |
/** |
| 24 |
* Create dimension helper instance. |
| 25 |
* |
| 26 |
* @param Image $image The source image. |
| 27 |
* @param float $dpr The device pixel ratio. |
| 28 |
*/ |
| 29 |
public function __construct(Image $image, $dpr = 1) |
| 30 |
{ |
| 31 |
$this->image = $image; |
| 32 |
$this->dpr = $dpr; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Resolve the dimension. |
| 37 |
* |
| 38 |
* @param string $value The dimension value. |
| 39 |
* |
| 40 |
* @return float|null The resolved dimension. |
| 41 |
*/ |
| 42 |
public function get($value) |
| 43 |
{ |
| 44 |
if (is_numeric($value) and $value > 0) { |
| 45 |
return (float) $value * $this->dpr; |
| 46 |
} |
| 47 |
|
| 48 |
if (preg_match('/^(\d{1,2}(?!\d)|100)(w|h)$/', $value, $matches)) { |
| 49 |
if ('h' === $matches[2]) { |
| 50 |
return (float) $this->image->height() * ((float) $matches[1] / 100); |
| 51 |
} |
| 52 |
|
| 53 |
return (float) $this->image->width() * ((float) $matches[1] / 100); |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|