|
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 InsetDimensions 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 and be less than target side. |
|
20
|
|
|
* |
|
21
|
|
|
* Example: |
|
22
|
|
|
* ```php |
|
23
|
|
|
* $size = new InsetDimensions( |
|
24
|
|
|
* new Dimensions(1000, 500), |
|
25
|
|
|
* new Dimensions(300, 300) |
|
26
|
|
|
* ); |
|
27
|
|
|
* |
|
28
|
|
|
* $width = $size->getWidth(); // 300 (maximum width to fit in target dimensions) |
|
29
|
|
|
* $height = $size->getHeight(); // 150 (height according to the original aspect ratio) |
|
30
|
|
|
* ``` |
|
31
|
|
|
*/ |
|
32
|
|
View Code Duplication |
class InsetDimensions implements DimensionsInterface |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var Dimensions |
|
36
|
|
|
*/ |
|
37
|
|
|
private $originalDimensions; |
|
38
|
|
|
/** |
|
39
|
|
|
* @var Dimensions |
|
40
|
|
|
*/ |
|
41
|
|
|
private $targetDimensions; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* InsetDimensions 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 intval(ceil($this->targetDimensions->getWidth() * $this->originalDimensions->getRatio())); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $this->targetDimensions->getWidth(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return 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 $this->targetDimensions->getHeight(); |
|
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.