Created
October 17, 2024 15:55
-
-
Save yewknee/a7be6c5fbd8f011f6e57532d81b3d325 to your computer and use it in GitHub Desktop.
PHP + GD, create a magic eye image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
// Merge Image to Pattern to create Autostereogram | |
// AKA Magic Eye Maker | |
// Ensure GD library is enabled in your PHP installation | |
// yewknee.com + ChatGPT authoring | |
*/ | |
// your directory with an image sequence - name files however you want, just make them sequential | |
$directory_name = "Depths/Depth-8-512"; | |
$directory = preg_grep("/^([^.])/", scandir($directory_name)); | |
// if your process times out, you can slice up your array into smaller chunks to process | |
// $directory = array_slice($directory, 462); | |
function makeMagicEye($directory_name, $source) | |
{ | |
// Load the depth map and pattern images | |
$depthMap = imagecreatefrompng($directory_name . "/" . $source); // Load depth map | |
// what pattern PNG do you want to use. Keep it smallish (500x500 max) so it can tile | |
$pattern_filename = "name-of-tile.png"; | |
$pattern = imagecreatefrompng($pattern_filename); // Load pattern | |
// Get dimensions of the depth map and pattern images | |
$depthWidth = imagesx($depthMap); | |
$depthHeight = imagesy($depthMap); | |
$patternWidth = imagesx($pattern); | |
$patternHeight = imagesy($pattern); | |
// Create an output image with the same size as the depth map | |
$outputImage = imagecreatetruecolor($depthWidth, $depthHeight); | |
// Loop through every pixel of the depth map | |
for ($y = 0; $y < $depthHeight; $y++) { | |
for ($x = 0; $x < $depthWidth; $x++) { | |
// Get the color value from the depth map | |
$depthColor = imagecolorat($depthMap, $x, $y); | |
// Extract red, green, and blue components (should be equal if grayscale) | |
$r = ($depthColor >> 16) & 0xff; | |
$g = ($depthColor >> 8) & 0xff; | |
$b = $depthColor & 0xff; | |
// Calculate the grayscale value accurately (could use averaging or other formulas) | |
$gray = (int) (($r + $g + $b) / 3); // Average the RGB values to get grayscale | |
// Calculate the horizontal shift (depth) based on the grayscale value | |
// We can use the grayscale value more precisely to map the depth | |
$shift = (int) (($gray / 255.0) * 30); // Depth mapping factor (20 can be adjusted) | |
// Wrap the pattern image horizontally using modulo | |
$patternX = ($x + $shift) % $patternWidth; | |
$patternY = $y % $patternHeight; | |
// Get the color from the pattern | |
$patternColor = imagecolorat($pattern, $patternX, $patternY); | |
// Set the corresponding pixel in the output image | |
imagesetpixel($outputImage, $x, $y, $patternColor); | |
} | |
} | |
// Output the final autostereogram image | |
header("Content-Type: image/png"); | |
//imagepng($outputImage); | |
if (!is_dir($directory_name . "/output")) { | |
mkdir($directory_name . "/output"); | |
} | |
// $im = imagecreatefrompng( | |
// $directory_name . "/output/" . $source, | |
// $outputImage | |
// ); | |
imagepng($outputImage, $directory_name . "/output/" . $source); | |
// Clean up memory | |
imagedestroy($depthMap); | |
imagedestroy($pattern); | |
imagedestroy($outputImage); | |
} | |
foreach ($directory as $file) { | |
if (!is_dir($directory_name . "/" . $file)) { | |
makeMagicEye($directory_name, $file); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment