width(), $size->height()); // copy resolution to clone $resolution = imageresolution($gd); if (is_array($resolution) && array_key_exists(0, $resolution) && array_key_exists(1, $resolution)) { imageresolution($clone, $resolution[0], $resolution[1]); } // fill with background $processor = new ColorProcessor(); imagefill($clone, 0, 0, $processor->colorToNative($background)); imagealphablending($clone, true); imagesavealpha($clone, true); // set background image as transparent if alpha channel value if color is below .5 // comes into effect when the end format only supports binary transparency (like GIF) if ($background->channel(Alpha::class)->value() < 128) { imagecolortransparent($clone, $processor->colorToNative($background)); } return $clone; } /** * Create a clone of an GdImage that is positioned on the specified background color. * Possible transparent areas are mixed with this color. * * @param GdImage $gd * @param ColorInterface $background * @throws ColorException * @return GdImage */ public static function cloneBlended(GdImage $gd, ColorInterface $background): GdImage { // create empty canvas with same size $clone = static::cloneEmpty($gd, background: $background); // transfer actual image to clone imagecopy($clone, $gd, 0, 0, 0, 0, imagesx($gd), imagesy($gd)); return $clone; } }