o2system /
image
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * This file is part of the O2System Framework package. |
||||
| 4 | * |
||||
| 5 | * For the full copyright and license information, please view the LICENSE |
||||
| 6 | * file that was distributed with this source code. |
||||
| 7 | * |
||||
| 8 | * @author Steeve Andrian Salim |
||||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||||
| 10 | */ |
||||
| 11 | |||||
| 12 | // ------------------------------------------------------------------------ |
||||
| 13 | |||||
| 14 | namespace O2System\Image; |
||||
| 15 | |||||
| 16 | // ------------------------------------------------------------------------ |
||||
| 17 | |||||
| 18 | use O2System\Image\Dimension\Axis; |
||||
| 19 | |||||
| 20 | /** |
||||
| 21 | * Class Dimension |
||||
| 22 | * |
||||
| 23 | * Image dimension object class. |
||||
| 24 | * |
||||
| 25 | * @package O2System\Image |
||||
| 26 | */ |
||||
| 27 | class Dimension |
||||
| 28 | {
|
||||
| 29 | /** |
||||
| 30 | * Dimension::$maintainAspectRatio |
||||
| 31 | * |
||||
| 32 | * Maintain image dimension aspect ratio. |
||||
| 33 | * |
||||
| 34 | * @var bool |
||||
| 35 | */ |
||||
| 36 | public $maintainAspectRatio = true; |
||||
| 37 | |||||
| 38 | /** |
||||
| 39 | * Dimension::$directive |
||||
| 40 | * |
||||
| 41 | * Image dimension directive. |
||||
| 42 | * |
||||
| 43 | * @var string |
||||
| 44 | */ |
||||
| 45 | protected $directive = 'RATIO'; |
||||
| 46 | |||||
| 47 | /** |
||||
| 48 | * Dimension::$orientation |
||||
| 49 | * |
||||
| 50 | * Image dimension orientation. |
||||
| 51 | * |
||||
| 52 | * @var string |
||||
| 53 | */ |
||||
| 54 | protected $orientation = 'AUTO'; |
||||
| 55 | |||||
| 56 | /** |
||||
| 57 | * Dimension::$quadrant |
||||
| 58 | * |
||||
| 59 | * Image dimension quadrant. |
||||
| 60 | * |
||||
| 61 | * @var string |
||||
| 62 | */ |
||||
| 63 | protected $quadrant = 'CENTER'; |
||||
| 64 | |||||
| 65 | /** |
||||
| 66 | * Dimension::$width |
||||
| 67 | * |
||||
| 68 | * Image width dimension. |
||||
| 69 | * |
||||
| 70 | * @var int |
||||
| 71 | */ |
||||
| 72 | protected $width = 0; |
||||
| 73 | |||||
| 74 | /** |
||||
| 75 | * Dimension::$height |
||||
| 76 | * |
||||
| 77 | * Image height dimension. |
||||
| 78 | * |
||||
| 79 | * @var int |
||||
| 80 | */ |
||||
| 81 | protected $height = 0; |
||||
| 82 | |||||
| 83 | /** |
||||
| 84 | * Dimension::$axis |
||||
| 85 | * |
||||
| 86 | * Image dimension axis. |
||||
| 87 | * |
||||
| 88 | * @var Axis |
||||
| 89 | */ |
||||
| 90 | protected $axis; |
||||
| 91 | |||||
| 92 | // ------------------------------------------------------------------------ |
||||
| 93 | |||||
| 94 | /** |
||||
| 95 | * Dimension::__construct |
||||
| 96 | * |
||||
| 97 | * @param int $width Image width. |
||||
| 98 | * @param int $height Image height. |
||||
| 99 | */ |
||||
| 100 | public function __construct($width, $height, $x = 0, $y = 0) |
||||
| 101 | {
|
||||
| 102 | $this->width = (int)$width; |
||||
| 103 | $this->height = (int)$height; |
||||
| 104 | |||||
| 105 | $this->axis = new Axis($x, $y); |
||||
| 106 | } |
||||
| 107 | |||||
| 108 | // ------------------------------------------------------------------------ |
||||
| 109 | |||||
| 110 | /** |
||||
| 111 | * Dimension::getOrientation |
||||
| 112 | * |
||||
| 113 | * @return string |
||||
| 114 | */ |
||||
| 115 | public function getOrientation() |
||||
| 116 | {
|
||||
| 117 | if ($this->orientation === 'AUTO') {
|
||||
| 118 | if ($this->width > $this->height) {
|
||||
| 119 | $this->orientation = 'LANDSCAPE'; |
||||
| 120 | } elseif ($this->width < $this->height) {
|
||||
| 121 | $this->orientation = 'PORTRAIT'; |
||||
| 122 | } elseif ($this->width == $this->height) {
|
||||
| 123 | $this->orientation = 'SQUARE'; |
||||
| 124 | } |
||||
| 125 | } |
||||
| 126 | |||||
| 127 | return $this->orientation; |
||||
| 128 | } |
||||
| 129 | |||||
| 130 | // ------------------------------------------------------------------------ |
||||
| 131 | |||||
| 132 | /** |
||||
| 133 | * Dimension::withOrientation |
||||
| 134 | * |
||||
| 135 | * Get new image dimension with defined orientation. |
||||
| 136 | * |
||||
| 137 | * @param string $orientation Supported image orientation: |
||||
| 138 | * 1. AUTO |
||||
| 139 | * 2. LANDSCAPE |
||||
| 140 | * 3. PORTRAIT |
||||
| 141 | * 4. SQUARE |
||||
| 142 | * |
||||
| 143 | * @return Dimension |
||||
| 144 | */ |
||||
| 145 | public function withOrientation($orientation) |
||||
| 146 | {
|
||||
| 147 | $newDimension = clone $this; |
||||
| 148 | |||||
| 149 | if (in_array($orientation, ['AUTO', 'LANDSCAPE', 'PORTRAIT', 'SQUARE'])) {
|
||||
| 150 | $newDimension->orientation = $orientation; |
||||
| 151 | } |
||||
| 152 | |||||
| 153 | return $newDimension; |
||||
| 154 | } |
||||
| 155 | |||||
| 156 | // ------------------------------------------------------------------------ |
||||
| 157 | |||||
| 158 | /** |
||||
| 159 | * Dimension::withDirective |
||||
| 160 | * |
||||
| 161 | * Get new image dimension with defined directive. |
||||
| 162 | * |
||||
| 163 | * return Dimension |
||||
| 164 | */ |
||||
| 165 | public function withDirective($directive) |
||||
| 166 | {
|
||||
| 167 | $newDimension = clone $this; |
||||
| 168 | |||||
| 169 | if (in_array($directive, ['RATIO', 'UP', 'DOWN'])) {
|
||||
| 170 | $newDimension->directive = $directive; |
||||
| 171 | } |
||||
| 172 | |||||
| 173 | return $newDimension; |
||||
| 174 | } |
||||
| 175 | |||||
| 176 | // ------------------------------------------------------------------------ |
||||
| 177 | |||||
| 178 | /** |
||||
| 179 | * Dimension::getFocus |
||||
| 180 | * |
||||
| 181 | * Gets defined dimension focus. |
||||
| 182 | * |
||||
| 183 | * @return string |
||||
| 184 | */ |
||||
| 185 | public function getFocus() |
||||
| 186 | {
|
||||
| 187 | return $this->quadrant; |
||||
| 188 | } |
||||
| 189 | |||||
| 190 | // ------------------------------------------------------------------------ |
||||
| 191 | |||||
| 192 | /** |
||||
| 193 | * Dimension::withFocus |
||||
| 194 | * |
||||
| 195 | * Get new image dimension with defined focus. |
||||
| 196 | * |
||||
| 197 | * +----+----+----+ |
||||
| 198 | * | NW | N | NE | |
||||
| 199 | * +----+----+----+ |
||||
| 200 | * | W | C | E | |
||||
| 201 | * +----+----+----+ |
||||
| 202 | * | SW | S | SE | |
||||
| 203 | * +----+----+----+ |
||||
| 204 | * |
||||
| 205 | * @param string $focus Supported image quadrant: |
||||
| 206 | * 1. CENTER |
||||
| 207 | * 2. NORTH |
||||
| 208 | * 3. NORTHWEST |
||||
| 209 | * 4. NORTHEAST |
||||
| 210 | * 5. SOUTH |
||||
| 211 | * 6. SOUTHWEST |
||||
| 212 | * 7. SOUTHEAST |
||||
| 213 | * 8. WEST |
||||
| 214 | * 9. EAST |
||||
| 215 | * |
||||
| 216 | * @return Dimension |
||||
| 217 | */ |
||||
| 218 | public function withFocus($focus) |
||||
| 219 | {
|
||||
| 220 | $newDimension = clone $this; |
||||
| 221 | |||||
| 222 | if (in_array($focus, |
||||
| 223 | ['CENTER', 'NORTH', 'NORTHWEST', 'NORTHEAST', 'SOUTH', 'SOUTHWEST', 'SOUTHEAST', 'EAST', 'WEST'])) {
|
||||
| 224 | $newDimension->quadrant = $focus; |
||||
| 225 | } |
||||
| 226 | |||||
| 227 | return $newDimension; |
||||
| 228 | } |
||||
| 229 | |||||
| 230 | // ------------------------------------------------------------------------ |
||||
| 231 | |||||
| 232 | /** |
||||
| 233 | * Dimension::getAxis |
||||
| 234 | * |
||||
| 235 | * Gets image dimension axis. |
||||
| 236 | * |
||||
| 237 | * @return \O2System\Image\Dimension\Axis |
||||
| 238 | */ |
||||
| 239 | public function getAxis() |
||||
| 240 | {
|
||||
| 241 | return $this->axis; |
||||
| 242 | } |
||||
| 243 | |||||
| 244 | // ------------------------------------------------------------------------ |
||||
| 245 | |||||
| 246 | /** |
||||
| 247 | * Dimension::withAxis |
||||
| 248 | * |
||||
| 249 | * Gets new image file with new sets of axis. |
||||
| 250 | * |
||||
| 251 | * @param Axis $axis New image axis. |
||||
| 252 | * |
||||
| 253 | * @return Dimension |
||||
| 254 | */ |
||||
| 255 | public function withAxis(Axis $axis) |
||||
| 256 | {
|
||||
| 257 | $newDimension = clone $this; |
||||
| 258 | $newDimension->axis = $axis; |
||||
| 259 | |||||
| 260 | return $newDimension; |
||||
| 261 | } |
||||
| 262 | |||||
| 263 | // ------------------------------------------------------------------------ |
||||
| 264 | |||||
| 265 | /** |
||||
| 266 | * Dimension::getRatio |
||||
| 267 | * |
||||
| 268 | * Gets image dimension ratio. |
||||
| 269 | * |
||||
| 270 | * @return int |
||||
| 271 | */ |
||||
| 272 | public function getRatio() |
||||
| 273 | {
|
||||
| 274 | return (int)round($this->width / $this->height); |
||||
| 275 | } |
||||
| 276 | |||||
| 277 | // ------------------------------------------------------------------------ |
||||
| 278 | |||||
| 279 | /** |
||||
| 280 | * Dimension::withWidth |
||||
| 281 | * |
||||
| 282 | * Gets new image dimension with defined width. |
||||
| 283 | * |
||||
| 284 | * @param int $newWidth New image width. |
||||
| 285 | * |
||||
| 286 | * @return \O2System\Image\Dimension |
||||
| 287 | */ |
||||
| 288 | public function withWidth($newWidth) |
||||
| 289 | {
|
||||
| 290 | return $this->withSize($newWidth, ceil($newWidth * $this->height / $this->width)); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 291 | } |
||||
| 292 | |||||
| 293 | // ------------------------------------------------------------------------ |
||||
| 294 | |||||
| 295 | /** |
||||
| 296 | * Dimension::withSize |
||||
| 297 | * |
||||
| 298 | * Gets new image dimension with defined width and height. |
||||
| 299 | * |
||||
| 300 | * @param int $newWidth New image width. |
||||
| 301 | * @param int $newHeight New image height. |
||||
| 302 | * |
||||
| 303 | * @return \O2System\Image\Dimension |
||||
| 304 | */ |
||||
| 305 | public function withSize($newWidth, $newHeight) |
||||
| 306 | {
|
||||
| 307 | $newDimension = clone $this; |
||||
| 308 | |||||
| 309 | if ($newWidth == 0 && $newHeight > 0) {
|
||||
| 310 | $newDimension->withHeight($newHeight); |
||||
| 311 | $newWidth = $newDimension->getWidth(); |
||||
| 312 | } else {
|
||||
| 313 | if ($newWidth > 0 && $newHeight == 0) {
|
||||
| 314 | $newDimension->withWidth($newWidth); |
||||
| 315 | $newHeight = $newDimension->getHeight(); |
||||
| 316 | } |
||||
| 317 | } |
||||
| 318 | |||||
| 319 | $targetDimension = clone $this; |
||||
| 320 | |||||
| 321 | if ($this->directive === 'UP') {
|
||||
| 322 | if ($newWidth > $this->width) {
|
||||
| 323 | $targetDimension->width = $newWidth; |
||||
| 324 | $targetDimension->height = $newHeight; |
||||
| 325 | |||||
| 326 | return $targetDimension; |
||||
| 327 | } elseif ($newWidth < $this->width) {
|
||||
| 328 | return $targetDimension; |
||||
| 329 | } |
||||
| 330 | } elseif ($this->directive === 'DOWN') {
|
||||
| 331 | if ($newWidth > $this->width) {
|
||||
| 332 | return $targetDimension; |
||||
| 333 | } elseif ($newWidth < $this->width) {
|
||||
| 334 | $targetDimension->width = $newWidth; |
||||
| 335 | $targetDimension->height = $newHeight; |
||||
| 336 | |||||
| 337 | return $targetDimension; |
||||
| 338 | } |
||||
| 339 | } |
||||
| 340 | |||||
| 341 | if ($this->maintainAspectRatio) {
|
||||
| 342 | $targetDimension->width = $newWidth > $this->width ? $this->width : $newWidth; |
||||
| 343 | $targetDimension->height = $newHeight > $this->height ? $this->height : $newHeight; |
||||
| 344 | } else {
|
||||
| 345 | $targetDimension->width = (int)$newWidth; |
||||
| 346 | $targetDimension->height = (int)$newHeight; |
||||
| 347 | } |
||||
| 348 | |||||
| 349 | return $targetDimension; |
||||
| 350 | } |
||||
| 351 | |||||
| 352 | // ------------------------------------------------------------------------ |
||||
| 353 | |||||
| 354 | /** |
||||
| 355 | * Dimension::withHeight |
||||
| 356 | * |
||||
| 357 | * Gets new image dimension with defined height. |
||||
| 358 | * |
||||
| 359 | * @param int $newHeight New image height. |
||||
| 360 | * |
||||
| 361 | * @return \O2System\Image\Dimension |
||||
| 362 | */ |
||||
| 363 | public function withHeight($newHeight) |
||||
| 364 | {
|
||||
| 365 | return $this->withSize(ceil($this->width * $newHeight / $this->height), $newHeight); |
||||
|
0 ignored issues
–
show
ceil($this->width * $newHeight / $this->height) of type double is incompatible with the type integer expected by parameter $newWidth of O2System\Image\Dimension::withSize().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 366 | } |
||||
| 367 | |||||
| 368 | // ------------------------------------------------------------------------ |
||||
| 369 | |||||
| 370 | /** |
||||
| 371 | * Dimension::getWidth |
||||
| 372 | * |
||||
| 373 | * Gets image dimension width. |
||||
| 374 | * |
||||
| 375 | * @return int |
||||
| 376 | */ |
||||
| 377 | public function getWidth() |
||||
| 378 | {
|
||||
| 379 | return $this->width; |
||||
| 380 | } |
||||
| 381 | |||||
| 382 | // ------------------------------------------------------------------------ |
||||
| 383 | |||||
| 384 | /** |
||||
| 385 | * Dimension::getHeight |
||||
| 386 | * |
||||
| 387 | * Gets image dimension height. |
||||
| 388 | * |
||||
| 389 | * @return int |
||||
| 390 | */ |
||||
| 391 | public function getHeight() |
||||
| 392 | {
|
||||
| 393 | return $this->height; |
||||
| 394 | } |
||||
| 395 | |||||
| 396 | // ------------------------------------------------------------------------ |
||||
| 397 | |||||
| 398 | /** |
||||
| 399 | * Dimension::withScale |
||||
| 400 | * |
||||
| 401 | * Gets new image dimension with defined scale. |
||||
| 402 | * |
||||
| 403 | * @param int $scale New image scale. |
||||
| 404 | * |
||||
| 405 | * @return \O2System\Image\Dimension |
||||
| 406 | */ |
||||
| 407 | public function withScale($scale) |
||||
| 408 | {
|
||||
| 409 | $newDimension = clone $this; |
||||
| 410 | |||||
| 411 | return $newDimension->withSize( |
||||
| 412 | round($this->width * ($scale / 100)), |
||||
|
0 ignored issues
–
show
round($this->width * $scale / 100) of type double is incompatible with the type integer expected by parameter $newWidth of O2System\Image\Dimension::withSize().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 413 | round($this->height * ($scale / 100)) |
||||
|
0 ignored issues
–
show
round($this->height * $scale / 100) of type double is incompatible with the type integer expected by parameter $newHeight of O2System\Image\Dimension::withSize().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 414 | ); |
||||
| 415 | } |
||||
| 416 | } |