|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* HiPanel core package |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://hipanel.com/ |
|
6
|
|
|
* @package hipanel-core |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hipanel\widgets\filePreview; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class OutboundDimensions implements [[DimensionsInterface]] and provides |
|
15
|
|
|
* API to resize [[originalDimensions]] to [[targetDimensions]] using the following logic:. |
|
16
|
|
|
* |
|
17
|
|
|
* The smallest side (width or height) of the [[originalDimensions]] will be resized to |
|
18
|
|
|
* the [[targetDimensions]] side. The second side will be resized accordingly to the original |
|
19
|
|
|
* aspect ratio. |
|
20
|
|
|
* |
|
21
|
|
|
* Example: |
|
22
|
|
|
* ```php |
|
23
|
|
|
* $size = new OutboundDimensions( |
|
24
|
|
|
* new Dimensions(500, 1000), |
|
25
|
|
|
* new Dimensions(300, 300) |
|
26
|
|
|
* ); |
|
27
|
|
|
* |
|
28
|
|
|
* $width = $size->getWidth(); // 300 (matches target width) |
|
29
|
|
|
* $height = $size->getHeight(); // 600 (resized to keep aspect ratio) |
|
30
|
|
|
* ``` |
|
31
|
|
|
*/ |
|
32
|
|
View Code Duplication |
class OutboundDimensions implements DimensionsInterface |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var Dimensions |
|
36
|
|
|
*/ |
|
37
|
|
|
private $originalDimensions; |
|
38
|
|
|
/** |
|
39
|
|
|
* @var Dimensions |
|
40
|
|
|
*/ |
|
41
|
|
|
private $targetDimensions; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* OutboundDimensions constructor. |
|
45
|
|
|
* |
|
46
|
|
|
* @param Dimensions $originalDimensions the original image dimensions |
|
47
|
|
|
* @param Dimensions $targetDimensions the target image dimensions |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct(Dimensions $originalDimensions, Dimensions $targetDimensions) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->originalDimensions = $originalDimensions; |
|
52
|
|
|
$this->targetDimensions = $targetDimensions; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return int |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getWidth() |
|
59
|
|
|
{ |
|
60
|
|
|
if ($this->originalDimensions->getWidth() < $this->targetDimensions->getWidth()) { |
|
61
|
|
|
return $this->originalDimensions->getWidth(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if ($this->isVertical()) { |
|
65
|
|
|
return $this->targetDimensions->getWidth(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $this->targetDimensions->getWidth(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return float|int |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getHeight() |
|
75
|
|
|
{ |
|
76
|
|
|
if ($this->originalDimensions->getHeight() < $this->targetDimensions->getHeight()) { |
|
77
|
|
|
return $this->originalDimensions->getHeight(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if ($this->isVertical()) { |
|
81
|
|
|
return intval(ceil($this->targetDimensions->getHeight() / $this->originalDimensions->getRatio())); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return intval(ceil($this->targetDimensions->getHeight() / $this->originalDimensions->getRatio())); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return bool whether image is in the portrait orientation |
|
89
|
|
|
*/ |
|
90
|
|
|
private function isVertical() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->originalDimensions->getRatio() < 1; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* {@inheritdoc} |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getRatio() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->originalDimensions->getRatio(); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.