Created
August 20, 2012 07:08
-
-
Save yangg/3401763 to your computer and use it in GitHub Desktop.
PHP Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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