1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Filled polygone is not rendered 129th time on same image

Discussion in 'PHP' started by Swan, Mar 20, 2008.

  1. #1
    I have a function which creates a polygon of blue color. I call this function inside a loop of 128 iterations. For every iteration, i change the value of x and y so that my polygon is rendered on new location. This goes fine and i get 128 new polygons on my image after running the script. But if change the number to 129, the 129th polygon is transparent or what i cannot guess. It just does not display or render at all. Is this a bug or some mistake with my code?
    SEMrush
    I am using php version 5.1.4 that came with WAMP5 Version 1.6.3

    my code is -
    <?php
    $im = imagecreate(600, 400);
    $x = 20;
    $y = 350;
    for($i=1;$i<=128;$i++) //create a polygon 128 times
    {
    drawPoly($x,$y,$im);
    $x = $x+2;
    $y = $y-2;
    }
    header('Content-type: image/png');
    imagepng($im);
    imagedestroy($im);
    function drawPoly($xCoord,$yCoord,$im)
    {
    $x1 = $x2 = $xCoord;
    $x3 = $x4 = $xCoord + 70;
    $y1 = $y4 = $yCoord;
    $y2 = $y3 = $yCoord + 30;
    $Poly = array(0 => $x1,1 => $y1,2 => $x2,3 => $y2,4 => $x3,5 => $y3,6

    => $x4,7 => $y4);
    $bg = imagecolorallocate($im, 150, 150, 150);
    $blue = imagecolorallocate($im, 0, 0, 255);
    imagefilledpolygon($im, $Poly, 4, $blue);
    }
    ?>
     
    Swan, Mar 20, 2008 IP
    SEMrush
  2. Swan

    Swan Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The issue is solved. It was the problem with the code.

    i took this line $bg = imagecolorallocate($im, 150, 150, 150); outside the function drawPoly(), and everything worked as

    expected.

    the new updated code is -

    <?php
    $im = imagecreate(600, 400);
    $bg = imagecolorallocate($im, 150, 150, 150);
    $x = 20;
    $y = 350;
    for($i=1;$i<=150;$i++) //create a polygon 128 times
    {
    drawPoly($x,$y,$im);
    $x = $x+2;
    $y = $y-2;
    }
    header('Content-type: image/png');
    imagepng($im);
    imagedestroy($im);
    function drawPoly($xCoord,$yCoord,$im)
    {
    $x1 = $x2 = $xCoord;
    $x3 = $x4 = $xCoord + 70;
    $y1 = $y4 = $yCoord;
    $y2 = $y3 = $yCoord + 30;
    $Poly = array(0 => $x1,1 => $y1,2 => $x2,3 => $y2,4 => $x3,5 => $y3,6 => $x4,7 => $y4);
    $blue = imagecolorallocate($im, 0, 0, 255);
    imagefilledpolygon($im, $Poly, 4, $blue);
    }
    ?>

    But i still want to know why this happened and why it worked when i took that line outside the function?
     
    Swan, Mar 20, 2008 IP