Contao\GdImage::convertToPaletteImage PHP Метод

convertToPaletteImage() публичный Метод

Convert a true color image to a palette image with 256 colors and preserve transparency
public convertToPaletteImage ( ) : static
Результат static
    public function convertToPaletteImage()
    {
        if (!imageistruecolor($this->gdResource)) {
            return $this;
        }
        $width = imagesx($this->gdResource);
        $height = imagesy($this->gdResource);
        $transparentColor = null;
        if ($this->countColors(256) <= 256) {
            $paletteImage = imagecreate($width, $height);
            $colors = array();
            $isTransparent = false;
            for ($x = 0; $x < $width; $x++) {
                for ($y = 0; $y < $height; $y++) {
                    $color = imagecolorat($this->gdResource, $x, $y);
                    // Check if the pixel is fully transparent
                    if (($color >> 24 & 0x7f) === 127) {
                        $isTransparent = true;
                    } else {
                        $colors[$color & 0xffffff] = true;
                    }
                }
            }
            $colors = array_keys($colors);
            foreach ($colors as $index => $color) {
                imagecolorset($paletteImage, $index, $color >> 16 & 0xff, $color >> 8 & 0xff, $color & 0xff);
            }
            if ($isTransparent) {
                $transparentColor = imagecolorallocate($paletteImage, 0, 0, 0);
                imagecolortransparent($paletteImage, $transparentColor);
            }
            imagecopy($paletteImage, $this->gdResource, 0, 0, 0, 0, $width, $height);
        } else {
            $paletteImage = imagecreatetruecolor($width, $height);
            imagealphablending($paletteImage, false);
            imagesavealpha($paletteImage, true);
            imagecopy($paletteImage, $this->gdResource, 0, 0, 0, 0, $width, $height);
            // 256 minus 1 for the transparent color
            imagetruecolortopalette($paletteImage, false, 255);
            $transparentColor = imagecolorallocate($paletteImage, 0, 0, 0);
            imagecolortransparent($paletteImage, $transparentColor);
        }
        if ($transparentColor !== null) {
            // Fix fully transparent pixels
            for ($x = 0; $x < $width; $x++) {
                for ($y = 0; $y < $height; $y++) {
                    // Check if the pixel is fully transparent
                    if ((imagecolorat($this->gdResource, $x, $y) >> 24 & 0x7f) === 127) {
                        imagefilledrectangle($paletteImage, $x, $y, $x, $y, $transparentColor);
                    }
                }
            }
        }
        imagedestroy($this->gdResource);
        $this->gdResource = $paletteImage;
        return $this;
    }

Usage Example

Пример #1
0
 /**
  * Tests the convertToPaletteImage() method from a true color image.
  */
 public function testConvertToPaletteImageFromTrueColor()
 {
     $image = imagecreatetruecolor(100, 100);
     for ($x = 0; $x < 100; ++$x) {
         for ($y = 0; $y < 100; ++$y) {
             imagefilledrectangle($image, $x, $y, $x + 1, $y + 1, imagecolorallocatealpha($image, $x, $y, 0, 0));
         }
     }
     // Bottom right pixel transparent
     imagealphablending($image, false);
     imagefilledrectangle($image, 99, 99, 100, 100, imagecolorallocatealpha($image, 0, 0, 0, 127));
     $image = new GdImage($image);
     $image->convertToPaletteImage();
     $this->assertInternalType('resource', $image->getResource());
     $this->assertFalse(imageistruecolor($image->getResource()));
     $this->assertEquals(256, imagecolorstotal($image->getResource()));
     $this->assertEquals(127, imagecolorsforindex($image->getResource(), imagecolorat($image->getResource(), 99, 99))['alpha'], 'Bottom right pixel should be transparent');
 }