note 118993 added to function.imagecolorallocate

From: Date: Mon, 14 Mar 2016 09:30:53 +0000
Subject: note 118993 added to function.imagecolorallocate
Groups: php.notes 
Request: Send a blank email to php-notes+get-205154@lists.php.net to get a copy of this message
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; } ---- Server IP: 72.52.91.14 Probable Submitter: 85.164.165.184 ---- Manual Page -- http://php.net/manual/en/function.imagecolorallocate.php Edit -- https://master.php.net/note/edit/118993 Del: integrated -- https://master.php.net/note/delete/118993/integrated Del: useless -- https://master.php.net/note/delete/118993/useless Del: bad code -- https://master.php.net/note/delete/118993/bad+code Del: spam -- https://master.php.net/note/delete/118993/spam Del: non-english -- https://master.php.net/note/delete/118993/non-english Del: in docs -- https://master.php.net/note/delete/118993/in+docs Del: other reasons-- https://master.php.net/note/delete/118993 Reject -- https://master.php.net/note/reject/118993 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#205154) next »