<?php
/*
* bicolore.php5
*
* Constructs an image with randomly colored layers.
* Requires PHP5 or later (for imagefilter).
*
* Depending on the configuration of your PHP, gd2 must be imported manually.
* Also, you might want to replace the suffix ".php5" with ".php".
*
* Example for a call:
*
* http://www.janthor.com/bicolore/bicolore.php5
*
* or
*
* http://www.janthor.com/bicolore/bicolore.php5?r1=255&g1=255&b1=128&r2=0&g2=64&b2=153&hair=1&gun=2&bra=2&briefs=2&boots=2
*
* r1, g1, b1 and r2, g2, b2 are two colors (values in the range 0 to 255).
* hair, gun, bra, briefs, boots decide whether these items have color 1 or
* color 2. Any value not present is replaced with a random value.
*
* The images earth.png, sky.png, babe.png, haar.png, gun.png, bra.png,
* brief.png and socks.png are expected to be of the same size and in the same
* directory as bicolore.php5.
*
* Jan Thor, 2007-08-09
*/
$skyname = "earth";
$earthname = "sky";
$basename = "babe";
$thingnames = array("haar", "gun", "bra", "brief", "socks");
$colorstamp = True; /* adds a stamp with the values of the two random colors */
$stampcolor = array(0, 0, 0, 64); /* red green blue 0-255 alpha 0-127 */
/* load the images */
$sky = imagecreatefrompng($skyname . ".png");
$earth = imagecreatefrompng($earthname . ".png");
$base = imagecreatefrompng($basename . ".png");
$things = array();
foreach ($thingnames as $name) $things[] = imagecreatefrompng($name . ".png");
/* define two random colors, and color choices for the items */
$r1 = rand(0, 255); $g1 = rand(0, 255); $b1 = rand(0, 255);
$r2 = rand(0, 255); $g2 = rand(0, 255); $b2 = rand(0, 255);
$i1 = rand(0, 1); $i2 = rand(0, 1); $i3 = rand(0, 1); $i4 = rand(0, 1); $i5 = rand(0, 1);
/* allow caller to override */
if (isset($_GET['r1']) and (($_GET['r1'] >= 0) and ($_GET['r1'] < 256))) $r1 = $_GET['r1'];
if (isset($_GET['g1']) and (($_GET['g1'] >= 0) and ($_GET['g1'] < 256))) $g1 = $_GET['g1'];
if (isset($_GET['b1']) and (($_GET['b1'] >= 0) and ($_GET['b1'] < 256))) $b1 = $_GET['b1'];
if (isset($_GET['r2']) and (($_GET['r2'] >= 0) and ($_GET['r2'] < 256))) $r2 = $_GET['r2'];
if (isset($_GET['g2']) and (($_GET['g2'] >= 0) and ($_GET['g2'] < 256))) $g2 = $_GET['g2'];
if (isset($_GET['b2']) and (($_GET['b2'] >= 0) and ($_GET['b2'] < 256))) $b2 = $_GET['b2'];
if (isset($_GET['hair']) and (($_GET['hair'] > 0) and ($_GET['hair'] < 3))) $i1 = $_GET['hair'] - 1;
if (isset($_GET['gun']) and (($_GET['gun'] > 0) and ($_GET['gun'] < 3))) $i2 = $_GET['gun'] - 1;
if (isset($_GET['bra']) and (($_GET['bra'] > 0) and ($_GET['bra'] < 3))) $i3 = $_GET['bra'] - 1;
if (isset($_GET['briefs']) and (($_GET['briefs'] > 0) and ($_GET['briefs'] < 3))) $i4 = $_GET['briefs'] - 1;
if (isset($_GET['boots']) and (($_GET['boots'] > 0) and ($_GET['boots'] < 3))) $i5 = $_GET['boots'] - 1;
$choices = array($i1, $i2, $i3, $i4, $i5);
/* function to tint images */
function tint($img, $r, $g, $b) {
imagealphablending($img, False);
imagefilter($img, IMG_FILTER_NEGATE);
imagefilter($img, IMG_FILTER_COLORIZE, 255 - $r, 255 - $g, 255 - $b);
imagefilter($img, IMG_FILTER_NEGATE);
imagealphablending($img, True);
}
/* now tint the images */
tint($sky, $r1, $g1, $b1);
tint($earth, $r2, $g2, $b2);
$i = 0;
foreach ($things as $thing) {
if ($choices[$i] == 0) tint($thing, $r1, $g1, $b1);
else tint($thing, $r2, $g2, $b2);
$i = $i + 1;
}
/* merge all images into one */
$w = imagesx($sky);
$h = imagesy($sky);
imagecopy($sky, $earth, 0, 0, 0, 0, $w, $h);
imagecopy($sky, $base, 0, 0, 0, 0, $w, $h);
foreach ($things as $thing) imagecopy($sky, $thing, 0, 0, 0, 0, $w, $h);
/* Add color stamp */
if ($colorstamp) {
$string = sprintf("#%02x%02x%02x/#%02x%02x%02x", $r1, $g1, $b1, $r2, $g2, $b2);
$textcolor = imagecolorallocatealpha($sky, $stampcolor[0], $stampcolor[1],
$stampcolor[2], $stampcolor[3]);
$tx = ($w - 7 * strlen($string)) / 2;
imagestring($sky, 3, $tx, $h - 16, $string, $textcolor);
}
/* send the image */
imagesavealpha($sky, TRUE);
header("Content-type: image/png");
imagepng($sky);
imagedestroy($sky);
imagedestroy($earth);
imagedestroy($base);
foreach($things as $thing) imagedestroy($thing);
?>