Source of: pngthing.php


<?php

function hex2dec($in) {
$colr = str_replace('#', '', $in);
$ret = array(
'r' => hexdec(substr($colr, 0, 2)),
'g' => hexdec(substr($colr, 2, 2)),
'b' => hexdec(substr($colr, 4, 2))
);
return
$ret;
}

$hex  = $_POST['hex'];
$r  = round($_POST['red']);
$g  = round($_POST['green']);
$b  = round($_POST['blue']);
$w  = round($_POST['width']);
$h  = round($_POST['height']);

$tried = $w || $h ;

function
hexColor($color) {
return
dechex(($color[0]<<16)|($color[1]<<8)|$color[2]);
}
$conv = hex2dec($hex);
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
    <table border="0" cellpadding="0">
        <tr>
            <td>
                Hex
            </td>
            <td>
                <input type="text" maxlength="7" size="7" name="hex" value="<?php echo $hex; ?>" />
            </td>
        </tr>
        <tr>
            <td>
                Red
            </td>
            <td>
                <input type="text" maxlength="3" size="4" name="red" value="<?php if (!$hex) {if ($r < .4){$r = 0;} else if ($r > 255){$r = 255;} echo $r;} else {$r = $conv[r]; echo $r;} ?>" /> 0 - 255
            </td>
        </tr>
        <tr>
            <td>
                Green
            </td>
            <td>
                <input type="text" maxlength="3" size="4" name="green" value="<?php if (!$hex) {if ($g < .4){$g = 0;} else if ($g > 255){$g = 255;} echo $g;} else {$g = $conv[g]; echo $g;} ?>" /> 0 - 255
            </td>
        </tr>
        <tr>
            <td>
                Blue
            </td>
            <td>
                <input type="text" maxlength="3" size="4" name="blue" value="<?php if (!$hex) {if ($b < .4){$b = 0;} else if ($b > 255){$b = 255;} echo $b;} else {$b = $conv[b]; echo $b;} ?>" /> 0 - 255
            </td>
        </tr>
        <tr>
            <td>
<?php
$col
[0] = $r;
$col[1] = $g;
$col[2] = $b;
echo
"hex value = </td><td> #".hexColor($col);
?>
            </td>
        </tr>
        <tr>
            <td>
                Width
            </td>
            <td>
                <input type="text" maxlength="3" size="4" name="width" value="<?php if ($w < 1){$w = 100; echo $w;} else {echo $w;} ?>" /> pixels
            </td>
        </tr>
        <tr>
            <td>
                Height
            </td>
            <td>
                <input type="text" maxlength="3" size="4" name="height" value="<?php if ($h < 1){$h = 100; echo $h;} else {echo $h;} ?>" /> pixels
            </td>
        </tr>
    </table>
    <p>
        <input type="hidden" name="tried" value="yes" /> <input type="submit" value="<?php echo $tried ? 'redraw' : 'draw'; ?>" />
    </p>
</form>
<?php
$id
= uniqid("");

$pic=ImageCreate($w,$h);
$color=ImageColorAllocate($pic,$r,$g,$b);
$grey=ImageColorAllocate($pic,255,252,252);
$red=ImageColorAllocate($pic,255,200,200);
$black=ImageColorAllocate($pic,0,0,0);

ImageFilledRectangle($pic,0,0,$w,$h,$color);

ImagePNG($pic,"cache/$id.png");
ImageDestroy($pic);
echo
"<div><img src=\"cache/$id.png\" alt=\"picture 1\" /><br />";

?>

Return to pngthing.php