| 1 |
<?php |
| 2 |
|
| 3 |
namespace League\Glide\Manipulators; |
| 4 |
|
| 5 |
use Intervention\Image\Image; |
| 6 |
|
| 7 |
/** |
| 8 |
* @property string $dpr |
| 9 |
* @property string|null $fit |
| 10 |
* @property string $h |
| 11 |
* @property string $w |
| 12 |
*/ |
| 13 |
class Size extends BaseManipulator |
| 14 |
{ |
| 15 |
/** |
| 16 |
* Maximum image size in pixels. |
| 17 |
* |
| 18 |
* @var int|null |
| 19 |
*/ |
| 20 |
protected $maxImageSize; |
| 21 |
|
| 22 |
/** |
| 23 |
* Create Size instance. |
| 24 |
* |
| 25 |
* @param int|null $maxImageSize Maximum image size in pixels. |
| 26 |
*/ |
| 27 |
public function __construct($maxImageSize = null) |
| 28 |
{ |
| 29 |
$this->maxImageSize = $maxImageSize; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Set the maximum image size. |
| 34 |
* |
| 35 |
* @param int|null Maximum image size in pixels. |
| 36 |
* |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
public function setMaxImageSize($maxImageSize) |
| 40 |
{ |
| 41 |
$this->maxImageSize = $maxImageSize; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Get the maximum image size. |
| 46 |
* |
| 47 |
* @return int|null Maximum image size in pixels. |
| 48 |
*/ |
| 49 |
public function getMaxImageSize() |
| 50 |
{ |
| 51 |
return $this->maxImageSize; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Perform size image manipulation. |
| 56 |
* |
| 57 |
* @param Image $image The source image. |
| 58 |
* |
| 59 |
* @return Image The manipulated image. |
| 60 |
*/ |
| 61 |
public function run(Image $image) |
| 62 |
{ |
| 63 |
$width = $this->getWidth(); |
| 64 |
$height = $this->getHeight(); |
| 65 |
$fit = $this->getFit(); |
| 66 |
$dpr = $this->getDpr(); |
| 67 |
|
| 68 |
list($width, $height) = $this->resolveMissingDimensions($image, $width, $height); |
| 69 |
list($width, $height) = $this->applyDpr($width, $height, $dpr); |
| 70 |
list($width, $height) = $this->limitImageSize($width, $height); |
| 71 |
|
| 72 |
if ((int) $width !== (int) $image->width() || (int) $height !== (int) $image->height() || 1.0 !== $this->getCrop()[2]) { |
| 73 |
$image = $this->runResize($image, $fit, (int) $width, (int) $height); |
| 74 |
} |
| 75 |
|
| 76 |
return $image; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Resolve width. |
| 81 |
* |
| 82 |
* @return int|null The resolved width. |
| 83 |
*/ |
| 84 |
public function getWidth() |
| 85 |
{ |
| 86 |
if (!is_numeric($this->w)) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
if ($this->w <= 0) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
return (int) $this->w; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Resolve height. |
| 99 |
* |
| 100 |
* @return int|null The resolved height. |
| 101 |
*/ |
| 102 |
public function getHeight() |
| 103 |
{ |
| 104 |
if (!is_numeric($this->h)) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
if ($this->h <= 0) { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
return (int) $this->h; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Resolve fit. |
| 117 |
* |
| 118 |
* @return string The resolved fit. |
| 119 |
*/ |
| 120 |
public function getFit() |
| 121 |
{ |
| 122 |
if (null === $this->fit) { |
| 123 |
return 'contain'; |
| 124 |
} |
| 125 |
|
| 126 |
if (in_array($this->fit, ['contain', 'fill', 'max', 'stretch', 'fill-max'], true)) { |
| 127 |
return $this->fit; |
| 128 |
} |
| 129 |
|
| 130 |
if (preg_match('/^(crop)(-top-left|-top|-top-right|-left|-center|-right|-bottom-left|-bottom|-bottom-right|-[\d]{1,3}-[\d]{1,3}(?:-[\d]{1,3}(?:\.\d+)?)?)*$/', $this->fit)) { |
| 131 |
return 'crop'; |
| 132 |
} |
| 133 |
|
| 134 |
return 'contain'; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Resolve the device pixel ratio. |
| 139 |
* |
| 140 |
* @return float The device pixel ratio. |
| 141 |
*/ |
| 142 |
public function getDpr() |
| 143 |
{ |
| 144 |
if (!is_numeric($this->dpr)) { |
| 145 |
return 1.0; |
| 146 |
} |
| 147 |
|
| 148 |
if ($this->dpr < 0 or $this->dpr > 8) { |
| 149 |
return 1.0; |
| 150 |
} |
| 151 |
|
| 152 |
return (float) $this->dpr; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Resolve missing image dimensions. |
| 157 |
* |
| 158 |
* @param Image $image The source image. |
| 159 |
* @param int|null $width The image width. |
| 160 |
* @param int|null $height The image height. |
| 161 |
* |
| 162 |
* @return int[] The resolved width and height. |
| 163 |
*/ |
| 164 |
public function resolveMissingDimensions(Image $image, $width, $height) |
| 165 |
{ |
| 166 |
if (is_null($width) and is_null($height)) { |
| 167 |
$width = $image->width(); |
| 168 |
$height = $image->height(); |
| 169 |
} |
| 170 |
|
| 171 |
if (is_null($width) || is_null($height)) { |
| 172 |
$size = (new \Intervention\Image\Size($image->width(), $image->height())) |
| 173 |
->resize($width, $height, function ($constraint) { |
| 174 |
$constraint->aspectRatio(); |
| 175 |
}); |
| 176 |
|
| 177 |
$width = $size->getWidth(); |
| 178 |
$height = $size->getHeight(); |
| 179 |
} |
| 180 |
|
| 181 |
return [ |
| 182 |
(int) $width, |
| 183 |
(int) $height, |
| 184 |
]; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Apply the device pixel ratio. |
| 189 |
* |
| 190 |
* @param int $width The target image width. |
| 191 |
* @param int $height The target image height. |
| 192 |
* @param float $dpr The device pixel ratio. |
| 193 |
* |
| 194 |
* @return int[] The modified width and height. |
| 195 |
*/ |
| 196 |
public function applyDpr($width, $height, $dpr) |
| 197 |
{ |
| 198 |
$width = $width * $dpr; |
| 199 |
$height = $height * $dpr; |
| 200 |
|
| 201 |
return [ |
| 202 |
(int) round($width), |
| 203 |
(int) round($height), |
| 204 |
]; |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Limit image size to maximum allowed image size. |
| 209 |
* |
| 210 |
* @param int $width The image width. |
| 211 |
* @param int $height The image height. |
| 212 |
* |
| 213 |
* @return int[] The limited width and height. |
| 214 |
*/ |
| 215 |
public function limitImageSize($width, $height) |
| 216 |
{ |
| 217 |
if (null !== $this->maxImageSize) { |
| 218 |
$imageSize = $width * $height; |
| 219 |
|
| 220 |
if ($imageSize > $this->maxImageSize) { |
| 221 |
$width = $width / sqrt($imageSize / $this->maxImageSize); |
| 222 |
$height = $height / sqrt($imageSize / $this->maxImageSize); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
return [ |
| 227 |
(int) $width, |
| 228 |
(int) $height, |
| 229 |
]; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Perform resize image manipulation. |
| 234 |
* |
| 235 |
* @param Image $image The source image. |
| 236 |
* @param string $fit The fit. |
| 237 |
* @param int $width The width. |
| 238 |
* @param int $height The height. |
| 239 |
* |
| 240 |
* @return Image The manipulated image. |
| 241 |
*/ |
| 242 |
public function runResize(Image $image, $fit, $width, $height) |
| 243 |
{ |
| 244 |
if ('contain' === $fit) { |
| 245 |
return $this->runContainResize($image, $width, $height); |
| 246 |
} |
| 247 |
|
| 248 |
if ('fill' === $fit) { |
| 249 |
return $this->runFillResize($image, $width, $height); |
| 250 |
} |
| 251 |
|
| 252 |
if ('fill-max' === $fit) { |
| 253 |
return $this->runFillMaxResize($image, $width, $height); |
| 254 |
} |
| 255 |
|
| 256 |
if ('max' === $fit) { |
| 257 |
return $this->runMaxResize($image, $width, $height); |
| 258 |
} |
| 259 |
|
| 260 |
if ('stretch' === $fit) { |
| 261 |
return $this->runStretchResize($image, $width, $height); |
| 262 |
} |
| 263 |
|
| 264 |
if ('crop' === $fit) { |
| 265 |
return $this->runCropResize($image, $width, $height); |
| 266 |
} |
| 267 |
|
| 268 |
return $image; |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Perform contain resize image manipulation. |
| 273 |
* |
| 274 |
* @param Image $image The source image. |
| 275 |
* @param int $width The width. |
| 276 |
* @param int $height The height. |
| 277 |
* |
| 278 |
* @return Image The manipulated image. |
| 279 |
*/ |
| 280 |
public function runContainResize(Image $image, $width, $height) |
| 281 |
{ |
| 282 |
return $image->resize($width, $height, function ($constraint) { |
| 283 |
$constraint->aspectRatio(); |
| 284 |
}); |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Perform max resize image manipulation. |
| 289 |
* |
| 290 |
* @param Image $image The source image. |
| 291 |
* @param int $width The width. |
| 292 |
* @param int $height The height. |
| 293 |
* |
| 294 |
* @return Image The manipulated image. |
| 295 |
*/ |
| 296 |
public function runMaxResize(Image $image, $width, $height) |
| 297 |
{ |
| 298 |
return $image->resize($width, $height, function ($constraint) { |
| 299 |
$constraint->aspectRatio(); |
| 300 |
$constraint->upsize(); |
| 301 |
}); |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Perform fill resize image manipulation. |
| 306 |
* |
| 307 |
* @param Image $image The source image. |
| 308 |
* @param int $width The width. |
| 309 |
* @param int $height The height. |
| 310 |
* |
| 311 |
* @return Image The manipulated image. |
| 312 |
*/ |
| 313 |
public function runFillResize($image, $width, $height) |
| 314 |
{ |
| 315 |
$image = $this->runMaxResize($image, $width, $height); |
| 316 |
|
| 317 |
return $image->resizeCanvas($width, $height, 'center'); |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Perform fill-max resize image manipulation. |
| 322 |
* |
| 323 |
* @param Image $image The source image. |
| 324 |
* @param int $width The width. |
| 325 |
* @param int $height The height. |
| 326 |
* |
| 327 |
* @return Image The manipulated image. |
| 328 |
*/ |
| 329 |
public function runFillMaxResize(Image $image, $width, $height) |
| 330 |
{ |
| 331 |
$image = $image->resize($width, $height, function ($constraint) { |
| 332 |
$constraint->aspectRatio(); |
| 333 |
}); |
| 334 |
|
| 335 |
return $image->resizeCanvas($width, $height, 'center'); |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* Perform stretch resize image manipulation. |
| 340 |
* |
| 341 |
* @param Image $image The source image. |
| 342 |
* @param int $width The width. |
| 343 |
* @param int $height The height. |
| 344 |
* |
| 345 |
* @return Image The manipulated image. |
| 346 |
*/ |
| 347 |
public function runStretchResize(Image $image, $width, $height) |
| 348 |
{ |
| 349 |
return $image->resize($width, $height); |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* Perform crop resize image manipulation. |
| 354 |
* |
| 355 |
* @param Image $image The source image. |
| 356 |
* @param int $width The width. |
| 357 |
* @param int $height The height. |
| 358 |
* |
| 359 |
* @return Image The manipulated image. |
| 360 |
*/ |
| 361 |
public function runCropResize(Image $image, $width, $height) |
| 362 |
{ |
| 363 |
list($resize_width, $resize_height) = $this->resolveCropResizeDimensions($image, $width, $height); |
| 364 |
|
| 365 |
$zoom = $this->getCrop()[2]; |
| 366 |
|
| 367 |
$image->resize($resize_width * $zoom, $resize_height * $zoom, function ($constraint) { |
| 368 |
$constraint->aspectRatio(); |
| 369 |
}); |
| 370 |
|
| 371 |
list($offset_x, $offset_y) = $this->resolveCropOffset($image, $width, $height); |
| 372 |
|
| 373 |
return $image->crop($width, $height, $offset_x, $offset_y); |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
* Resolve the crop resize dimensions. |
| 378 |
* |
| 379 |
* @param Image $image The source image. |
| 380 |
* @param int $width The width. |
| 381 |
* @param int $height The height. |
| 382 |
* |
| 383 |
* @return array The resize dimensions. |
| 384 |
*/ |
| 385 |
public function resolveCropResizeDimensions(Image $image, $width, $height) |
| 386 |
{ |
| 387 |
if ($height > $width * ($image->height() / $image->width())) { |
| 388 |
return [$height * ($image->width() / $image->height()), $height]; |
| 389 |
} |
| 390 |
|
| 391 |
return [$width, $width * ($image->height() / $image->width())]; |
| 392 |
} |
| 393 |
|
| 394 |
/** |
| 395 |
* Resolve the crop offset. |
| 396 |
* |
| 397 |
* @param Image $image The source image. |
| 398 |
* @param int $width The width. |
| 399 |
* @param int $height The height. |
| 400 |
* |
| 401 |
* @return array The crop offset. |
| 402 |
*/ |
| 403 |
public function resolveCropOffset(Image $image, $width, $height) |
| 404 |
{ |
| 405 |
list($offset_percentage_x, $offset_percentage_y) = $this->getCrop(); |
| 406 |
|
| 407 |
$offset_x = (int) (($image->width() * $offset_percentage_x / 100) - ($width / 2)); |
| 408 |
$offset_y = (int) (($image->height() * $offset_percentage_y / 100) - ($height / 2)); |
| 409 |
|
| 410 |
$max_offset_x = $image->width() - $width; |
| 411 |
$max_offset_y = $image->height() - $height; |
| 412 |
|
| 413 |
if ($offset_x < 0) { |
| 414 |
$offset_x = 0; |
| 415 |
} |
| 416 |
|
| 417 |
if ($offset_y < 0) { |
| 418 |
$offset_y = 0; |
| 419 |
} |
| 420 |
|
| 421 |
if ($offset_x > $max_offset_x) { |
| 422 |
$offset_x = $max_offset_x; |
| 423 |
} |
| 424 |
|
| 425 |
if ($offset_y > $max_offset_y) { |
| 426 |
$offset_y = $max_offset_y; |
| 427 |
} |
| 428 |
|
| 429 |
return [$offset_x, $offset_y]; |
| 430 |
} |
| 431 |
|
| 432 |
/** |
| 433 |
* Resolve crop with zoom. |
| 434 |
* |
| 435 |
* @return (float|int)[] The resolved crop. |
| 436 |
* |
| 437 |
* @psalm-return array{0: int, 1: int, 2: float} |
| 438 |
*/ |
| 439 |
public function getCrop() |
| 440 |
{ |
| 441 |
$cropMethods = [ |
| 442 |
'crop-top-left' => [0, 0, 1.0], |
| 443 |
'crop-top' => [50, 0, 1.0], |
| 444 |
'crop-top-right' => [100, 0, 1.0], |
| 445 |
'crop-left' => [0, 50, 1.0], |
| 446 |
'crop-center' => [50, 50, 1.0], |
| 447 |
'crop-right' => [100, 50, 1.0], |
| 448 |
'crop-bottom-left' => [0, 100, 1.0], |
| 449 |
'crop-bottom' => [50, 100, 1.0], |
| 450 |
'crop-bottom-right' => [100, 100, 1.0], |
| 451 |
]; |
| 452 |
|
| 453 |
if (null === $this->fit) { |
| 454 |
return [50, 50, 1.0]; |
| 455 |
} |
| 456 |
|
| 457 |
if (array_key_exists($this->fit, $cropMethods)) { |
| 458 |
return $cropMethods[$this->fit]; |
| 459 |
} |
| 460 |
|
| 461 |
if (preg_match('/^crop-([\d]{1,3})-([\d]{1,3})(?:-([\d]{1,3}(?:\.\d+)?))*$/', $this->fit, $matches)) { |
| 462 |
$matches[3] = isset($matches[3]) ? $matches[3] : 1; |
| 463 |
|
| 464 |
if ($matches[1] > 100 or $matches[2] > 100 or $matches[3] > 100) { |
| 465 |
return [50, 50, 1.0]; |
| 466 |
} |
| 467 |
|
| 468 |
return [ |
| 469 |
(int) $matches[1], |
| 470 |
(int) $matches[2], |
| 471 |
(float) $matches[3], |
| 472 |
]; |
| 473 |
} |
| 474 |
|
| 475 |
return [50, 50, 1.0]; |
| 476 |
} |
| 477 |
} |
| 478 |
|