I found that the following function can draw a vertical gradient in PHP. However, many web designers, like their gradients, have an upper left light source to make the gradient more realistic. So, how do you slightly change the angle on a vertical gradient, turning it into a small gradient? I do not want to overdo it completely, but only a slight movement to the right when it is moving along a vertical gradient.
<?php
function hex2rgb($sColor) {
$sColor = str_replace('#','',$sColor);
$nLen = strlen($sColor) / 3;
$anRGB = array();
$anRGB[]=hexdec(str_repeat(substr($sColor,0,$nLen),2/$nLen));
$anRGB[]=hexdec(str_repeat(substr($sColor,$nLen,$nLen),2/$nLen));
$anRGB[]=hexdec(str_repeat(substr($sColor,2*$nLen,$nLen),2/$nLen));
return $anRGB;
}
$nWidth = 960;
$nHeight = 250;
$sStartColor = '#2b8ae1';
$sEndColor = '#0054a1';
$nStep = 1;
$hImage = imagecreatetruecolor($nWidth,$nHeight);
$nRows = imagesy($hImage);
$nCols = imagesx($hImage);
list($r1,$g1,$b1) = hex2rgb($sStartColor);
list($r2,$g2,$b2) = hex2rgb($sEndColor);
$nOld_r = 0; $nOld_g = 0; $nOld_b = 0;
for ( $i = 0; $i < $nRows; $i=$i+1+$nStep ) {
$r = ( $r2 - $r1 != 0 ) ? intval( $r1 + ( $r2 - $r1 ) * ( $i / $nRows ) ): $r1;
$g = ( $g2 - $g1 != 0 ) ? intval( $g1 + ( $g2 - $g1 ) * ( $i / $nRows ) ): $g1;
$b = ( $b2 - $b1 != 0 ) ? intval( $b1 + ( $b2 - $b1 ) * ( $i / $nRows ) ): $b1;
if ( "$nOld_r,$nOld_g,$nOld_b" != "$r,$g,$b") {
$hFill = imagecolorallocate( $hImage, $r, $g, $b );
}
imagefilledrectangle($hImage, 0, $i, $nCols, $i+$nStep, $hFill);
$nOld_r= $r;
$nOld_g= $g;
$nOld_b= $b;
}
header("Content-type: image/png");
imagepng($hImage);
source
share