|
2 | 2 |
|
3 | 3 | namespace TraderInteractive\Util; |
4 | 4 |
|
| 5 | +use Imagick; |
5 | 6 | use InvalidArgumentException; |
6 | 7 | use TraderInteractive\Util; |
7 | 8 |
|
@@ -56,12 +57,140 @@ final class Image |
56 | 57 | ]; |
57 | 58 |
|
58 | 59 | /** |
59 | | - * Calls @see resizeMulti() with $boxWidth and $boxHeight as a single element in $boxSizes |
| 60 | + * @param Imagick $source The image magick object to resize |
| 61 | + * @param int $boxWidth The final width of the image. |
| 62 | + * @param int $boxHeight The final height of the image. |
| 63 | + * @param array $options Options for the resize operation. |
| 64 | + * |
| 65 | + * @return Imagick |
| 66 | + * |
| 67 | + * @throws \Exception Thrown if options are invalid. |
60 | 68 | */ |
61 | | - public static function resize(\Imagick $source, int $boxWidth, int $boxHeight, array $options = []) : \Imagick |
| 69 | + public static function resize(Imagick $source, int $boxWidth, int $boxHeight, array $options = []) : Imagick |
62 | 70 | { |
63 | | - $results = self::resizeMulti($source, [['width' => $boxWidth, 'height' => $boxHeight]], $options); |
64 | | - return $results[0]; |
| 71 | + $options += self::DEFAULT_OPTIONS; |
| 72 | + |
| 73 | + $color = $options['color']; |
| 74 | + Util::ensure(true, is_string($color), InvalidArgumentException::class, ['$options["color"] was not a string']); |
| 75 | + |
| 76 | + $upsize = $options['upsize']; |
| 77 | + Util::ensure(true, is_bool($upsize), InvalidArgumentException::class, ['$options["upsize"] was not a bool']); |
| 78 | + |
| 79 | + $bestfit = $options['bestfit']; |
| 80 | + Util::ensure(true, is_bool($bestfit), InvalidArgumentException::class, ['$options["bestfit"] was not a bool']); |
| 81 | + |
| 82 | + $blurBackground = $options['blurBackground']; |
| 83 | + Util::ensure( |
| 84 | + true, |
| 85 | + is_bool($blurBackground), |
| 86 | + InvalidArgumentException::class, |
| 87 | + ['$options["blurBackground"] was not a bool'] |
| 88 | + ); |
| 89 | + |
| 90 | + $blurValue = $options['blurValue']; |
| 91 | + Util::ensure( |
| 92 | + true, |
| 93 | + is_float($blurValue), |
| 94 | + InvalidArgumentException::class, |
| 95 | + ['$options["blurValue"] was not a float'] |
| 96 | + ); |
| 97 | + $maxWidth = $options['maxWidth']; |
| 98 | + Util::ensure(true, is_int($maxWidth), InvalidArgumentException::class, ['$options["maxWidth"] was not an int']); |
| 99 | + |
| 100 | + $maxHeight = $options['maxHeight']; |
| 101 | + Util::ensure( |
| 102 | + true, |
| 103 | + is_int($maxHeight), |
| 104 | + InvalidArgumentException::class, |
| 105 | + ['$options["maxHeight"] was not an int'] |
| 106 | + ); |
| 107 | + |
| 108 | + |
| 109 | + if ($boxWidth > $maxWidth || $boxWidth <= 0) { |
| 110 | + throw new InvalidArgumentException('a $boxSizes width was not between 0 and $options["maxWidth"]'); |
| 111 | + } |
| 112 | + |
| 113 | + if ($boxHeight > $maxHeight || $boxHeight <= 0) { |
| 114 | + throw new InvalidArgumentException('a $boxSizes height was not between 0 and $options["maxHeight"]'); |
| 115 | + } |
| 116 | + |
| 117 | + $clone = clone $source; |
| 118 | + |
| 119 | + self::rotateImage($clone); |
| 120 | + |
| 121 | + $width = $clone->getImageWidth(); |
| 122 | + $height = $clone->getImageHeight(); |
| 123 | + |
| 124 | + //ratio over 1 is horizontal, under 1 is vertical |
| 125 | + $boxRatio = $boxWidth / $boxHeight; |
| 126 | + //height should be positive since I didnt find a way you could get zero into imagick |
| 127 | + $originalRatio = $width / $height; |
| 128 | + |
| 129 | + $targetWidth = null; |
| 130 | + $targetHeight = null; |
| 131 | + $targetX = null; |
| 132 | + $targetY = null; |
| 133 | + if ($width < $boxWidth && $height < $boxHeight && !$upsize) { |
| 134 | + $targetWidth = $width; |
| 135 | + $targetHeight = $height; |
| 136 | + $targetX = ($boxWidth - $width) / 2; |
| 137 | + $targetY = ($boxHeight - $height) / 2; |
| 138 | + } else { |
| 139 | + //if box is more vertical than original |
| 140 | + if ($boxRatio < $originalRatio) { |
| 141 | + $targetWidth = $boxWidth; |
| 142 | + $targetHeight = (int)((double)$boxWidth / $originalRatio); |
| 143 | + $targetX = 0; |
| 144 | + $targetY = ($boxHeight - $targetHeight) / 2; |
| 145 | + } else { |
| 146 | + $targetWidth = (int)((double)$boxHeight * $originalRatio); |
| 147 | + $targetHeight = $boxHeight; |
| 148 | + $targetX = ($boxWidth - $targetWidth) / 2; |
| 149 | + $targetY = 0; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + $widthReduced = false; |
| 154 | + if ($width > $targetWidth) { |
| 155 | + $width = $targetWidth; |
| 156 | + $widthReduced = true; |
| 157 | + } |
| 158 | + |
| 159 | + $heightReduced = false; |
| 160 | + if ($height > $targetHeight) { |
| 161 | + $height = $targetHeight; |
| 162 | + $heightReduced = true; |
| 163 | + } |
| 164 | + |
| 165 | + if ($widthReduced || $heightReduced) { |
| 166 | + if ($clone->resizeImage($width, $height, \Imagick::FILTER_BOX, 1.0) !== true) { |
| 167 | + //cumbersome to test |
| 168 | + throw new \Exception('Imagick::resizeImage() did not return true');//@codeCoverageIgnore |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + if ($upsize && ($width < $targetWidth || $height < $targetHeight)) { |
| 173 | + if ($clone->resizeImage($targetWidth, $targetHeight, \Imagick::FILTER_CUBIC, 1.0, $bestfit) !== true) { |
| 174 | + //cumbersome to test |
| 175 | + throw new \Exception('Imagick::resizeImage() did not return true');//@codeCoverageIgnore |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + if ($clone->getImageHeight() === $boxHeight && $clone->getImageWidth() === $boxWidth) { |
| 180 | + return $clone; |
| 181 | + } |
| 182 | + |
| 183 | + //put image in box |
| 184 | + $canvas = self::getBackgroundCanvas($clone, $color, $blurBackground, $blurValue, $boxWidth, $boxHeight); |
| 185 | + if ($canvas->compositeImage($clone, \Imagick::COMPOSITE_ATOP, $targetX, $targetY) !== true) { |
| 186 | + //cumbersome to test |
| 187 | + throw new \Exception('Imagick::compositeImage() did not return true');//@codeCoverageIgnore |
| 188 | + } |
| 189 | + |
| 190 | + //reason we are not supporting the options in self::write() here is because format, and strip headers are |
| 191 | + //only relevant once written Imagick::stripImage() doesnt even have an effect until written |
| 192 | + //also the user can just call that function with the resultant $canvas |
| 193 | + return $canvas; |
65 | 194 | } |
66 | 195 |
|
67 | 196 | /** |
|
0 commit comments