multiple polygons on one image

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David

    multiple polygons on one image

    Is it possible to create more than one polygon (using the imagepolygon
    function) and then paste them on to one image? i have no idea how to do
    this. I did post this a few days ago but my computer seems to have lost the
    reply,

    thanks Dave


  • Dan Tripp

    #2
    Re: multiple polygons on one image

    David wrote:
    [color=blue]
    > Is it possible to create more than one polygon (using the imagepolygon
    > function) and then paste them on to one image? i have no idea how to do
    > this. I did post this a few days ago but my computer seems to have lost the
    > reply,
    >
    > thanks Dave[/color]


    The original response you got (from Pedro Graca) was:
    [color=blue]
    > What have you tried?
    > What happenned? What did you want to happen?[/color]

    After taking a look at the sample code from PHP.net I can see how
    somebody new-ish to PHP might be a bit confused. (An array is defined
    within a function call, etc... not the world's clearest example, IMHO.)

    Here's a hopefully clear(er) version. Also, note that I intentionally
    chose long, descriptive variable names to hopefully help clarify what's
    going on in the script. You probably won't want to use
    reallySuperStin kingLongVariabl eNames in your scripts. ;)

    - Dan




    =============== =============== =============== ==============
    Modified version of the sample at:

    =============== =============== =============== ==============

    <?php

    // create a blank image
    $image = imagecreate(400 , 300);

    // fill the background color
    $bg = imagecoloralloc ate($image, 0, 0, 0);

    // choose a color for the polygon
    $polygonOneColo r = imagecoloralloc ate($image, 255, 255, 255);
    $polygonTwoColo r = imagecoloralloc ate($image, 160, 160, 160);

    // Put the points for each polygon into an array, for the sake of
    // showing simpler code than declaring an array within a
    // function. (like the sample above)
    // Also, I padded the values below with spaces, just to make these
    // easier for human eyes to read. (aka - it's not necessary)
    $polygonOnePoin ts = array( 0, 0, 200,200, 200,300);
    $polygonTwoPoin ts = array(300,270, 250,225, 375,275);

    // A smarter way to determine the number of points would be to count the
    // # of items in the polygon's Points array and divide by 2 (left as an
    // exercise for the reader).
    $polygonOneNumb erOfPoints = 3;
    $polygonTwoNumb erOfPoints = 3;

    // draw the first polygon
    imagepolygon($i mage,$polygonOn ePoints,$polygo nOneNumberOfPoi nts,$polygonOne Color);

    // draw the second polygon
    imagepolygon($i mage,$polygonTw oPoints,$polygo nTwoNumberOfPoi nts,$polygonTwo Color);

    // output the picture
    header("Content-type: image/png");
    imagepng($image );

    ?>

    Comment

    • David

      #3
      Re: multiple polygons on one image

      Thanks that really helped, is it that obvious im a newbie :) ?

      Dave

      ps Id managed to work this one out though! But thanks
      [color=blue]
      > // A smarter way to determine the number of points would be to count the
      > // # of items in the polygon's Points array and divide by 2 (left as an
      > // exercise for the reader).[/color]



      "



      Comment

      • Dan Tripp

        #4
        Re: multiple polygons on one image

        David wrote:[color=blue]
        > Thanks that really helped, is it that obvious im a newbie :) ?
        >
        > Dave
        >
        > ps Id managed to work this one out though! But thanks
        >[color=green]
        >>// A smarter way to determine the number of points would be to count the
        >>// # of items in the polygon's Points array and divide by 2 (left as an
        >>// exercise for the reader).[/color]
        >[/color]

        I think it's kinda dopey that the function asks for it (number of
        points)... one would think that if it got three sets of x,y coords that
        it'd know "I've got three points!" instead of one having to specify it.
        There's some aspect of that implementation I just don't get. =)

        Frankly, I thought the question was odd until I looked at the sample on
        php.net. Then I thought "that's not such a good example" and did my
        best to write a better one. Glad it helped.

        I didn't mean "new-ish" in a derogatory manner. Everybody starts
        somewhere. I learn something every day. =)

        Regards,

        - Dan

        Comment

        Working...