Skip to content

Instantly share code, notes, and snippets.

@yangg
Created August 20, 2012 07:08
Show Gist options
  • Select an option

  • Save yangg/3401763 to your computer and use it in GitHub Desktop.

Select an option

Save yangg/3401763 to your computer and use it in GitHub Desktop.

Revisions

  1. yangg created this gist Aug 20, 2012.
    36 changes: 36 additions & 0 deletions linear-gradient.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    <?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));
    }