vendor/contao/image/src/ResizeConfiguration.php line 98

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of Contao.
  5. *
  6. * (c) Leo Feyer
  7. *
  8. * @license LGPL-3.0-or-later
  9. */
  10. namespace Contao\Image;
  11. use Contao\Image\Exception\InvalidArgumentException;
  12. class ResizeConfiguration
  13. {
  14. public const MODE_CROP = 'crop';
  15. public const MODE_BOX = 'box';
  16. /**
  17. * @deprecated Deprecated since version 1.2, to be removed in version 2.0.
  18. */
  19. public const MODE_PROPORTIONAL = 'proportional';
  20. /**
  21. * @var int
  22. */
  23. private $width = 0;
  24. /**
  25. * @var int
  26. */
  27. private $height = 0;
  28. /**
  29. * @var string
  30. */
  31. private $mode = self::MODE_CROP;
  32. /**
  33. * @var int
  34. */
  35. private $zoomLevel = 0;
  36. /**
  37. * Returns true if the resize would have no effect.
  38. */
  39. public function isEmpty(): bool
  40. {
  41. return 0 === $this->width && 0 === $this->height && 0 === $this->zoomLevel;
  42. }
  43. public function getWidth(): int
  44. {
  45. return $this->width;
  46. }
  47. public function setWidth(int $width): self
  48. {
  49. if ($width < 0) {
  50. throw new InvalidArgumentException('Width must not be negative');
  51. }
  52. $this->width = $width;
  53. return $this;
  54. }
  55. public function getHeight(): int
  56. {
  57. return $this->height;
  58. }
  59. public function setHeight(int $height): self
  60. {
  61. if ($height < 0) {
  62. throw new InvalidArgumentException('Height must not be negative');
  63. }
  64. $this->height = $height;
  65. return $this;
  66. }
  67. /**
  68. * @return string One of the ResizeConfiguration::MODE_* constants
  69. */
  70. public function getMode(): string
  71. {
  72. return $this->mode;
  73. }
  74. /**
  75. * @param string $mode One of the ResizeConfiguration::MODE_* constants
  76. */
  77. public function setMode(string $mode): self
  78. {
  79. if (!\in_array($mode, [self::MODE_CROP, self::MODE_BOX, self::MODE_PROPORTIONAL], true)) {
  80. throw new InvalidArgumentException('Mode must be one of the '.self::class.'::MODE_* constants');
  81. }
  82. if (self::MODE_PROPORTIONAL === $mode) {
  83. trigger_deprecation('contao/image', '1.2', 'Using ResizeConfiguration::MODE_PROPORTIONAL has been deprecated and will no longer work in version 2.0. Use ResizeConfiguration::MODE_BOX instead.');
  84. }
  85. $this->mode = $mode;
  86. return $this;
  87. }
  88. public function getZoomLevel(): int
  89. {
  90. return $this->zoomLevel;
  91. }
  92. public function setZoomLevel(int $zoomLevel): self
  93. {
  94. if ($zoomLevel < 0 || $zoomLevel > 100) {
  95. throw new InvalidArgumentException('Zoom level must be between 0 and 100');
  96. }
  97. $this->zoomLevel = $zoomLevel;
  98. return $this;
  99. }
  100. }