Skip to content

Instantly share code, notes, and snippets.

@quisido
Created August 2, 2018 17:13
Show Gist options
  • Save quisido/e6cd3dc0fd922a84064a8c7643bc45a6 to your computer and use it in GitHub Desktop.
Save quisido/e6cd3dc0fd922a84064a8c7643bc45a6 to your computer and use it in GitHub Desktop.
Creating a Dynamic Vertical Gradient in PHP
<?php
// Tell the browser it's a GIF image.
header('Content-Type: image/gif');
/*
Create an image with a width of 1 and height of $height.
Width only needs to be 1px because it will presumably
be tiled horizontally.
*/
$gradient = imagecreatetruecolor(1, $height);
// For each pixel in the image...
for ($x = 0; $x < $height; $x++) {
// Take the start color and increment it a step for every pixel.
$red = $start[0] + $step[0] * $x;
$green = $start[1] + $step[1] * $x;
$blue = $start[2] + $step[2] * $x;
// Create an image-readable color value.
$color = imagecolorallocate($gradient, $red, $green, $blue);
// Set the color of the pixel 0 from the left and
// $x from the top to $color.
imagesetpixel($gradient, 0, $x, $color);
}
// Output the image.
imagegif($gradient);
// Destroy the image from memory.
imagedestroy($gradient);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment