Page 207 - Open Soource Technologies 304.indd
P. 207

Open Source Technologies



                   Notes         The basic function used to create a new image is called ImageCreate(). This function creates the
                                 canvas area for your new image. For example to create an image that is 300px wide and 300px
                                 high you would use following code:
                                    1.  $imageOne = ImageCreate(300, 300);

                                 Now that you have canvis you need to define colours you want to use in it. Colours are defined
                                 using RGB color system. Using decimal values from 0 to 255 for each of the red (R), green (G), and
                                 blue(B) you can define a specific color. The function used to define colors is ImageColorAllocate().
                                 The first color you allocate is used as the background color of the image.

                                    1.  $white =  ImageColorAllocate($imageOne, 255, 255, 255);
                                    2.  $red = ImageColorAllocate($imageOne, 255, 0, 0);

                                    3.  $blue = ImageColorAllocate($imageOne, 0, 0, 255);
                                    4.  $green = ImageColorAllocate($imageOne, 0, 255, 0);

                                 12.3.1 Drawing Lines and Shapes
                                 There are several PHP functions to assist you in drawing lines and shapes. And as you can see
                                 below the function names are very descriptive.
                                    •  ImageEllipse() — To draw an ellipse.
                                    •  ImageArc() — To draw arc (partial ellipse).

                                    •  ImagePolygon() — To draw a polygon.
                                    •  ImageRectangle() — To draw a rectangle.

                                    •  ImageLine() — To draw a line.
                                 Each of these functions use x-axis and y-axis coordinates as indicators of where to start and stop
                                 the drawing on the canvas. Here is sample of green rectangle that is 30px wide, 50px high and
                                 10px from the left edge and 20px away from the top of the canvas.

                                    1.  Image Rectangle ($image One, 10, 20, 40, 70, $green);





















                                 As you can see drawing with PHP requires some planning ahead.
                                 12.3.2 Using a Colour Fill

                                 PHP can also fill the shapes with solid colour. Functions to do that are:
                                    •  ImageFilledEllipse() — To fill an ellipse.


        202                               LOVELY PROFESSIONAL UNIVERSITY
   202   203   204   205   206   207   208   209   210   211   212