13 class Gd2 extends \Magento\Framework\Image\Adapter\AbstractAdapter
27 private static $_callbacks = [
28 IMAGETYPE_GIF => [
'output' =>
'imagegif',
'create' =>
'imagecreatefromgif'],
29 IMAGETYPE_JPEG => [
'output' =>
'imagejpeg',
'create' =>
'imagecreatefromjpeg'],
30 IMAGETYPE_PNG => [
'output' =>
'imagepng',
'create' =>
'imagecreatefrompng'],
31 IMAGETYPE_XBM => [
'output' =>
'imagexbm',
'create' =>
'imagecreatefromxbm'],
32 IMAGETYPE_WBMP => [
'output' =>
'imagewbmp',
'create' =>
'imagecreatefromxbm'],
49 $this->_fileMimeType =
null;
50 $this->_fileType =
null;
60 public function open($filename)
62 $this->_fileName = $filename;
67 throw new \OverflowException(
'Memory limit has been reached.');
69 $this->imageDestroy();
71 $this->_getCallback(
'create',
null, sprintf(
'Unsupported image format. File: %s', $this->_fileName)),
75 if (in_array($fileType, [IMAGETYPE_PNG, IMAGETYPE_GIF])) {
76 $this->_keepTransparency =
true;
77 if ($this->_imageHandler) {
78 $isAlpha = $this->
checkAlpha($this->_fileName);
80 $this->_fillBackgroundColor($this->_imageHandler);
111 if (!isset($imageInfo[0]) || !isset($imageInfo[1])) {
114 if (!isset($imageInfo[
'channels'])) {
116 $imageInfo[
'channels'] = 4;
118 if (!isset($imageInfo[
'bits'])) {
120 $imageInfo[
'bits'] = 8;
124 ($imageInfo[0] * $imageInfo[1] * $imageInfo[
'bits'] * $imageInfo[
'channels'] / 8 + pow(2, 16)) * 1.65
138 if (stripos($memoryValue,
'G') !==
false) {
139 return (
int)$memoryValue * pow(1024, 3);
140 }
elseif (stripos($memoryValue,
'M') !==
false) {
141 return (
int)$memoryValue * 1024 * 1024;
142 }
elseif (stripos($memoryValue,
'K') !==
false) {
143 return (
int)$memoryValue * 1024;
146 return (
int)$memoryValue;
159 public function save($destination =
null, $newName =
null)
163 if (!$this->_resized) {
166 $isTrueColor =
false;
167 $this->_getTransparency($this->_imageHandler, $this->_fileType, $isAlpha, $isTrueColor);
170 $newImage = imagecreatetruecolor($this->_imageSrcWidth, $this->_imageSrcHeight);
172 $newImage = imagecreate($this->_imageSrcWidth, $this->_imageSrcHeight);
174 $this->_fillBackgroundColor($newImage);
175 imagecopy($newImage, $this->_imageHandler, 0, 0, 0, 0, $this->_imageSrcWidth, $this->_imageSrcHeight);
176 $this->imageDestroy();
177 $this->_imageHandler = $newImage;
182 imageinterlace($this->_imageHandler,
true);
185 switch ($this->_fileType) {
201 $functionParameters[] = $quality;
204 call_user_func_array($this->_getCallback(
'output'), $functionParameters);
217 call_user_func($this->_getCallback(
'output'), $this->_imageHandler);
218 return ob_get_clean();
230 private function _getCallback($callbackType, $fileType =
null, $unsupportedText =
'Unsupported image format.')
232 if (
null === $fileType) {
235 if (empty(self::$_callbacks[$fileType])) {
236 throw new \Exception($unsupportedText);
238 if (empty(self::$_callbacks[$fileType][$callbackType])) {
239 throw new \Exception(
'Callback not found.');
241 return self::$_callbacks[$fileType][$callbackType];
254 private function _fillBackgroundColor(&$imageResourceTo)
257 if ($this->_keepTransparency) {
259 $transparentIndex = $this->_getTransparency($this->_imageHandler, $this->_fileType, $isAlpha);
263 if (!imagealphablending($imageResourceTo,
false)) {
264 throw new \Exception(
'Failed to set alpha blending for PNG image.');
266 $transparentAlphaColor = imagecolorallocatealpha($imageResourceTo, 0, 0, 0, 127);
267 if (
false === $transparentAlphaColor) {
268 throw new \Exception(
'Failed to allocate alpha transparency for PNG image.');
270 if (!imagefill($imageResourceTo, 0, 0, $transparentAlphaColor)) {
271 throw new \Exception(
'Failed to fill PNG image with alpha transparency.');
273 if (!imagesavealpha($imageResourceTo,
true)) {
274 throw new \Exception(
'Failed to save alpha transparency into PNG image.');
277 return $transparentAlphaColor;
278 }
elseif (
false !== $transparentIndex) {
280 $transparentColor =
false;
281 if ($transparentIndex >= 0 && $transparentIndex <= imagecolorstotal($this->_imageHandler)) {
282 list($r, $g, $b) = array_values(imagecolorsforindex($this->_imageHandler, $transparentIndex));
283 $transparentColor = imagecolorallocate($imageResourceTo, $r, $g, $b);
285 if (
false === $transparentColor) {
286 throw new \Exception(
'Failed to allocate transparent color for image.');
288 if (!imagefill($imageResourceTo, 0, 0, $transparentColor)) {
289 throw new \Exception(
'Failed to fill image with transparency.');
291 imagecolortransparent($imageResourceTo, $transparentColor);
292 return $transparentColor;
294 }
catch (\Exception $e) {
299 $color = imagecolorallocate($imageResourceTo, $r, $g, $b);
300 if (!imagefill($imageResourceTo, 0, 0, $color)) {
301 throw new \Exception(
"Failed to fill image background with color {$r} {$g} {$b}.");
328 private function _getTransparency($imageResource, $fileType, &$isAlpha =
false, &$isTrueColor =
false)
331 $isTrueColor =
false;
333 if (IMAGETYPE_GIF === $fileType || IMAGETYPE_PNG === $fileType) {
335 $transparentIndex = imagecolortransparent($imageResource);
336 if ($transparentIndex >= 0) {
337 return $transparentIndex;
338 }
elseif (IMAGETYPE_PNG === $fileType) {
340 $isAlpha = $this->
checkAlpha($this->_fileName);
343 return $transparentIndex;
346 if (IMAGETYPE_JPEG === $fileType) {
359 public function resize($frameWidth =
null, $frameHeight =
null)
365 $isTrueColor =
false;
366 $this->_getTransparency($this->_imageHandler, $this->_fileType, $isAlpha, $isTrueColor);
368 $newImage = imagecreatetruecolor($dims[
'frame'][
'width'], $dims[
'frame'][
'height']);
370 $newImage = imagecreate($dims[
'frame'][
'width'], $dims[
'frame'][
'height']);
374 $this->_saveAlpha($newImage);
378 $this->_fillBackgroundColor($newImage);
380 if ($this->_imageHandler) {
384 $this->_imageHandler,
389 $dims[
'dst'][
'width'],
390 $dims[
'dst'][
'height'],
391 $this->_imageSrcWidth,
392 $this->_imageSrcHeight
395 $this->imageDestroy();
396 $this->_imageHandler = $newImage;
398 $this->_resized =
true;
409 $rotatedImage = imagerotate($this->_imageHandler, $angle, $this->imageBackgroundColor);
410 $this->imageDestroy();
411 $this->_imageHandler = $rotatedImage;
429 public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity = 30, $tile =
false)
431 list($watermarkSrcWidth, $watermarkSrcHeight, $watermarkFileType,) = $this->
_getImageOptions($imagePath);
434 $this->_getCallback(
'create', $watermarkFileType,
'Unsupported watermark image format.'),
445 imagealphablending($newWatermark,
false);
446 $col = imagecolorallocate($newWatermark, 255, 255, 255);
447 imagecolortransparent($newWatermark, $col);
449 imagesavealpha($newWatermark,
true);
462 $watermark = $newWatermark;
468 $newWatermark = imagecreatetruecolor($this->_imageSrcWidth, $this->_imageSrcHeight);
469 imagealphablending($newWatermark,
false);
470 $col = imagecolorallocate($newWatermark, 255, 255, 255);
471 imagecolortransparent($newWatermark, $col);
472 imagefilledrectangle($newWatermark, 0, 0, $this->_imageSrcWidth, $this->_imageSrcHeight, $col);
473 imagesavealpha($newWatermark,
true);
481 $this->_imageSrcWidth,
482 $this->_imageSrcHeight,
486 $watermark = $newWatermark;
488 $positionX = $this->_imageSrcWidth / 2 - imagesx($watermark) / 2;
489 $positionY = $this->_imageSrcHeight / 2 - imagesy($watermark) / 2;
490 $this->imagecopymergeWithAlphaFix(
491 $this->_imageHandler,
502 $positionX = $this->_imageSrcWidth - imagesx($watermark);
503 $this->imagecopymergeWithAlphaFix(
504 $this->_imageHandler,
515 $this->imagecopymergeWithAlphaFix(
516 $this->_imageHandler,
527 $positionX = $this->_imageSrcWidth - imagesx($watermark);
528 $positionY = $this->_imageSrcHeight - imagesy($watermark);
529 $this->imagecopymergeWithAlphaFix(
530 $this->_imageHandler,
541 $positionY = $this->_imageSrcHeight - imagesy($watermark);
542 $this->imagecopymergeWithAlphaFix(
543 $this->_imageHandler,
555 if ($tile ===
false && $merged ===
false) {
556 $this->imagecopymergeWithAlphaFix(
557 $this->_imageHandler,
568 $offsetX = $positionX;
569 $offsetY = $positionY;
570 while ($offsetY <= $this->_imageSrcHeight + imagesy($watermark)) {
571 while ($offsetX <= $this->_imageSrcWidth + imagesx($watermark)) {
572 $this->imagecopymergeWithAlphaFix(
573 $this->_imageHandler,
583 $offsetX += imagesx($watermark);
585 $offsetX = $positionX;
586 $offsetY += imagesy($watermark);
590 imagedestroy($watermark);
603 public function crop($top = 0, $left = 0, $right = 0, $bottom = 0)
605 if ($left == 0 && $top == 0 && $right == 0 && $bottom == 0) {
609 $newWidth = $this->_imageSrcWidth - $left - $right;
610 $newHeight = $this->_imageSrcHeight - $top - $bottom;
612 $canvas = imagecreatetruecolor($newWidth, $newHeight);
614 if ($this->_fileType == IMAGETYPE_PNG) {
615 $this->_saveAlpha($canvas);
620 $this->_imageHandler,
630 $this->imageDestroy();
631 $this->_imageHandler = $canvas;
644 foreach ($this->_requiredExtensions as
$value) {
645 if (!extension_loaded(
$value)) {
646 throw new \Exception(
"Required PHP extension '{$value}' was not loaded.");
658 $this->_imageSrcWidth = imagesx($this->_imageHandler);
659 $this->_imageSrcHeight = imagesy($this->_imageHandler);
667 $this->imageDestroy();
675 private function imageDestroy()
678 imagedestroy($this->_imageHandler);
688 private function _saveAlpha($imageHandler)
690 $background = imagecolorallocate($imageHandler, 0, 0, 0);
691 imagecolortransparent($imageHandler, $background);
692 imagealphablending($imageHandler,
false);
693 imagesavealpha($imageHandler,
true);
705 $colorIndex = imagecolorat($this->_imageHandler, $x, $y);
706 return imagecolorsforindex($this->_imageHandler, $colorIndex);
719 $this->_resized =
true;
722 }
catch (\Exception $e) {
726 if ($error || empty($this->_imageHandler)) {
741 $width = imagefontwidth($this->_fontSize) * strlen(
$text);
742 $height = imagefontheight($this->_fontSize);
746 $black = imagecolorallocate($this->_imageHandler, 0, 0, 0);
747 imagestring($this->_imageHandler, $this->_fontSize, 0, 0,
$text, $black);
762 $boundingBox = imagettfbbox($this->_fontSize, 0, $font,
$text);
763 $width = abs($boundingBox[4] - $boundingBox[0]);
764 $height = abs($boundingBox[5] - $boundingBox[1]);
768 $black = imagecolorallocate($this->_imageHandler, 0, 0, 0);
770 $this->_imageHandler,
774 $height - $boundingBox[1],
780 throw new \Exception(
'Unable to create TTF text');
793 $this->_fileType = IMAGETYPE_PNG;
794 $image = imagecreatetruecolor($width, $height);
795 $colorWhite = imagecolorallocatealpha(
$image, 255, 255, 255, 127);
797 imagealphablending(
$image,
true);
798 imagesavealpha(
$image,
true);
800 imagefill(
$image, 0, 0, $colorWhite);
801 $this->imageDestroy();
802 $this->_imageHandler =
$image;
820 private function imagecopymergeWithAlphaFix(
832 return imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
839 $sizeX = imagesx($src_im);
840 $sizeY = imagesy($src_im);
841 if (
false === $sizeX ||
false === $sizeY) {
845 $tmpImg = imagecreatetruecolor($src_w, $src_h);
846 if (
false === $tmpImg) {
850 if (
false === imagealphablending($tmpImg,
false)) {
854 if (
false === imagecopy($tmpImg, $src_im, 0, 0, 0, 0, $sizeX, $sizeY)) {
858 $transparancy = 127 - (($pct*127)/100);
859 if (
false === imagefilter($tmpImg, IMG_FILTER_COLORIZE, 0, 0, 0, $transparancy)) {
863 $result = imagecopy($dst_im, $tmpImg, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
864 imagedestroy($tmpImg);
elseif(isset( $params[ 'redirect_parent']))
_adaptResizeValues($frameWidth, $frameHeight)
call_user_func($callable, $param)
save($destination=null, $newName=null)
_getImageNeedMemorySize($file)
getWatermarkImageOpacity()
_createEmptyImage($width, $height)
_convertToByte($memoryValue)
_createImageFromTtfText($text, $font)
crop($top=0, $left=0, $right=0, $bottom=0)
watermark($imagePath, $positionX=0, $positionY=0, $opacity=30, $tile=false)
resize($frameWidth=null, $frameHeight=null)
_prepareDestination($destination=null, $newName=null)
_createImageFromText($text)
_getImageOptions($filePath)
createPngFromString($text, $font='')