| 1 |
<?php |
| 2 |
|
| 3 |
namespace League\Glide\Manipulators; |
| 4 |
|
| 5 |
use Intervention\Image\Image; |
| 6 |
use League\Glide\Manipulators\Helpers\Color; |
| 7 |
use League\Glide\Manipulators\Helpers\Dimension; |
| 8 |
|
| 9 |
/** |
| 10 |
* @property string $border |
| 11 |
* @property string $dpr |
| 12 |
*/ |
| 13 |
class Border extends BaseManipulator |
| 14 |
{ |
| 15 |
/** |
| 16 |
* Perform border image manipulation. |
| 17 |
* |
| 18 |
* @param Image $image The source image. |
| 19 |
* |
| 20 |
* @return Image The manipulated image. |
| 21 |
*/ |
| 22 |
public function run(Image $image) |
| 23 |
{ |
| 24 |
if ($border = $this->getBorder($image)) { |
| 25 |
list($width, $color, $method) = $border; |
| 26 |
|
| 27 |
if ('overlay' === $method) { |
| 28 |
return $this->runOverlay($image, $width, $color); |
| 29 |
} |
| 30 |
|
| 31 |
if ('shrink' === $method) { |
| 32 |
return $this->runShrink($image, $width, $color); |
| 33 |
} |
| 34 |
|
| 35 |
if ('expand' === $method) { |
| 36 |
return $this->runExpand($image, $width, $color); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
return $image; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Resolve border amount. |
| 45 |
* |
| 46 |
* @param Image $image The source image. |
| 47 |
* |
| 48 |
* @return (float|string)[]|null The resolved border amount. |
| 49 |
* |
| 50 |
* @psalm-return array{0: float, 1: string, 2: string}|null |
| 51 |
*/ |
| 52 |
public function getBorder(Image $image) |
| 53 |
{ |
| 54 |
if (!$this->border) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
$values = explode(',', $this->border); |
| 59 |
|
| 60 |
$width = $this->getWidth($image, $this->getDpr(), isset($values[0]) ? $values[0] : null); |
| 61 |
$color = $this->getColor(isset($values[1]) ? $values[1] : null); |
| 62 |
$method = $this->getMethod(isset($values[2]) ? $values[2] : null); |
| 63 |
|
| 64 |
if ($width) { |
| 65 |
return [$width, $color, $method]; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get border width. |
| 71 |
* |
| 72 |
* @param Image $image The source image. |
| 73 |
* @param float $dpr The device pixel ratio. |
| 74 |
* @param string $width The border width. |
| 75 |
* |
| 76 |
* @return float|null The resolved border width. |
| 77 |
*/ |
| 78 |
public function getWidth(Image $image, $dpr, $width) |
| 79 |
{ |
| 80 |
return (new Dimension($image, $dpr))->get($width); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Get formatted color. |
| 85 |
* |
| 86 |
* @param string $color The color. |
| 87 |
* |
| 88 |
* @return string The formatted color. |
| 89 |
*/ |
| 90 |
public function getColor($color) |
| 91 |
{ |
| 92 |
return (new Color($color))->formatted(); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Resolve the border method. |
| 97 |
* |
| 98 |
* @param string $method The raw border method. |
| 99 |
* |
| 100 |
* @return string The resolved border method. |
| 101 |
*/ |
| 102 |
public function getMethod($method) |
| 103 |
{ |
| 104 |
if (!in_array($method, ['expand', 'shrink', 'overlay'], true)) { |
| 105 |
return 'overlay'; |
| 106 |
} |
| 107 |
|
| 108 |
return $method; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Resolve the device pixel ratio. |
| 113 |
* |
| 114 |
* @return float The device pixel ratio. |
| 115 |
*/ |
| 116 |
public function getDpr() |
| 117 |
{ |
| 118 |
if (!is_numeric($this->dpr)) { |
| 119 |
return 1.0; |
| 120 |
} |
| 121 |
|
| 122 |
if ($this->dpr < 0 or $this->dpr > 8) { |
| 123 |
return 1.0; |
| 124 |
} |
| 125 |
|
| 126 |
return (float) $this->dpr; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Run the overlay border method. |
| 131 |
* |
| 132 |
* @param Image $image The source image. |
| 133 |
* @param float $width The border width. |
| 134 |
* @param string $color The border color. |
| 135 |
* |
| 136 |
* @return Image The manipulated image. |
| 137 |
*/ |
| 138 |
public function runOverlay(Image $image, $width, $color) |
| 139 |
{ |
| 140 |
return $image->rectangle( |
| 141 |
(int) round($width / 2), |
| 142 |
(int) round($width / 2), |
| 143 |
(int) round($image->width() - ($width / 2)), |
| 144 |
(int) round($image->height() - ($width / 2)), |
| 145 |
function ($draw) use ($width, $color) { |
| 146 |
$draw->border($width, $color); |
| 147 |
} |
| 148 |
); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Run the shrink border method. |
| 153 |
* |
| 154 |
* @param Image $image The source image. |
| 155 |
* @param float $width The border width. |
| 156 |
* @param string $color The border color. |
| 157 |
* |
| 158 |
* @return Image The manipulated image. |
| 159 |
*/ |
| 160 |
public function runShrink(Image $image, $width, $color) |
| 161 |
{ |
| 162 |
return $image |
| 163 |
->resize( |
| 164 |
(int) round($image->width() - ($width * 2)), |
| 165 |
(int) round($image->height() - ($width * 2)) |
| 166 |
) |
| 167 |
->resizeCanvas( |
| 168 |
(int) round($width * 2), |
| 169 |
(int) round($width * 2), |
| 170 |
'center', |
| 171 |
true, |
| 172 |
$color |
| 173 |
); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Run the expand border method. |
| 178 |
* |
| 179 |
* @param Image $image The source image. |
| 180 |
* @param float $width The border width. |
| 181 |
* @param string $color The border color. |
| 182 |
* |
| 183 |
* @return Image The manipulated image. |
| 184 |
*/ |
| 185 |
public function runExpand(Image $image, $width, $color) |
| 186 |
{ |
| 187 |
return $image->resizeCanvas( |
| 188 |
(int) round($width * 2), |
| 189 |
(int) round($width * 2), |
| 190 |
'center', |
| 191 |
true, |
| 192 |
$color |
| 193 |
); |
| 194 |
} |
| 195 |
} |
| 196 |
|