Draw geometric shapes in php
Introduction Show In this article I explain how to draw basic shapes in PHP. PHP shapes are a very simple. You can easily draw in your application, but before creating a shape program you should install the GD library. The PHP GD library is very helpful for creating an image program in PHP, therefore you need to include the GD library in your PHP application. Let's begin a basic shapes drawing
program using the PHP GD library. In this article I will use some image functions. Example In this example I used a green background color and output this graphics to the browser as a .PNG file. php header("Content-type: image/png"); $image_width= 200; $image_height=200; $imagecolor =ImageCreateTrueColor($image_width, $image_height); // setgreen color $green=ImageColorAllocate($imagecolor, 0, 255, 0); ImageFillToBorder($imagecolor, 0, 0, $green, $green); ImagePNG($imagecolor); //destroythememoryspace ImageDestroy($imagecolor); ?> Output Example Draw a Simple Line Shape php header("Content-type: image/png"); $image_width =350; $image_height=360; $imagecolor =ImageCreateTrueColor($image_width, $image_height); ImageAntiAlias($imagecolor, true); $automatic=ImageColorAllocate($imagecolor, 255, 255, 255); ImageFillToBorder($imagecolor, 0, 0, $automatic, $automatic); // set theredcolour $red=ImageColorAllocate($imagecolor, 255, 0, 0); //createaline ImageLine($imagecolor, 10, 10, 250, 300, $red); ImageDashedLine($imagecolor, 30, 10, 280, 300, $red); ImagePNG($imagecolor); //destroymemoryspace ImageDestroy($imagecolor); ?> Output Example Draw a Simple Rectangle Shape php header("Content-type: image/png"); $image_width= 350; $image_height=360; $imagecolor= ImageCreateTrueColor($image_width, $image_height); ImageAntiAlias($imagecolor, true); $automatic=ImageColorAllocate($imagecolor, 255, 255, 255); ImageFillToBorder($imagecolor, 0, 0, $automatic, $automatic); // setsblackand redcolour $black=ImageColorAllocate($imagecolor, 0, 0, 0); $red=ImageColorAllocate($imagecolor, 255, 0, 0); // setsthe dimensionsoftherectangle $rectangle_width= 150; $rectangle_height=100; $r_x= 100; $r_y=50; ImageRectangle($imagecolor, $r_x, $r_y, $r_x+$rectangle_width, $r_y+$rectangle_height, $black); $rectangle_width=150; $rectangle_height =100; $r_x=100; $r_y =200; ImageFilledRectangle($imagecolor, $r_x, $r_y, $r_x+$rectangle_width, $r_y+$rectangle_height, $red); ImagePNG($imagecolor); //destoythememoryspace ImageDestroy($imagecolor); ?> Output Example Draw a Simple Circle Shape php header("Content-type: image/png"); $image_width=350; $image_height=360; $imagecolor= ImageCreateTrueColor($image_width, $image_height); ImageAntiAlias($imagecolor, true); // setsbackgroundcolorwhite $automatic= ImageColorAllocate($imagecolor, 255, 255, 255); ImageFillToBorder($imagecolor, 0, 0, $automatic, $automatic); // setstheblackandredcolour $black=ImageColorAllocate($imagecolor, 0, 0, 0); $red= ImageColorAllocate($imagecolor, 255, 0, 0); // createemptycircle ImageEllipse($imagecolor, 180, 100, 100, 100, $black); ImageFilledEllipse($imagecolor, 180, 220, 150, 50, $red); ImagePNG($imagecolor); // destroythe memoryspaces ImageDestroy($imagecolor); ?> Output How to draw rectangles in PHP?PHP | imagefilledrectangle() Function
The imagefilledrectangle() function is an inbuilt function in PHP which is used to create a filled rectangle. This function creates a rectangle filled with a given color in the image. The top left corner of the image is (0, 0).
How to draw Lines in PHP?Normally in PHP you would use the imageline() function to draw a line between two points. The following block of code creates and image and draws a white line from the coordinates 50x,50y to 200x,150y.
...
Drawling A Line With Pixels In PHP.. Can we draw images using PHP?PHP provides many functions to draw lines, rectangles, polygons, arcs, ellipses and much more. The GD library is utilized for dynamic picture creation. In PHP, we can easily use the GD library to make GIF, PNG, or JPG pictures quickly from our code.
How to make graphics in PHP?The imagecreate() function is an inbuilt function in PHP which is used to create a new image. This function returns the blank image of given size. In general imagecreatetruecolor() function is used instead of imagecreate() function because imagecreatetruecolor() function creates high quality images.
|