SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict amitjoshi's Avatar
    Join Date
    Jul 2005
    Location
    Oklahoma, USA
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP GD question:imagecolorallocate function

    I tried to allocate many colors using following code.

    PHP Code:
    $im imagecreate(150100)
        or die(
    "Cannot Initialize new GD image stream");
    $background_color imagecolorallocate($im00255);
    $text_color imagecolorallocate($im2331491);

    for (
    $i=0;$i<150;$i++)
    {
        for (
    $j=0;$j<100;$j++)
        {
            
    $r rand(0,255);
            
    //$array[$i][$j] =imagecolorallocate($im, 255, 255,0); 
            
    $g rand(0,255);
            
    $b rand(0,255);
            
    $color imagecolorallocate($im$r,$g,$b);        
            echo 
    $color." "
            
    imagesetpixel($im,$i,$j,$color);        
        }
        echo 
    "<br>";

    But after the 225 th imagecollorallocate, all it returns is -1. Which probably means that the color is not being allocated. Why is this?

    Please help.
    Amit

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Not sure on this, but you might want to do an echo out of $r, $g, and $b, as well as print_r of ($im) to see what exactly php is seeing.

  3. #3
    SitePoint Addict amitjoshi's Avatar
    Join Date
    Jul 2005
    Location
    Oklahoma, USA
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $r, $g, $b values are correct. i even tried the following code.
    It seems like that gd can only allocate 255 different colors. After 255 th allocation, imagecolorallocate function only returns -1.

    PHP Code:
    <?php
    $im 
    imagecreate(150100)   or die("Cannot Initialize new GD image stream");
    for (
    $i=0;$i<270;$i++)
    {
        
    $color imagecolorallocate($im$i%255,$i%255,$i%255);        
        echo 
    $color." <br>"
    }
    imagedestroy($im);
    ?>
    Amit


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •