Skip to content

Instantly share code, notes, and snippets.

@yangg
Created August 20, 2012 07:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yangg/3401763 to your computer and use it in GitHub Desktop.
Save yangg/3401763 to your computer and use it in GitHub Desktop.
PHP Functions
<?php
$height = isset($_GET['height']) ? $_GET['height'] : 80;
$width = isset($_GET['width']) ? $_GET['width'] : 80;
// top | left
$dir = isset($_GET['direction']) ? $_GET['direction'] : 'top';
$start = $_GET['start'];
$end = $_GET['end'];
list($r, $g, $b) = hex2rgb($start);
list($r2, $g2, $b2) = hex2rgb($end);
$img = imagecreatetruecolor($width, $height);
$pixel = $dir == 'top' ? $height : $width;
for($i = 0; $i < $pixel; $i++) {
$color = imagecolorallocate($img,
round($r - ($r - $r2)/($pixel-1)*$i),
round($g - ($g - $g2)/($pixel-1)*$i),
round($b - ($b - $b2)/($pixel-1)*$i));
imageline($img,
$dir == 'top' ? 0 : $i, $dir == 'top' ? $i : 0,
$dir == 'top' ? $width : $i, $dir == 'top' ? $i : $height,
$color);
}
// output image
header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);
function hex2rgb($color) {
return array_map(function($val) {
return hexdec($val);
}, str_split($color, 2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment