Created
July 8, 2009 20:51
-
-
Save stubbetje/143182 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* simpele gradient image adhv GET parameters | |
* | |
* @author Stefan Stubbe | |
*/ | |
// default settings definiëren | |
$defaults = array( | |
'height' => 100 , | |
'width' => 220 , | |
'from' => '8ae234' , | |
'to' => '4e9a06' , | |
); | |
// samenvoegen van default settings met parameters meegegeven in $_GET. | |
// => GET parameters overschrijven dus de default settings | |
$options = array_merge( $defaults , $_GET ); | |
// controleren of GD library beschikbaar is | |
if( ! function_exists( 'imagecreate' ) ) { | |
die_with_error( 'GD library is missing' ); | |
} | |
// valideren van de settings | |
$options = array_map( 'trim' , $options ); | |
$height = (int) $options['height']; | |
if( $height <= 0 || $height > 200 ) { | |
die_with_error( 'Invalid height given, must be between 1 and 200' ); | |
} | |
$width = (int) $options['width']; | |
if( $width <= 0 || $width > 1000 ) { | |
die_with_error( 'Invalid width given, must be between 1 and 1000' ); | |
} | |
if( ! preg_match( '#^[a-z0-9]{6}$#i' , $options['from'] ) ) { | |
die_with_error( 'Invalid start color, must be a hexadecimal RGB color' ); | |
} | |
if( ! preg_match( '#^[a-z0-9]{6}$#i' , $options['to'] ) ) { | |
die_with_error( 'Invalid end color, must be a hexadecimal RGB color' ); | |
} | |
// afbeelding alloceren | |
if( false === ( $gd = imagecreate( $width , $height ) ) ) { | |
die_with_error( 'Could not create image' ); | |
} | |
// twee manieren om RGB op te splitsen in R, G en B | |
if( true ) { | |
// een eerste manier om het hexadecimaal RGB kleur (RRGGBB) op te splitsen in rood, groen en blauw | |
$redStart = hexdec( substr( $options['from'] , 0 , 2 ) ); | |
$greenStart = hexdec( substr( $options['from'] , 2 , 2 ) ); | |
$blueStart = hexdec( substr( $options['from'] , 4 , 2 ) ); | |
$redEnd = hexdec( substr( $options['to'] , 0 , 2 ) ); | |
$greenEnd = hexdec( substr( $options['to'] , 2 , 2 ) ); | |
$blueEnd = hexdec( substr( $options['to'] , 4 , 2 ) ); | |
} else { | |
// de tweede manier: binair | |
$start = hexdec( $options['from'] ); | |
$end = hexdec( $options['to'] ); | |
$redStart = ( $start >> 16 ) & 0xFF; | |
$greenStart = ( $start >> 8 ) & 0xFF; | |
$blueStart = $start & 0xFF; | |
$redEnd = ( $end >> 16 ) & 0xFF; | |
$greenEnd = ( $end >> 8 ) & 0xFF; | |
$blueEnd = $end & 0xFF; | |
} | |
// stappen tussen 'from' kleur en 'to' kleur bepalen | |
$redStep = ( $redEnd - $redStart ) / $height; | |
$greenStep = ( $greenEnd - $greenStart ) / $height; | |
$blueStep = ( $blueEnd - $blueStart ) / $height; | |
// gradient maken | |
$red = $redStart; | |
$green = $greenStart; | |
$blue = $blueStart; | |
for( $y = 0 ; $y < $height ; $y++ ) { | |
// kleur alloceren om te gebruiken in de afbeelding | |
$color = imagecolorallocate( $gd , (int) $red , (int) $green , (int) $blue ); | |
// horizontale lijn tekenen in dit kleur | |
imageline( $gd , 0 , $y , $width , $y , $color ); | |
// kleuren aanpassen | |
$red += $redStep; | |
$green += $greenStep; | |
$blue += $blueStep; | |
} | |
// output van afbeelding | |
header( 'Content-Type: image/png' ); | |
imagepng( $gd ); | |
exit; | |
// ================================================================== | |
function die_with_error( $msg ) | |
{ | |
header( 'HTTP/1.0 500 Server error' ); | |
echo 'error: ' , $msg; | |
exit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment