www.xbdev.net
xbdev - software development
Tuesday April 14, 2026
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($mosaic, true);
    $transparent = imagecolorallocatealpha($mosaic, 0, 0, 0, 127);
    imagefill($mosaic, 0, 0, $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, $sourceImage, 0, 0, $tileX, $tileY, $tileWidth, $tileHeight);
            
            // Apply slight brightness variation
            $brightnessAdjust = rand(-50, 50); // Random variation between -120 and +120
            imagefilter($tile, IMG_FILTER_BRIGHTNESS, $brightnessAdjust);
            
            // Randomly apply a color tint
            if (rand(0, 1) === 0) { // ~20% chance of tinting
                $tintColor = rand(1, 3);
                switch ($tintColor) {
                    case 1:
                        imagefilter($tile, IMG_FILTER_COLORIZE, 50, 0, 0); // Red tint
                        break;
                    case 2:
                        imagefilter($tile, IMG_FILTER_COLORIZE, 0, 50, 0); // Green tint
                        break;
                    case 3:
                        imagefilter($tile, IMG_FILTER_COLORIZE, 0, 0, 50); // Blue tint
                        break;
                }
            }
            
            // Add border to each tile
            $borderColor = imagecolorallocate($tile, 200, 200, 200);
            imagerectangle($tile, 0, 0, $tileWidth-1, $tileHeight-1, $borderColor);
            
            // Copy tile to mosaic
            imagecopy($mosaic, $tile, $tileX, $tileY, 0, 0, $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-2026 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.