| 1 |
<?php |
| 2 |
|
| 3 |
namespace Intervention\Image; |
| 4 |
|
| 5 |
use Intervention\Image\Exception\InvalidArgumentException; |
| 6 |
use Intervention\Image\Exception\NotSupportedException; |
| 7 |
|
| 8 |
abstract class AbstractEncoder |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Buffer of encode result data |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
public $result; |
| 16 |
|
| 17 |
/** |
| 18 |
* Image object to encode |
| 19 |
* |
| 20 |
* @var Image |
| 21 |
*/ |
| 22 |
public $image; |
| 23 |
|
| 24 |
/** |
| 25 |
* Output format of encoder instance |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
public $format; |
| 30 |
|
| 31 |
/** |
| 32 |
* Output quality of encoder instance |
| 33 |
* |
| 34 |
* @var int |
| 35 |
*/ |
| 36 |
public $quality; |
| 37 |
|
| 38 |
/** |
| 39 |
* Processes and returns encoded image as JPEG string |
| 40 |
* |
| 41 |
* @return string |
| 42 |
*/ |
| 43 |
abstract protected function processJpeg(); |
| 44 |
|
| 45 |
/** |
| 46 |
* Processes and returns encoded image as PNG string |
| 47 |
* |
| 48 |
* @return string |
| 49 |
*/ |
| 50 |
abstract protected function processPng(); |
| 51 |
|
| 52 |
/** |
| 53 |
* Processes and returns encoded image as GIF string |
| 54 |
* |
| 55 |
* @return string |
| 56 |
*/ |
| 57 |
abstract protected function processGif(); |
| 58 |
|
| 59 |
/** |
| 60 |
* Processes and returns encoded image as TIFF string |
| 61 |
* |
| 62 |
* @return string |
| 63 |
*/ |
| 64 |
abstract protected function processTiff(); |
| 65 |
|
| 66 |
/** |
| 67 |
* Processes and returns encoded image as BMP string |
| 68 |
* |
| 69 |
* @return string |
| 70 |
*/ |
| 71 |
abstract protected function processBmp(); |
| 72 |
|
| 73 |
/** |
| 74 |
* Processes and returns encoded image as ICO string |
| 75 |
* |
| 76 |
* @return string |
| 77 |
*/ |
| 78 |
abstract protected function processIco(); |
| 79 |
|
| 80 |
/** |
| 81 |
* Processes and returns image as WebP encoded string |
| 82 |
* |
| 83 |
* @return string |
| 84 |
*/ |
| 85 |
abstract protected function processWebp(); |
| 86 |
|
| 87 |
/** |
| 88 |
* Processes and returns image as Avif encoded string |
| 89 |
* |
| 90 |
* @return string |
| 91 |
*/ |
| 92 |
abstract protected function processAvif(); |
| 93 |
|
| 94 |
/** |
| 95 |
* Processes and returns image as Heic encoded string |
| 96 |
* |
| 97 |
* @return string |
| 98 |
*/ |
| 99 |
abstract protected function processHeic(); |
| 100 |
|
| 101 |
/** |
| 102 |
* Process a given image |
| 103 |
* |
| 104 |
* @param Image $image |
| 105 |
* @param string $format |
| 106 |
* @param int $quality |
| 107 |
* @return Image |
| 108 |
*/ |
| 109 |
public function process(Image $image, $format = null, $quality = null) |
| 110 |
{ |
| 111 |
$this->setImage($image); |
| 112 |
$this->setFormat($format); |
| 113 |
$this->setQuality($quality); |
| 114 |
|
| 115 |
switch (strtolower($this->format)) { |
| 116 |
|
| 117 |
case 'data-url': |
| 118 |
$this->result = $this->processDataUrl(); |
| 119 |
break; |
| 120 |
|
| 121 |
case 'gif': |
| 122 |
case 'image/gif': |
| 123 |
$this->result = $this->processGif(); |
| 124 |
break; |
| 125 |
|
| 126 |
case 'png': |
| 127 |
case 'image/png': |
| 128 |
case 'image/x-png': |
| 129 |
$this->result = $this->processPng(); |
| 130 |
break; |
| 131 |
|
| 132 |
case 'jpg': |
| 133 |
case 'jpeg': |
| 134 |
case 'jfif': |
| 135 |
case 'image/jp2': |
| 136 |
case 'image/jpg': |
| 137 |
case 'image/jpeg': |
| 138 |
case 'image/pjpeg': |
| 139 |
case 'image/jfif': |
| 140 |
$this->result = $this->processJpeg(); |
| 141 |
break; |
| 142 |
|
| 143 |
case 'tif': |
| 144 |
case 'tiff': |
| 145 |
case 'image/tiff': |
| 146 |
case 'image/tif': |
| 147 |
case 'image/x-tif': |
| 148 |
case 'image/x-tiff': |
| 149 |
$this->result = $this->processTiff(); |
| 150 |
break; |
| 151 |
|
| 152 |
case 'bmp': |
| 153 |
case 'ms-bmp': |
| 154 |
case 'x-bitmap': |
| 155 |
case 'x-bmp': |
| 156 |
case 'x-ms-bmp': |
| 157 |
case 'x-win-bitmap': |
| 158 |
case 'x-windows-bmp': |
| 159 |
case 'x-xbitmap': |
| 160 |
case 'image/ms-bmp': |
| 161 |
case 'image/x-bitmap': |
| 162 |
case 'image/x-bmp': |
| 163 |
case 'image/x-ms-bmp': |
| 164 |
case 'image/x-win-bitmap': |
| 165 |
case 'image/x-windows-bmp': |
| 166 |
case 'image/x-xbitmap': |
| 167 |
$this->result = $this->processBmp(); |
| 168 |
break; |
| 169 |
|
| 170 |
case 'ico': |
| 171 |
case 'image/x-ico': |
| 172 |
case 'image/x-icon': |
| 173 |
case 'image/vnd.microsoft.icon': |
| 174 |
$this->result = $this->processIco(); |
| 175 |
break; |
| 176 |
|
| 177 |
case 'psd': |
| 178 |
case 'image/vnd.adobe.photoshop': |
| 179 |
$this->result = $this->processPsd(); |
| 180 |
break; |
| 181 |
|
| 182 |
case 'webp': |
| 183 |
case 'image/webp': |
| 184 |
case 'image/x-webp': |
| 185 |
$this->result = $this->processWebp(); |
| 186 |
break; |
| 187 |
|
| 188 |
case 'avif': |
| 189 |
case 'image/avif': |
| 190 |
$this->result = $this->processAvif(); |
| 191 |
break; |
| 192 |
|
| 193 |
case 'heic': |
| 194 |
case 'image/heic': |
| 195 |
case 'image/heif': |
| 196 |
$this->result = $this->processHeic(); |
| 197 |
break; |
| 198 |
|
| 199 |
default: |
| 200 |
throw new NotSupportedException( |
| 201 |
"Encoding format ({$this->format}) is not supported." |
| 202 |
); |
| 203 |
} |
| 204 |
|
| 205 |
$this->setImage(null); |
| 206 |
|
| 207 |
return $image->setEncoded($this->result); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Processes and returns encoded image as data-url string |
| 212 |
* |
| 213 |
* @return string |
| 214 |
*/ |
| 215 |
protected function processDataUrl() |
| 216 |
{ |
| 217 |
$mime = $this->image->mime ? $this->image->mime : 'image/png'; |
| 218 |
|
| 219 |
return sprintf('data:%s;base64,%s', |
| 220 |
$mime, |
| 221 |
base64_encode($this->process($this->image, $mime, $this->quality)) |
| 222 |
); |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Sets image to process |
| 227 |
* |
| 228 |
* @param Image $image |
| 229 |
*/ |
| 230 |
protected function setImage($image) |
| 231 |
{ |
| 232 |
$this->image = $image; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Determines output format |
| 237 |
* |
| 238 |
* @param string $format |
| 239 |
*/ |
| 240 |
protected function setFormat($format = null) |
| 241 |
{ |
| 242 |
if ($format == '' && $this->image instanceof Image) { |
| 243 |
$format = $this->image->mime; |
| 244 |
} |
| 245 |
|
| 246 |
$this->format = $format ? $format : 'jpg'; |
| 247 |
|
| 248 |
return $this; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Determines output quality |
| 253 |
* |
| 254 |
* @param int $quality |
| 255 |
*/ |
| 256 |
protected function setQuality($quality) |
| 257 |
{ |
| 258 |
$quality = is_null($quality) ? 90 : $quality; |
| 259 |
$quality = $quality === 0 ? 1 : $quality; |
| 260 |
|
| 261 |
if ($quality < 0 || $quality > 100) { |
| 262 |
throw new InvalidArgumentException( |
| 263 |
'Quality must range from 0 to 100.' |
| 264 |
); |
| 265 |
} |
| 266 |
|
| 267 |
$this->quality = intval($quality); |
| 268 |
|
| 269 |
return $this; |
| 270 |
} |
| 271 |
} |
| 272 |
|