Skip to content

Instantly share code, notes, and snippets.

@yugecin
Created May 13, 2017 13:15
Show Gist options
  • Save yugecin/60795dfa5b450c078cac96440428d52b to your computer and use it in GitHub Desktop.
Save yugecin/60795dfa5b450c078cac96440428d52b to your computer and use it in GitHub Desktop.
php background triangle pattern generator
<?php
if (empty($_GET['w']) || empty($_GET['h']) || empty($_GET['tw'])) {
die('w=width&h=height&tw=trianglewidth');
}
$w = $_GET['w'];
$h = $_GET['h'];
$tw = $_GET['tw'];
$sx = $tw;
$sy = $sx * 2 / sqrt(3);
$im = @imagecreate($w, $h) or die("Cannot Initialize new GD image stream");
if (!isset($_GET['d'])) header("Content-Type: image/png");
imagefilledrectangle($im, 0, 0, $w, $h, imagecolorallocatealpha($im, 90, 90, 90, 0));
$_m = 127 / 255;
$c = array(
imagecolorallocatealpha($im, 255, 255, 255, (255 - 27) * $_m),
imagecolorallocatealpha($im, 255, 255, 255, (255 - 36) * $_m),
imagecolorallocatealpha($im, 255, 255, 255, (255 - 45) * $_m),
imagecolorallocatealpha($im, 255, 255, 255, (255 - 43) * $_m),
imagecolorallocatealpha($im, 255, 255, 255, (255 - 18) * $_m),
);
$cc = count($c) - 1;
$bc = imagecolorallocatealpha($im, 255, 255, 255, (255 - 41) * $_m);
for ($i = 0; $i < $w / $sx; $i++) {
$z = 0;
for ($j = -$sy; $j < $h; $j += $sy / 2) {
$z = $z ^ 1;
$points = array(
$i * $sx, $j,
$i * $sx + $sx, $j + $sy / 2,
$i * $sx, $j + $sy,
);
if ($i % 2 == 0) {
for ($k = 0; $k < 3; $k++) {
$points[$k * 2 + 1] += $sy / 2;
}
}
if ($z % 2 == 0) {
$points[0] += $sx;
$points[2] -= $sx;
$points[4] += $sx;
}
imagefilledpolygon($im, $points, 3, $c[rand(0, $cc)]);
imageline($im, $points[0], $points[1], $points[2], $points[3], $bc);
imageline($im, $points[2], $points[3], $points[4], $points[5], $bc);
}
}
imagepng($im);
imagedestroy($im);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment