PHP 8.5.0 Beta 1 available for testing

Voting

: five plus two?
(Example: nine)

The Note You're Voting On

divinity76 at gmail dot com
9 years ago
you have VERY limited space for color indexes (255 indexes on my system, with over 10 GB ram available, cli, no memory limit), when there is no more indexes available, imagecolorallocate will return false. when you create 2x indexes with the same r/g/b, you waste this very limited space. the function below should never fail, AND never waste any color index space. if there's already an index with the rgb, it will return the existing index, else it will try allocate 1, in the event that allocation fail (presumably because all index space is used up already), it will return the closest match to what you requested, and warn you via $couldFindExact.

function myimagecolorallocate($gd,int $red,int $green,int $blue,bool &$couldFindExact=null):int{
$ret=imagecolorexact($gd, $red, $green, $blue);
if($ret===-1){
$ret=imagecolorallocate($gd, $red, $green, $blue);
if($ret===false){
$couldFindExact=false;//out of color indexes (255 index by default..wish i knew why)
$ret=imagecolorclosest($gd, $red, $green, $blue);
} else {
$couldFindExact=true;
}
} else {
$couldFindExact=true;
}
return $ret;
}

<< Back to user notes page

To Top