File indexing completed on 2025-01-26 05:29:15
0001 <?php 0002 0003 namespace Intervention\Image; 0004 0005 abstract class AbstractEncoder 0006 { 0007 /** 0008 * Buffer of encode result data 0009 * 0010 * @var string 0011 */ 0012 public $result; 0013 0014 /** 0015 * Image object to encode 0016 * 0017 * @var Image 0018 */ 0019 public $image; 0020 0021 /** 0022 * Output format of encoder instance 0023 * 0024 * @var string 0025 */ 0026 public $format; 0027 0028 /** 0029 * Output quality of encoder instance 0030 * 0031 * @var int 0032 */ 0033 public $quality; 0034 0035 /** 0036 * Processes and returns encoded image as JPEG string 0037 * 0038 * @return string 0039 */ 0040 abstract protected function processJpeg(); 0041 0042 /** 0043 * Processes and returns encoded image as PNG string 0044 * 0045 * @return string 0046 */ 0047 abstract protected function processPng(); 0048 0049 /** 0050 * Processes and returns encoded image as GIF string 0051 * 0052 * @return string 0053 */ 0054 abstract protected function processGif(); 0055 0056 /** 0057 * Processes and returns encoded image as TIFF string 0058 * 0059 * @return string 0060 */ 0061 abstract protected function processTiff(); 0062 0063 /** 0064 * Processes and returns encoded image as BMP string 0065 * 0066 * @return string 0067 */ 0068 abstract protected function processBmp(); 0069 0070 /** 0071 * Processes and returns encoded image as ICO string 0072 * 0073 * @return string 0074 */ 0075 abstract protected function processIco(); 0076 0077 /** 0078 * Processes and returns image as WebP encoded string 0079 * 0080 * @return string 0081 */ 0082 abstract protected function processWebp(); 0083 0084 /** 0085 * Process a given image 0086 * 0087 * @param Image $image 0088 * @param string $format 0089 * @param int $quality 0090 * @return Image 0091 */ 0092 public function process(Image $image, $format = null, $quality = null) 0093 { 0094 $this->setImage($image); 0095 $this->setFormat($format); 0096 $this->setQuality($quality); 0097 0098 switch (strtolower($this->format)) { 0099 0100 case 'data-url': 0101 $this->result = $this->processDataUrl(); 0102 break; 0103 0104 case 'gif': 0105 case 'image/gif': 0106 $this->result = $this->processGif(); 0107 break; 0108 0109 case 'png': 0110 case 'image/png': 0111 case 'image/x-png': 0112 $this->result = $this->processPng(); 0113 break; 0114 0115 case 'jpg': 0116 case 'jpeg': 0117 case 'image/jpg': 0118 case 'image/jpeg': 0119 case 'image/pjpeg': 0120 $this->result = $this->processJpeg(); 0121 break; 0122 0123 case 'tif': 0124 case 'tiff': 0125 case 'image/tiff': 0126 case 'image/tif': 0127 case 'image/x-tif': 0128 case 'image/x-tiff': 0129 $this->result = $this->processTiff(); 0130 break; 0131 0132 case 'bmp': 0133 case 'bmp': 0134 case 'ms-bmp': 0135 case 'x-bitmap': 0136 case 'x-bmp': 0137 case 'x-ms-bmp': 0138 case 'x-win-bitmap': 0139 case 'x-windows-bmp': 0140 case 'x-xbitmap': 0141 case 'image/ms-bmp': 0142 case 'image/x-bitmap': 0143 case 'image/x-bmp': 0144 case 'image/x-ms-bmp': 0145 case 'image/x-win-bitmap': 0146 case 'image/x-windows-bmp': 0147 case 'image/x-xbitmap': 0148 $this->result = $this->processBmp(); 0149 break; 0150 0151 case 'ico': 0152 case 'image/x-ico': 0153 case 'image/x-icon': 0154 case 'image/vnd.microsoft.icon': 0155 $this->result = $this->processIco(); 0156 break; 0157 0158 case 'psd': 0159 case 'image/vnd.adobe.photoshop': 0160 $this->result = $this->processPsd(); 0161 break; 0162 0163 case 'webp': 0164 case 'image/webp': 0165 case 'image/x-webp': 0166 $this->result = $this->processWebp(); 0167 break; 0168 0169 default: 0170 throw new \Intervention\Image\Exception\NotSupportedException( 0171 "Encoding format ({$format}) is not supported." 0172 ); 0173 } 0174 0175 $this->setImage(null); 0176 0177 return $image->setEncoded($this->result); 0178 } 0179 0180 /** 0181 * Processes and returns encoded image as data-url string 0182 * 0183 * @return string 0184 */ 0185 protected function processDataUrl() 0186 { 0187 $mime = $this->image->mime ? $this->image->mime : 'image/png'; 0188 0189 return sprintf('data:%s;base64,%s', 0190 $mime, 0191 base64_encode($this->process($this->image, $mime, $this->quality)) 0192 ); 0193 } 0194 0195 /** 0196 * Sets image to process 0197 * 0198 * @param Image $image 0199 */ 0200 protected function setImage($image) 0201 { 0202 $this->image = $image; 0203 } 0204 0205 /** 0206 * Determines output format 0207 * 0208 * @param string $format 0209 */ 0210 protected function setFormat($format = null) 0211 { 0212 if ($format == '' && $this->image instanceof Image) { 0213 $format = $this->image->mime; 0214 } 0215 0216 $this->format = $format ? $format : 'jpg'; 0217 0218 return $this; 0219 } 0220 0221 /** 0222 * Determines output quality 0223 * 0224 * @param int $quality 0225 */ 0226 protected function setQuality($quality) 0227 { 0228 $quality = is_null($quality) ? 90 : $quality; 0229 $quality = $quality === 0 ? 1 : $quality; 0230 0231 if ($quality < 0 || $quality > 100) { 0232 throw new \Intervention\Image\Exception\InvalidArgumentException( 0233 'Quality must range from 0 to 100.' 0234 ); 0235 } 0236 0237 $this->quality = intval($quality); 0238 0239 return $this; 0240 } 0241 }