Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Rectangle.php
1<?php
10
12
14{
15 protected
20
28 public function __construct($width = 0, $height = 0, $x = 0, $y = 0)
29 {
30 $this->setWidth($width)
31 ->setHeight($height)
32 ->setX($x)
33 ->setY($y);
34 }
35
39 public function getX()
40 {
41 return $this->x;
42 }
43
48 public function setX($x)
49 {
50 $this->x = (int)$x;
51 return $this;
52 }
53
57 public function getY()
58 {
59 return $this->y;
60 }
61
66 public function setY($y)
67 {
68 $this->y = (int)$y;
69 return $this;
70 }
71
75 public function getWidth()
76 {
77 return $this->width;
78 }
79
84 public function setWidth($width)
85 {
86 $this->width = (int)$width;
87 return $this;
88 }
89
93 public function getHeight()
94 {
95 return $this->height;
96 }
97
102 public function setHeight($height)
103 {
104 $this->height = (int)$height;
105 return $this;
106 }
107
114 public function resize(Rectangle $destination, $mode)
115 {
116 if($this->width == 0 || $this->height == 0)
117 {
118 return false;
119 }
120
121 if($destination->width == 0 || $destination->height == 0)
122 {
123 return false;
124 }
125
126 if($this->width == $destination->width && $this->height == $destination->height)
127 {
128 return false;
129 }
130
131 $result = false;
132
133 switch($mode)
134 {
135 case File\Image::RESIZE_EXACT:
136 //in this mode we change the source rectangle, the destination one is intact
137 if(($this->width / $this->height) < ($destination->width / $destination->height))
138 {
139 $ratio = $destination->width / $this->width;
140 }
141 else
142 {
143 $ratio = $destination->height / $this->height;
144 }
145
146 $this->x = max(0, round(($this->width / 2) - (($destination->width / 2) / $ratio)));
147 $this->y = max(0, round(($this->height / 2) - (($destination->height / 2) / $ratio)));
148
149 $this->width = round($destination->width / $ratio);
150 $this->height = round($destination->height / $ratio);
151
152 $result = true;
153 break;
154
155 case File\Image::RESIZE_PROPORTIONAL_ALT:
156 case File\Image::RESIZE_PROPORTIONAL:
157 //in this mode we change the destination rectangle, the source one is intact
159 {
160 $width = max($this->width, $this->height);
161 $height = min($this->width, $this->height);
162 }
163 else
164 {
167 }
168 $ratioWidth = $destination->width / $width;
169 $ratioHeight = $destination->height / $height;
170
171 $ratio = min($ratioWidth, $ratioHeight);
172
173 //todo: enlarge?
174 if($ratio > 0 && $ratio < 1)
175 {
176 // scale down
177 $result = true;
178 }
179 else
180 {
181 // destination is larger than source
182 $ratio = 1;
183 }
184 $destination->width = max(1, round($ratio * $this->width));
185 $destination->height = max(1, round($ratio * $this->height));
186
187 break;
188 }
189
190 return $result;
191 }
192
193 public function scale($ratio)
194 {
195 $this->setWidth($this->getWidth() * $ratio)
196 ->setHeight($this->getHeight() * $ratio)
197 ->setX($this->getX() * $ratio)
198 ->setY($this->getY() * $ratio);
199 }
200}
__construct($width=0, $height=0, $x=0, $y=0)
Definition Rectangle.php:28
resize(Rectangle $destination, $mode)
const RESIZE_PROPORTIONAL_ALT
Definition Image.php:23