<?php
$tiles = array();
$tiles[] = array("mapId"=>1 );
$tiles[] = array("mapId"=>2 );
//$tiles[] = array("mapId"=>3 );
recreateTileFile(1, $tiles);
function recreateTileFile($excludeMapId, $mergedTiles){
$destIm = false;
$im = false;
$count = 1;
foreach($mergedTiles as $tile){
if( $tile["mapId"] != $excludeMapId){
echo "Handle ".$tile["mapId"]."\n";
if( $destIm ){
echo "Merge\n";
$n = $tile["mapId"]."_img.png";
echo "Create img from $n, save debug $count\n";
$im = imagecreatefrompng($n);
imagepng($im, $count."_debug.png");
imagecopymerge_alpha($destIm, $im, 0,0, 0,0, 266,266,100);
imagepng($im, (++$count)."_debug.png");
echo "After merge, save im to debug $count\n";
imagepng($destIm, (++$count)."_debug.png");
echo "After merge, save destIm to debug $count\n";
echo "Done merge\n";
}
else{
echo "No dest, create first from ".$tile["mapId"]."\n";
$destIm = imagecreatefrompng($tile["mapId"]."_img.png");
echo "Save debug $count\n";
imagepng($destIm, $count."_debug.png");
}
}
$count++;
if($im){
imagedestroy($im);
}
}
echo "Destroy and save recreated\n";
imagepng($destIm, "recreated_img.png");
imagedestroy($destIm);
}
function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
if(!isset($pct)){
return false;
}
$pct /= 100;
// Get image width and height
$w = imagesx( $src_im );
$h = imagesy( $src_im );
// Turn alpha blending off
imagealphablending( $src_im, false );
// Find the most opaque pixel in the image (the one with the smallest alpha value)
$minalpha = 127;
for( $x = 0; $x < $w; $x++ )
for( $y = 0; $y < $h; $y++ ){
$alpha = ( imagecolorat( $src_im, $x, $y ) >> 24 ) & 0xFF;
if( $alpha < $minalpha ){
$minalpha = $alpha;
}
}
//loop through image pixels and modify alpha for each
for( $x = 0; $x < $w; $x++ ){
for( $y = 0; $y < $h; $y++ ){
//get current alpha value (represents the TANSPARENCY!)
$colorxy = imagecolorat( $src_im, $x, $y );
$alpha = ( $colorxy >> 24 ) & 0xFF;
//calculate new alpha
if( $minalpha !== 127 ){
$alpha = 127 + 127 * $pct * ( $alpha - 127 ) / ( 127 - $minalpha );
} else {
$alpha += 127 * $pct;
}
//get the color index with new alpha
$alphacolorxy = imagecolorallocatealpha( $src_im, ( $colorxy >> 16 ) & 0xFF, ( $colorxy >> 8 ) & 0xFF, $colorxy & 0xFF, $alpha );
//set pixel with the new color + opacity
if( !imagesetpixel( $src_im, $x, $y, $alphacolorxy ) ){
return false;
}
}
}
// The image copy
imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
}
?>