I've had this code working for some time that i pulled from somewhere on the net, probably here. It converts Hex Code to RGB to make a gradient on a GD image. I upgraded my server to PHP 8.1 from 7.4 and not it never produces and image and will just infinitely load with thousands or more "Depricated" Float errors like:
Deprecated: Implicit conversion from float 45.99999999999999 to int loses precision
Original Hex2RGB code:
// Functions
function gradient($w=100, $h=100, $c=array('#FFFFFF','#FF0000','#00FF00','#0000FF'), $hex=true) {
$blockbg = 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($blockbg,$rgb[0],$rgb[1],$rgb[2]);
imagesetpixel($blockbg,$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 $blockbg;
}
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);
}
////////////////////
//*----- Hex to RGB for Number and Font Color conversion -----*//
function hex2rgb2($hex2) {
$hex2 = str_replace('#', '', $hex2);
if(strlen($hex2) == 3) {
$r = hexdec(substr($hex2,0,1).substr($hex2,0,1));
$g = hexdec(substr($hex2,1,1).substr($hex2,1,1));
$b = hexdec(substr($hex2,2,1).substr($hex2,2,1));
} else {
$r = hexdec(substr($hex2,0,2));
$g = hexdec(substr($hex2,2,2));
$b = hexdec(substr($hex2,4,2));
}
return array($r, $g, $b); // RETURN ARRAY INSTEAD OF STRING
}
Any help is appreciated. I had a whole image system and now it's completely broken.
I keep getting the error specially on this line. I already know float is deprecated, which is why I'm here and I can't seem to find the place to put a 'round' or 'intval'.
$col=imagecolorallocate($blockbg,$rgb[0],$rgb[1],$rgb[2]);
