| 1 |
<?php |
| 2 |
|
| 3 |
namespace Intervention\Image\Gd; |
| 4 |
|
| 5 |
use Intervention\Image\Exception\NotSupportedException; |
| 6 |
|
| 7 |
class Encoder extends \Intervention\Image\AbstractEncoder |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Processes and returns encoded image as JPEG string |
| 11 |
* |
| 12 |
* @return string |
| 13 |
*/ |
| 14 |
protected function processJpeg() |
| 15 |
{ |
| 16 |
ob_start(); |
| 17 |
imagejpeg($this->image->getCore(), null, $this->quality); |
| 18 |
$this->image->mime = image_type_to_mime_type(IMAGETYPE_JPEG); |
| 19 |
$buffer = ob_get_contents(); |
| 20 |
ob_end_clean(); |
| 21 |
|
| 22 |
return $buffer; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Processes and returns encoded image as PNG string |
| 27 |
* |
| 28 |
* @return string |
| 29 |
*/ |
| 30 |
protected function processPng() |
| 31 |
{ |
| 32 |
ob_start(); |
| 33 |
$resource = $this->image->getCore(); |
| 34 |
imagealphablending($resource, false); |
| 35 |
imagesavealpha($resource, true); |
| 36 |
imagepng($resource, null, -1); |
| 37 |
$this->image->mime = image_type_to_mime_type(IMAGETYPE_PNG); |
| 38 |
$buffer = ob_get_contents(); |
| 39 |
ob_end_clean(); |
| 40 |
|
| 41 |
return $buffer; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Processes and returns encoded image as GIF string |
| 46 |
* |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
protected function processGif() |
| 50 |
{ |
| 51 |
ob_start(); |
| 52 |
imagegif($this->image->getCore()); |
| 53 |
$this->image->mime = image_type_to_mime_type(IMAGETYPE_GIF); |
| 54 |
$buffer = ob_get_contents(); |
| 55 |
ob_end_clean(); |
| 56 |
|
| 57 |
return $buffer; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Processes and returns encoded image as WEBP string |
| 62 |
* |
| 63 |
* @return string |
| 64 |
*/ |
| 65 |
protected function processWebp() |
| 66 |
{ |
| 67 |
if ( ! function_exists('imagewebp')) { |
| 68 |
throw new NotSupportedException( |
| 69 |
"Webp format is not supported by PHP installation." |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
ob_start(); |
| 74 |
imagepalettetotruecolor($this->image->getCore()); |
| 75 |
imagealphablending($this->image->getCore(), true); |
| 76 |
imagesavealpha($this->image->getCore(), true); |
| 77 |
imagewebp($this->image->getCore(), null, $this->quality); |
| 78 |
$this->image->mime = defined('IMAGETYPE_WEBP') ? image_type_to_mime_type(IMAGETYPE_WEBP) : 'image/webp'; |
| 79 |
$buffer = ob_get_contents(); |
| 80 |
ob_end_clean(); |
| 81 |
|
| 82 |
return $buffer; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Processes and returns encoded image as TIFF string |
| 87 |
* |
| 88 |
* @return string |
| 89 |
*/ |
| 90 |
protected function processTiff() |
| 91 |
{ |
| 92 |
throw new NotSupportedException( |
| 93 |
"TIFF format is not supported by Gd Driver." |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Processes and returns encoded image as BMP string |
| 99 |
* |
| 100 |
* @return string |
| 101 |
*/ |
| 102 |
protected function processBmp() |
| 103 |
{ |
| 104 |
if ( ! function_exists('imagebmp')) { |
| 105 |
throw new NotSupportedException( |
| 106 |
"BMP format is not supported by PHP installation." |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
ob_start(); |
| 111 |
imagebmp($this->image->getCore()); |
| 112 |
$this->image->mime = defined('IMAGETYPE_BMP') ? image_type_to_mime_type(IMAGETYPE_BMP) : 'image/bmp'; |
| 113 |
$buffer = ob_get_contents(); |
| 114 |
ob_end_clean(); |
| 115 |
|
| 116 |
return $buffer; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Processes and returns encoded image as ICO string |
| 121 |
* |
| 122 |
* @return string |
| 123 |
*/ |
| 124 |
protected function processIco() |
| 125 |
{ |
| 126 |
throw new NotSupportedException( |
| 127 |
"ICO format is not supported by Gd Driver." |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Processes and returns encoded image as PSD string |
| 133 |
* |
| 134 |
* @return string |
| 135 |
*/ |
| 136 |
protected function processPsd() |
| 137 |
{ |
| 138 |
throw new NotSupportedException( |
| 139 |
"PSD format is not supported by Gd Driver." |
| 140 |
); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Processes and returns encoded image as AVIF string |
| 145 |
* |
| 146 |
* @return string |
| 147 |
*/ |
| 148 |
protected function processAvif() |
| 149 |
{ |
| 150 |
if ( ! function_exists('imageavif')) { |
| 151 |
throw new NotSupportedException( |
| 152 |
"AVIF format is not supported by PHP installation." |
| 153 |
); |
| 154 |
} |
| 155 |
|
| 156 |
ob_start(); |
| 157 |
$resource = $this->image->getCore(); |
| 158 |
imagepalettetotruecolor($resource); |
| 159 |
imagealphablending($resource, true); |
| 160 |
imagesavealpha($resource, true); |
| 161 |
imageavif($resource, null, $this->quality); |
| 162 |
$this->image->mime = defined('IMAGETYPE_AVIF') ? image_type_to_mime_type(IMAGETYPE_AVIF) : 'image/avif'; |
| 163 |
$buffer = ob_get_contents(); |
| 164 |
ob_end_clean(); |
| 165 |
|
| 166 |
return $buffer; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Processes and returns encoded image as HEIC string |
| 171 |
* |
| 172 |
* @return string |
| 173 |
*/ |
| 174 |
protected function processHeic() |
| 175 |
{ |
| 176 |
throw new NotSupportedException( |
| 177 |
"HEIC format is not supported by Gd Driver." |
| 178 |
); |
| 179 |
} |
| 180 |
} |
| 181 |
|