www.xbdev.net
xbdev - software development
Monday June 2, 2025
Home | Contact | Support | PHP... a powerful, flexible, fully supported, battle tested server side language ..
     
 

PHP...

a powerful, flexible, fully supported, battle tested server side language ..

 

Create a Mosaic Image from a Source Image with Slight Color Variations



// Function to create a mosaic image from a source image with slight color variations
function createMosaicImage($sourceImagePath$tileSize 50) {
    if (!
file_exists($sourceImagePath)) return false;
    
    
$imageInfo getimagesize($sourceImagePath);
    
$mimeType $imageInfo['mime'];
    
    switch (
$mimeType) {
        case 
'image/jpeg':
            
$sourceImage imagecreatefromjpeg($sourceImagePath);
            break;
        case 
'image/png':
            
$sourceImage imagecreatefrompng($sourceImagePath);
            break;
        case 
'image/gif':
            
$sourceImage imagecreatefromgif($sourceImagePath);
            break;
        default:
            return 
false;
    }
    
    
$width imagesx($sourceImage);
    
$height imagesy($sourceImage);
    
    
// Calculate number of tiles
    
$cols ceil($width $tileSize);
    
$rows ceil($height $tileSize);
    
    
// Create a transparent mosaic base
    
$mosaic imagecreatetruecolor($width$height);
    
imagesavealpha($mosaictrue);
    
$transparent imagecolorallocatealpha($mosaic000127);
    
imagefill($mosaic00$transparent);
    
    
// Create tiles with slight color variation
    
for ($y 0$y $rows$y++) {
        for (
$x 0$x $cols$x++) {
            
$tileX $x $tileSize;
            
$tileY $y $tileSize;
            
$tileWidth min($tileSize$width $tileX);
            
$tileHeight min($tileSize$height $tileY);
            
            
// Create a small tile
            
$tile imagecreatetruecolor($tileWidth$tileHeight);
            
imagecopy($tile$sourceImage00$tileX$tileY$tileWidth$tileHeight);
            
            
// Apply slight brightness variation
            
$brightnessAdjust rand(-5050); // Random variation between -120 and +120
            
imagefilter($tileIMG_FILTER_BRIGHTNESS$brightnessAdjust);
            
            
// Randomly apply a color tint
            
if (rand(01) === 0) { // ~20% chance of tinting
                
$tintColor rand(13);
                switch (
$tintColor) {
                    case 
1:
                        
imagefilter($tileIMG_FILTER_COLORIZE5000); // Red tint
                        
break;
                    case 
2:
                        
imagefilter($tileIMG_FILTER_COLORIZE0500); // Green tint
                        
break;
                    case 
3:
                        
imagefilter($tileIMG_FILTER_COLORIZE0050); // Blue tint
                        
break;
                }
            }
            
            
// Add border to each tile
            
$borderColor imagecolorallocate($tile200200200);
            
imagerectangle($tile00$tileWidth-1$tileHeight-1$borderColor);
            
            
// Copy tile to mosaic
            
imagecopy($mosaic$tile$tileX$tileY00$tileWidth$tileHeight);
            
            
imagedestroy($tile);
        }
    }
    
    
// Save mosaic to cache
    
$mosaicPath CACHE_DIR 'mosaic_' basename($sourceImagePath);
    
imagepng($mosaic$mosaicPath);
    
imagedestroy($sourceImage);
    
imagedestroy($mosaic);
    
    return 
$mosaicPath;
}







 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2025 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.