0

My script glues several images into one image but result image have single color dominated on first image from glue images set:

enter image description here

But input 4 images are in different colors (yellow, green, blue, red). Only first image from set is looks correct.

$images = array();

foreach ($fileNames as $fileName) {
    $image = imagecreatefrompng($path . $fileName);
    if ($image) {
        $images[] = $image;
    }
}

// ...

$img = imagecreate($w, $h);

$x = 0;
foreach ($images as $image) {
    $width = imagesx($image);
    $height = imagesy($image);

    imagecopy($img, $image, $x, 0, 0, 0, $width, $height);
    $x += $width;
}

Another one example (if first image from glue set is blue and other in differ colors):

enter image description here

Charles
  • 50,943
  • 13
  • 104
  • 142
Dmytro Zarezenko
  • 10,526
  • 11
  • 62
  • 104

1 Answers1

2

You are probably mixing paletted images, whereby the palette will be taken from the first destination image.

Convert them to TrueColor by creating a True Color image of suitable size, then imageCopy'ing all images into that one.

Afterwards, you can try reducing the destination image to palette again, even if this may yield slightly "off" colours.

$img = imageCreateTrueColor($w, $h);
// Add transparency management if needed

$x = 0;
foreach ($images as $image) {
    $width  = imagesx($image);
    $height = imagesy($image);    
    imagecopy($img, $image, $x, 0, 0, 0, $width, $height);
    $x += $width;
}
// Reduce image to non-dithered, 256-color paletted if needed
// imageTrueColorToPalette($img, False, 256);
LSerni
  • 55,617
  • 10
  • 65
  • 107