Last active
December 11, 2015 07:28
-
-
Save charlycoste/4566269 to your computer and use it in GitHub Desktop.
Gradient generator
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 | |
// get some values for the params from get; we also have some default values that render an 100x100 image with a horizontal gradient from black to white | |
$width = 100; | |
if (isset($_GET['width'])) // the width of out output pic | |
{ | |
$width = filter_var($_GET['width'], FILTER_VALIDATE_INT); // I am not using filter_input without the isset() test because some applications will use url rewriting | |
} | |
$height = 100; | |
if (isset($_GET['height'])) // height of the pic | |
{ | |
$height = filter_var($_GET['height'], FILTER_VALIDATE_INT); | |
} | |
$from_r = 0; | |
if (isset($_GET['from_r'])) // red component of the starting color | |
{ | |
$from_r = filter_var($_GET['from_r'], FILTER_VALIDATE_INT); | |
} | |
$from_g = 0; | |
if (isset($_GET['from_g'])) // start color: green | |
{ | |
$from_g = filter_var($_GET['from_g'], FILTER_VALIDATE_INT); | |
} | |
$from_b = 0; | |
if (isset($_GET['from_b'])) // start color: blue | |
{ | |
$from_b = filter_var($_GET['from_b'], FILTER_VALIDATE_INT); | |
} | |
$from_a = 0; | |
if (isset($_GET['from_a'])) // start alpha: plain | |
{ | |
$from_a = filter_var($_GET['from_a'], FILTER_VALIDATE_INT); | |
} | |
$to_r = 255; | |
if (isset($_GET['to_r'])) // red component of our final color | |
{ | |
$to_r = filter_var($_GET['to_r'], FILTER_VALIDATE_INT); | |
} | |
$to_g = 255; | |
if (isset($_GET['to_g'])) // final: green | |
{ | |
$to_g = filter_var($_GET['to_g'], FILTER_VALIDATE_INT); | |
} | |
$to_b = 255; | |
if (isset($_GET['to_b'])) // final: blue | |
{ | |
$to_b = filter_var($_GET['to_b'], FILTER_VALIDATE_INT); | |
} | |
$to_a = 0; | |
if (isset($_GET['to_a'])) // final alpha: plain | |
{ | |
$to_a = filter_var($_GET['to_a'], FILTER_VALIDATE_INT); | |
} | |
// direction for each of the gradient lines; | |
// using the default values the picture will be black at the top and white at the bottom; | |
// with the same values but changing this one will render a picture white on the right side and black on its left side | |
$vertical = false; | |
if (isset($_GET['vertical'])) | |
{ | |
$vertical = true; | |
} | |
gradient($width, $height, $from_r, $from_g, $from_b, $from_a, $to_r, $to_g, $to_b, $to_a, $vertical); | |
function gradient($image_width, $image_height,$c1_r, $c1_g, $c1_b, $c1_a, $c2_r, $c2_g, $c2_b, $c2_a, $vertical=false) | |
{ | |
// make sure that the parameters are what we need them to be | |
$image_width = intval($image_width); | |
$image_height = intval($image_height); | |
$c1_r = intval($c1_r); | |
$c1_g = intval($c1_g); | |
$c1_b = intval($c1_b); | |
$c1_a = intval($c1_a); | |
$c2_r = intval($c2_r); | |
$c2_g = intval($c2_g); | |
$c2_b = intval($c2_b); | |
$c2_a = intval($c2_a); | |
$vertical = (bool)$vertical; | |
// create an image; this will be our output | |
$image = imagecreatetruecolor($image_width, $image_height); | |
// render gradient step by step | |
for($i=0; $i<$image_height; $i++) | |
{ | |
// get each color component for this step | |
$color_r = floor($i * ($c2_r-$c1_r) / $image_height)+$c1_r; | |
$color_g = floor($i * ($c2_g-$c1_g) / $image_height)+$c1_g; | |
$color_b = floor($i * ($c2_b-$c1_b) / $image_height)+$c1_b; | |
$color_a = floor($i * ($c2_a-$c1_a) / $image_height)+$c1_a; | |
// create this color | |
$color = imagecolorallocatealpha($image, $color_r, $color_g, $color_b, $color_a); | |
// draw a line using this color | |
imageline($image, 0, $i, $image_width, $i, $color); | |
} | |
if($vertical) // rotate the image | |
{ | |
// php.net/imagerotate: "Note: This function is only available if PHP is compiled with the bundled version of the GD library." | |
// so if this function doesn't work you must find another way to rotate the image; | |
// one option is to use the function that I found googling and I added to the end of this file called rotateImage | |
$image = imagerotate($image, 90, 0); | |
} | |
// we will output the image now.. if you want you can save the picture insead using the second parameter for the imagepng function | |
header('Content-type: image/png'); // set headers | |
imagepng($image); // or imagejpeg, imagegif.. | |
imagedestroy($image); // free memory | |
die; | |
} | |
function rotateImage($img, $rotation) | |
{ | |
$width = imagesx($img); | |
$height = imagesy($img); | |
switch($rotation) | |
{ | |
case 90: | |
$newimg= @imagecreatetruecolor($height , $width ); | |
break; | |
case 180: | |
$newimg= @imagecreatetruecolor($width , $height ); | |
break; | |
case 270: | |
$newimg= @imagecreatetruecolor($height , $width ); | |
break; | |
case 0: | |
return $img; | |
break; | |
case 360: | |
return $img; | |
break; | |
} | |
if($newimg) | |
{ | |
for($i = 0;$i < $width ; $i++) | |
{ | |
for($j = 0;$j < $height ; $j++) | |
{ | |
$reference = imagecolorat($img,$i,$j); | |
switch($rotation) | |
{ | |
case 90: if(!@imagesetpixel($newimg, ($height - 1) - $j, $i, $reference )){return false;}break; | |
case 180: | |
if(!@imagesetpixel($newimg, $width - $i-1, ($height - 1) - $j, $reference )) | |
{ | |
return false; | |
} | |
break; | |
case 270: if(!@imagesetpixel($newimg, $j, $width - $i, $reference )){return false;}break; | |
} | |
} | |
} | |
return $newimg; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment