Kad
Kad

Reputation: 606

Image tile code applies color filter

I am trying to make tiled image with using different images. After first tile, average color of first tile applied to another tile as filter. How can i use each tile picture without filter? My code is below;

<?php 

$dest = imagecreatefrompng('myimage.png');

$src[] = imagecreatefrompng('otter.png');
$src[] = imagecreatefrompng('fox.png');
$src[] = imagecreatefrompng('opals.png');

// Copy and merge
for($i=0;$i<count($src);$i++){
imagecopymerge($dest, $src[$i], 32*$i, 0, 0, 0, 32, 32, 100);

}
// Output and free from memory
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);

for($ii=0;$ii<count($src);$ii++){
imagedestroy($src[$ii]);
}

?>

Sample tile link > http://7kitap.com/fox.png

Code link > http://7kitap.com/fillimage.php

As you will see on link above tile picture colors are changing after first tile.

Upvotes: 0

Views: 194

Answers (2)

emartel
emartel

Reputation: 7773

Your script could have some improvements, right now your tiles rendering will not work properly if you have more $src pictures than fills the width.

I adjusted your code to handle as many tiles as possible:

<?php 

$width = 320; // pick size
$height = 320; // pick size

$dest = imagecreatetruecolor($width, $height);

// I just added the same 3 pictures just to test the overflow
$src[] = imagecreatefrompng('otter.png');
$src[] = imagecreatefrompng('fox.png');
$src[] = imagecreatefrompng('opals.png');
$src[] = imagecreatefrompng('otter.png');
$src[] = imagecreatefrompng('fox.png');
$src[] = imagecreatefrompng('opals.png');
$src[] = imagecreatefrompng('otter.png');
$src[] = imagecreatefrompng('fox.png');
$src[] = imagecreatefrompng('opals.png');
$src[] = imagecreatefrompng('otter.png');
$src[] = imagecreatefrompng('fox.png');
$src[] = imagecreatefrompng('opals.png');
$src[] = imagecreatefrompng('otter.png');
$src[] = imagecreatefrompng('fox.png');
$src[] = imagecreatefrompng('opals.png');

$thumbnailWidth = 32; // Should be deduced from the thumbnails

$imagesPerRow = $width / $thumbnailWidth;
// Copy and merge
for($i=0;$i<count($src);$i++)
{
    // Using this code, your thumbnails will overflow to the next row
    $row = floor($i / $imagesPerRow);
    $column = $i % $imagesPerRow;
    imagecopymerge($dest, $src[$i], 32*$column, 32*$row, 0, 0, 32, 32, 100);
}
// Output and free from memory
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);

for($ii=0;$ii<count($src);$ii++){
    imagedestroy($src[$ii]);
}

?>

Which will give you the following result:

Tiles

Upvotes: 1

teynon
teynon

Reputation: 8298

Try using imagecreatetruecolor. Perhaps the colors from the first image are being used as the basis for the rest of them.

Upvotes: 1

Related Questions