Monday June 2, 2025
Home
|
Contact
|
Support
|
PHP... a powerful, flexible, fully supported, battle tested server side language ..
<<
Random Page
Programming
C/C++
C# (with .net)
Win32/Win64
RegEx
Markdown (.md)
Python (.py)
Batch (.bat)
Programming XBOX & 360
XDK Tuts
NonXDK Tuts
OpenXDK Tuts
XNA with C#
Tutorials
Win32/Win64
DirectX3D (DirectX)
Maths of 3D
ASM..10101
Java
Physics
Visual Studio
Vulkan
Graphics
LaTeX
Data Mining & Machine Learning
AI
Fractals
Dual-Quaternions
Shaders
Vertex & Pixel Asm
Cg
Effect Files (.fx)
GLSL (.glsl)
WGSL (.wgsl)
Executable File Formats
PE (.exe) Inside Out
XBOX (.xbe) Format
Android (.apk)
3D File Formats
Quake2 (.md2) Format
Quake3 (.md3) Format
Quake3 BSP (.bsp)
3DS Format (.3ds)
X DirectX (.x)
GLTF (.glTF/.glb)
...
Image Formats
Bitmap (.bmp)
JPEG (.jpg)
DirectX DDS
Flash SWF
PNG (.png)
GIF (.gif)
WebP (.webp)
...
Internet Programming
WinSock
PHP
JavaScript
WebGL
WebXR
Retro Games
WebGPU
WebNN
Projects
Misc Demos
Notebook
Pixels
TexLive (LaTeX)
Crosswords
eNotes
Media
Quotes
Evolve
HSK
Slides
Trending
WordSearch
Links
Recommended Books
Donations (PayPal)
Contact
Books
Amazon
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-2025 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.