note 93920 added to function.imagefill
| From: | ChristopherKramer at osu1 dot php dot net | Date: | Tue, 06 Oct 2009 22:00:50 +0000 |
| Subject: | note 93920 added to function.imagefill | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-161417@lists.php.net to get a copy of this message | ||
Here is a function to generate a gradient.
You specify width, heigth and 4 colors (of the 4 corners).
An image handle of the gradient image will be returned.
You can copy the returned image onto another image using imageecopy.
This can be helpful if you want to generate a shadow for example.
("Glow effect example": Generate 8 gradients, one for each side and one for each corner.
The outer side of the gradients have the background color and the inner sides have a bright color
such as white.)
But beware: This function is not optimized for performance, it might become slow on big images. For
shadows, better cache the generated gradients.
Note: For gradients, using true-color is highly recommended.
<?php
function gradient($w=100, $h=100,
$c=array('#FFFFFF','#FF0000','#00FF00','#0000FF'),
$hex=true) {
/*
Generates a gradient image
Author: Christopher Kramer
Parameters:
w: width in px
h: height in px
c: color-array with 4 elements:
$c[0]: top left color
$c[1]: top right color
$c[2]: bottom left color
$c[3]: bottom right color
if $hex is true (default), colors are hex-strings like '#FFFFFF' (NOT '#FFF')
if $hex is false, a color is an array of 3 elements which are the rgb-values, e.g.:
$c[0]=array(0,255,255);
*/
$im=imagecreatetruecolor($w,$h);
if($hex) { // convert hex-values to rgb
for($i=0;$i<=3;$i++) {
$c[$i]=hex2rgb($c[$i]);
}
}
$rgb=$c[0]; // start with top left color
for($x=0;$x<=$w;$x++) { // loop columns
for($y=0;$y<=$h;$y++) { // loop rows
// set pixel color
$col=imagecolorallocate($im,$rgb[0],$rgb[1],$rgb[2]);
imagesetpixel($im,$x-1,$y-1,$col);
// calculate new color
for($i=0;$i<=2;$i++) {
$rgb[$i]=
$c[0][$i]*(($w-$x)*($h-$y)/($w*$h)) +
$c[1][$i]*($x *($h-$y)/($w*$h)) +
$c[2][$i]*(($w-$x)*$y /($w*$h)) +
$c[3][$i]*($x *$y /($w*$h));
}
}
}
return $im;
}
function hex2rgb($hex)
{
$rgb[0]=hexdec(substr($hex,1,2));
$rgb[1]=hexdec(substr($hex,3,2));
$rgb[2]=hexdec(substr($hex,5,2));
return($rgb);
}
// usage example
$image=gradient(300, 300, array('#000000', '#FFFFFF', '#FF0000',
'#0000FF'));
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
----
Server IP: 85.214.154.209
Probable Submitter: 77.23.19.136
----
Manual Page -- http://www.php.net/manual/en/function.imagefill.php
Edit -- https://master.php.net/note/edit/93920
Del: integrated -- https://master.php.net/note/delete/93920/integrated
Del: useless -- https://master.php.net/note/delete/93920/useless
Del: bad code -- https://master.php.net/note/delete/93920/bad+code
Del: spam -- https://master.php.net/note/delete/93920/spam
Del: non-english -- https://master.php.net/note/delete/93920/non-english
Del: in docs -- https://master.php.net/note/delete/93920/in+docs
Del: other reasons-- https://master.php.net/note/delete/93920
Reject -- https://master.php.net/note/reject/93920
Search -- https://master.php.net/manage/user-notes.php