Skip to content

Instantly share code, notes, and snippets.

@ninsuo
Created March 6, 2019 09:58
Show Gist options
  • Save ninsuo/b691aa9f82d438f4ed64f578294f31fe to your computer and use it in GitHub Desktop.
Save ninsuo/b691aa9f82d438f4ed64f578294f31fe to your computer and use it in GitHub Desktop.
degrade.php - the most ugly code i ever made in life
<?php
// ------------------------------------------
// DEGRADE ( GD )
// ------------------------------------------
// ---------------------------------------------
// Allowed GET arguments:
// a -> applies transparency to background color
// c -> colors (use '-' for color separator)
// g -> background color (for circle box)
// h -> horizontal box size (height)
// r -> rotation (0 = 0 ; 1 = 90 ; 2 = 180 ; 3 = 270)
// t -> type (1 = ||| ; 2 = \\\ ; 3 = x ; 4 = o)
// v -> vertical box size (width)
// ---------------------------------------------
// Usage example:
// degrade.php?&h=500&v=500&c=FF0000-00FF00-0000FF-FFFF00-FFFFFF-FF00FF-00FFFF&t=1&r=1
// degrade.php?&h=500&v=500&c=FF0000-00FF00-0000FF-FFFF00-00FFFF-FF00FF-FFFFFF&t=2&r=2
// degrade.php?&h=500&v=300&c=FF0000-0000FF-00FF00-FF00FF-FFFF00-FFFFFF-00FFFF&t=3&r=3
// degrade.php?&h=500&v=500&c=FF0000-00FF00-0000FF-FFFF00-00FFFF-FF00FF&t=4&r=1&g=424242&a=1
// ---------------------------------------------
// ---------------------------------------------
// Parsing parameters
// ---------------------------------------------
// 'a' (transparency)
$alpha = false;
if (isset($_GET['a']) && ($_GET['a'] == 1)) {
$alpha = true;
}
// 'c' (colors)
if (!isset($_GET['c'])) {
$c = "000000-FFFFFF";
}
else {
$c = trim(addslashes($_GET['c']));
}
$c = explode("-", $c);
if (count($c) < 2) {
$c[0] = "000000";
$c[1] = "FFFFFF";
}
$count = (count($c) - 1);
for ($i = 0; ($i <= $count); $i++) {
$c[$i] = (int)hexdec($c[$i]);
if ($c[$i] > 0xFFFFFF) {
$c[$i] = 0;
}
}
// 'h' (height)
if (!isset($_GET['h'])) {
$h = 100;
} else {
$h = trim(addslashes($_GET['h']));
}
if (!is_numeric($h)) {
$h = 100;
}
if ($h > 2000 || $h < 1) {
$h = 100;
}
// 'v' (width)
if (!isset($_GET['v'])) {
$v = 100;
} else {
$v = trim(addslashes($_GET['v']));
}
if (!is_numeric($v)) {
$v = 100;
}
if ($v > 2000 || $v < 1) {
$v = 100;
}
// 't' (type)
if (!isset($_GET['t'])) {
$t = 1;
} else {
$t = trim(addslashes($_GET['t']));
}
if ((!is_numeric($t)) || ($t < 1) || ($t > 4)) {
$t = 1;
}
// 'r' (rotation)
if (!isset($_GET['r'])) {
$r = 1;
} else {
$r = trim(addslashes($_GET['r']));
}
if ((!is_numeric($r)) || ($r < 1) || ($r > 4)) {
$r = 1;
}
// 'g' (background)
if (!isset($_GET['g'])) {
$g = 4342338;
}
else {
$g = hexdec(trim(addslashes($_GET['g'])));
}
// ---------------------------------------------
// Functions
// ---------------------------------------------
function reverse($old) {
$c = NULL;
$n = 0;
for ($i = (count($old) - 1); ($i >= 0); $i--) {
$c[$n++] = $old[$i];
}
return ($c);
}
function callibrate($img, $red, $green, $blue)
{
if ($red > 0x0A) {
$red -= 0x0A;
}
if ($green > 0x0A) {
$green -= 0x0A;
}
if ($blue > 0x0A) {
$blue -= 0x0A;
}
return (ImageColorAllocate($img, $red, $green, $blue));
}
// ---------------------------------------------
// Processing ...
// ---------------------------------------------
// creating image and filling background
Header("Content-type: image/png");
$img = ImageCreateTrueColor($h, $v);
ImageColorAllocate($img, ($g / (256 * 256)), (($g / 256) % 256), ($g % 256));
ImageSetThickness($img, 1);
// type = linear
if ($t == 1) {
// horizontal
if (($r == 1) || ($r == 3)) {
if ($r == 3) {
$c = reverse($c);
}
// c1 and c2 are calculated according to image width
for ($k = 0; ($k < $count); $k++) {
$c1 = $c[($k + 1)];
$c2 = $c[$k];
// degrade from c1 to c2
for ($y = (($k * $v) / $count); ($y < ((($k + 1) * $v) / $count)); $y++) {
$red = ((((($c1 / 65536) * ((1024 * ($y - ($k * $v / $count))) / $v * $count)) + (($c2 / 65536) * (1024 - ((1024 * ($y - ($k * $v / $count))) / $v * $count))))) / 1024);
$green = ((((($c1 / 256 % 256) * ((1024 * ($y - ($k * $v / $count))) / $v * $count)) + (($c2 / 256 % 256) * (1024 - ((1024 * ($y - ($k * $v / $count))) / $v * $count))))) / 1024);
$blue = ((((($c1 % 256) * ((1024 * ($y - ($k * $v / $count))) / $v * $count)) + (($c2 % 256) * (1024 - ((1024 * ($y - ($k * $v / $count))) / $v * $count))))) / 1024);
$color = callibrate($img, $red, $green, $blue);
ImageLine($img, 0, $y, $h, $y, $color);
}
}
}
// vertical
if (($r == 2) || ($r == 4)) {
if ($r == 4) {
$c = reverse($c);
}
// c1 and c2 are calculated according to image width
for ($k = 0; ($k < $count); $k++) {
$c1 = $c[($k + 1)];
$c2 = $c[$k];
// degrade from c1 to c2
for ($x = (($k * $h) / $count); ($x < ((($k + 1) * $h) / $count)); $x++) {
$red = ((((($c1 / 65536) * ((1024 * ($x - ($k * $h / $count))) / $h * $count)) + (($c2 / 65536) * (1024 - ((1024 * ($x - ($k * $h / $count))) / $h * $count))))) / 1024);
$green = ((((($c1 / 256 % 256) * ((1024 * ($x - ($k * $h / $count))) / $h * $count)) + (($c2 / 256 % 256) * (1024 - ((1024 * ($x - ($k * $h / $count))) / $h * $count))))) / 1024);
$blue = ((((($c1 % 256) * ((1024 * ($x - ($k * $h / $count))) / $h * $count)) + (($c2 % 256) * (1024 - ((1024 * ($x - ($k * $h / $count))) / $h * $count))))) / 1024);
$color = callibrate($img, $red, $green, $blue);
ImageLine($img, $x, 0, $x, $v, $color);
}
}
}
}
// type = diagonal
if ($t == 2) {
$hyp = sqrt(($h * $h) + ($v * $v));
if ((($count + 1) % 2) == 1) {
$color = callibrate($img, ($c[($count / 2)] / 65536), (($c[($count / 2)] / 256) % 256), ($c[($count / 2)] % 256));
ImageLine($img, 0, 0, $h, $v, $color);
}
$count = $count / 2;
// c1 and c2 are calculated according to image width
for ($k = 0; ($k < $count); $k++) {
$c1 = $c[($k + 1)];
$c2 = $c[$k];
$c3 = $c[(($count * 2) - ($k + 1))];
$c4 = $c[(($count * 2) - $k)];
// drawing lines using thales
for ($dist = (($k * $hyp) / $count); ($dist <= ((($k + 1) * $hyp) / $count)); $dist++) {
$x = (((($dist * 1024) / $hyp) * $h) / 1024);
$y = (((($dist * 1024) / $hyp) * $v) / 1024);
$red = ((((($c1 / 65536) * ((1024 * ($dist - ($k * $hyp / $count))) / $hyp * $count)) + (($c2 / 65536) * (1024 - ((1024 * ($dist - ($k * $hyp / $count))) / $hyp * $count))))) / 1024);
$green = ((((($c1 / 256 % 256) * ((1024 * ($dist - ($k * $hyp / $count))) / $hyp * $count)) + (($c2 / 256 % 256) * (1024 - ((1024 * ($dist - ($k * $hyp / $count))) / $hyp * $count))))) / 1024);
$blue = ((((($c1 % 256) * ((1024 * ($dist - ($k * $hyp / $count))) / $hyp * $count)) + (($c2 % 256) * (1024 - ((1024 * ($dist - ($k * $hyp / $count))) / $hyp * $count))))) / 1024);
$color1 = callibrate($img, $red, $green, $blue);
$red = ((((($c3 / 65536) * ((1024 * ($dist - ($k * $hyp / $count))) / $hyp * $count)) + (($c4 / 65536) * (1024 - ((1024 * ($dist - ($k * $hyp / $count))) / $hyp * $count))))) / 1024);
$green = ((((($c3 / 256 % 256) * ((1024 * ($dist - ($k * $hyp / $count))) / $hyp * $count)) + (($c4 / 256 % 256) * (1024 - ((1024 * ($dist - ($k * $hyp / $count))) / $hyp * $count))))) / 1024);
$blue = ((((($c3 % 256) * ((1024 * ($dist - ($k * $hyp / $count))) / $hyp * $count)) + (($c4 % 256) * (1024 - ((1024 * ($dist - ($k * $hyp / $count))) / $hyp * $count))))) / 1024);
$color2 = callibrate($img, $red, $green, $blue);
// rotate = 0*
if ($r == 1) {
ImageLine($img, $x, 0, 0, $y, $color1);
ImageLine($img, ($h - $x), $v, $h, ($v - $y), $color2);
}
// rotate = 90*
else if ($r == 2) {
ImageLine($img, ($h - $x), 0, $h, $y, $color1);
ImageLine($img, 0, ($v - $y), $x, $v, $color2);
}
// rotate = 180*
else if ($r == 3) {
ImageLine($img, ($h - $x), $v, $h, ($v - $y), $color1);
ImageLine($img, $x, 0, 0, $y, $color2);
}
// rotate = 270*
else {
ImageLine($img, 0, ($v - $y), $x, $v, $color1);
ImageLine($img, ($h - $x), 0, $h, $y, $color2);
}
}
}
}
// type = square
if ($t == 3) {
if (($r == 2) || ($r == 4)) {
$c = reverse($c);
}
$hyp = sqrt((($h / 2) * ($h / 2)) + (($v / 2) * ($v / 2)));
// c1 and c2 are calculated according to image width
for ($k = 0; ($k < $count); $k++) {
$c1 = $c[($k + 1)];
$c2 = $c[$k];
// filling square by running on square hypotenuse
for ($dist = (($k * $hyp) / $count); ($dist <= ((($k + 1) * $hyp) / $count)); $dist++) {
$red = ((((($c1 / 65536) * ((1024 * ($dist - ($k * $hyp / $count))) / $hyp * $count)) + (($c2 / 65536) * (1024 - ((1024 * ($dist - ($k * $hyp / $count))) / $hyp * $count))))) / 1024);
$green = ((((($c1 / 256 % 256) * ((1024 * ($dist - ($k * $hyp / $count))) / $hyp * $count)) + (($c2 / 256 % 256) * (1024 - ((1024 * ($dist - ($k * $hyp / $count))) / $hyp * $count))))) / 1024);
$blue = ((((($c1 % 256) * ((1024 * ($dist - ($k * $hyp / $count))) / $hyp * $count)) + (($c2 % 256) * (1024 - ((1024 * ($dist - ($k * $hyp / $count))) / $hyp * $count))))) / 1024);
$color = callibrate($img, $red, $green, $blue);
ImageLine($img, (($h * $dist) / (2 * $hyp)), (($v * $dist) / (2 * $hyp)), (($h * $dist) / (2 * $hyp)), ($v - (($v * $dist) / (2 * $hyp))), $color);
ImageLine($img, ($h - (($h * $dist) / (2 * $hyp))), (($v * $dist) / (2 * $hyp)), ($h - (($h * $dist) / (2 * $hyp))), ($v - (($v * $dist) / (2 * $hyp))), $color);
ImageLine($img, (($h * $dist) / (2 * $hyp)), (($v * $dist) / (2 * $hyp)), ($h - (($h * $dist) / (2 * $hyp))), (($v * $dist) / (2 * $hyp)), $color);
ImageLine($img, (($h * $dist) / (2 * $hyp)), ($v - (($v * $dist) / (2 * $hyp))), ($h - (($h * $dist) / (2 * $hyp))), ($v - (($v * $dist) / (2 * $hyp))), $color);
}
}
}
// type = circle
if ($t == 4) {
ImageFilledRectangle($img, 0, 0, ($h - 1), ($v - 1), $g);
// if alpha is ordered, background color becomes transparent
if ($alpha == true) {
ImageTrueColorToPalette($img, FALSE, 256);
$transparency = ImageColorAt($img, 0, 0);
ImageColorTransparent($img, $transparency);
ImageAlphaBlending($img, TRUE);
ImageCopyMerge($img, $img, 0, 0, 0, 0, $h, $v, 100);
}
if (($r == 1) || ($r == 3)) {
$c = reverse($c);
}
$v /= 2;
// c1 and c2 are calculated according to image width
for ($k = ($count - 1); ($k != -1); $k--) {
$c1 = $c[($k + 1)];
$c2 = $c[$k];
// draws circles, they ray runs on image's (height / 2)
for ($y = ((($k + 1) * $v) / $count); ($y > (($k * $v) / $count)); $y--) {
$red = ((((($c1 / 65536) * ((1024 * ($y - ($k * $v / $count))) / $v * $count)) + (($c2 / 65536) * (1024 - ((1024 * ($y - ($k * $v / $count))) / $v * $count))))) / 1024);
$green = ((((($c1 / 256 % 256) * ((1024 * ($y - ($k * $v / $count))) / $v * $count)) + (($c2 / 256 % 256) * (1024 - ((1024 * ($y - ($k * $v / $count))) / $v * $count))))) / 1024);
$blue = ((((($c1 % 256) * ((1024 * ($y - ($k * $v / $count))) / $v * $count)) + (($c2 % 256) * (1024 - ((1024 * ($y - ($k * $v / $count))) / $v * $count))))) / 1024);
$color = callibrate($img, $red, $green, $blue);
$x = ((($y * 1024) / $v) * ($h / 1024));
ImageFilledEllipse($img, ($h / 2), $v, $x, ($y * 2), $color);
}
}
}
// dumping image
ImagePng($img);
ImageDestroy($img);
<?php
/*
** Degrade - usage examples
*/
// ----------------------------------------------------------------
function getLink($width, $height, $colors, $type, $rotate, $background, $transparency)
{
return ('degrade.php?&h=' . $width . '&v=' . $height . '&c=' . $colors . '&t=' . $type . '&r=' . $rotate . '&g=' . $background . '&a=' . $transparency);
}
function dumpImg($width, $height, $colors, $type, $rotate, $background="424242", $transparency="0")
{
echo '<br/>';
echo '<img src="' . getLink($width, $height, $colors, $type, $rotate, $background, $transparency) . '" />';
echo '<br/><br/>';
}
// ----------------------------------------------------------------
echo '<body bgcolor="#DDDDDD">';
echo '<table>';
// Linear degrade (type = 1)
echo '<tr>';
echo '<td colspan="4"><span style="font-size: 30px;">A - Linear</span></td>';
echo '</tr>';
echo '<tr>';
echo '<td align="center">';
dumpImg($width=250,
$height=250,
$colors="FF0000-00FF00-0000FF-FFFF00-00FFFF-FF00FF",
$type=1,
$rotate=1);
echo 'Rotation: 0*';
echo '</td>';
echo '<td align="center">';
dumpImg($width=250,
$height=250,
$colors="FF0000-00FF00-0000FF-FFFF00-00FFFF-FF00FF",
$type=1,
$rotate=2);
echo 'Rotation: 90*';
echo '</td>';
echo '<td align="center">';
dumpImg($width=250,
$height=250,
$colors="FF0000-00FF00-0000FF-FFFF00-00FFFF-FF00FF",
$type=1,
$rotate=3);
echo 'Rotation: 180*';
echo '</td>';
echo '<td align="center">';
dumpImg($width=250,
$height=250,
$colors="FF0000-00FF00-0000FF-FFFF00-00FFFF-FF00FF",
$type=1,
$rotate=4);
echo 'Rotation: 270*';
echo '</td>';
echo '</tr>';
// Diagonal degrade (type = 2)
echo '<tr>';
echo '<td colspan="4">&nbsp;</td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="4"><span style="font-size: 30px;">B - Diagonal</span></td>';
echo '</tr>';
echo '<tr>';
echo '<td align="center">';
dumpImg($width=250,
$height=250,
$colors="FF0000-00FF00-0000FF-FFFF00-00FFFF-FF00FF",
$type=2,
$rotate=1);
echo 'Rotation: 0*';
echo '</td>';
echo '<td align="center">';
dumpImg($width=250,
$height=250,
$colors="FF0000-00FF00-0000FF-FFFF00-00FFFF-FF00FF",
$type=2,
$rotate=2);
echo 'Rotation: 90*';
echo '</td>';
echo '<td align="center">';
dumpImg($width=250,
$height=250,
$colors="FF0000-00FF00-0000FF-FFFF00-00FFFF-FF00FF",
$type=2,
$rotate=3);
echo 'Rotation: 180*';
echo '</td>';
echo '<td align="center">';
dumpImg($width=250,
$height=250,
$colors="FF0000-00FF00-0000FF-FFFF00-00FFFF-FF00FF",
$type=2,
$rotate=4);
echo 'Rotation: 270*';
echo '</td>';
echo '</tr>';
// Square degrade (type = 3)
echo '<tr>';
echo '<td colspan="4">&nbsp;</td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="4"><span style="font-size: 30px;">C - Square</span></td>';
echo '</tr>';
echo '<tr>';
echo '<td align="center">';
dumpImg($width=250,
$height=250,
$colors="FF0000-00FF00-0000FF-FFFF00-00FFFF-FF00FF",
$type=3,
$rotate=1);
echo 'Rotation: 1 (normal)';
echo '</td>';
echo '<td align="center">';
dumpImg($width=250,
$height=250,
$colors="FF0000-00FF00-0000FF-FFFF00-00FFFF-FF00FF",
$type=3,
$rotate=2);
echo 'Rotation: 2 (reverse)';
echo '</td>';
echo '<td align="center">&nbsp;</td>';
echo '<td align="center">&nbsp;</td>';
echo '</tr>';
// Circle degrade (type = 4)
echo '<tr>';
echo '<td colspan="4">&nbsp;</td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="4"><span style="font-size: 30px;">D - Circle</span></td>';
echo '</tr>';
echo '<tr>';
echo '<td align="center">';
dumpImg($width=250,
$height=250,
$colors="FF0000-00FF00-0000FF-FFFF00-00FFFF-FF00FF",
$type=4,
$rotate=1);
echo 'Rotation: 1 (normal)';
echo '</td>';
echo '<td align="center">';
dumpImg($width=250,
$height=250,
$colors="FF0000-00FF00-0000FF-FFFF00-00FFFF-FF00FF",
$type=4,
$rotate=2);
echo 'Rotation: 2 (reverse)';
echo '</td>';
echo '<td align="center">';
dumpImg($width=250,
$height=250,
$colors="FF0000-00FF00-0000FF-FFFF00-00FFFF-FF00FF",
$type=4,
$rotate=1,
$background="DDDD55",
$transparency=0);
echo 'With defined background';
echo '</td>';
echo '<td align="center">';
dumpImg($width=250,
$height=250,
$colors="FF0000-00FF00-0000FF-FFFF00-00FFFF-FF00FF",
$type=4,
$rotate=2,
$background="000000",
$transparency=1);
echo 'With transparent background';
echo '</td>';
echo '</tr>';
echo '</table>';
echo '<br/><br/>';
// OTHER EXAMPLES
echo '<div style="font-size: 30px;"><span style="color: #FF0000;">Other</span> examples</div>';
// the text box
echo '<br/><br/>';
$bigDiv = getLink($width=500,
$height=150,
$colors="ffffff-7777aa-ffffff-7777aa-ffffff",
$type=2,
$rotate=1,
$background="",
$transparency=0);
$headDiv = getLink($width=500,
$height=20,
$colors="000055-7777aa-000055",
$type=1,
$rotate=1,
$background="",
$transparency=0);
echo '<div style="width: 500px; height: 150px; background-image: url(' . $bigDiv . '); border: 2px solid #000055;">';
echo '<div style="width: 500px; height: 20px; background-image: url(' . $headDiv . ');">';
echo '<font color="white">A beatiful title</font>';
echo '</div>';
echo 'A beatiful templatable text box';
echo '</div>';
echo '<br/><br/>';
// some colors
dumpImg($width=500,
$height=500,
$colors="c70e2f-94040d-f550a9-e18ebb-fdd7ed-000000-0e0769-3f16bf-5babeb-7ac8f5-c4f5fb-000000-a30a3b-c5113f-f33a0b-f97242-f4b27a-fbd3ba-000000-590773-8d0ea9-c53ae3-ec70f4-ebbdf9-000000-55061f-000000",
$type=2,
$rotate=1);
echo '<br/><br/>';
echo '</body>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment