Draw Shapes in PHP

Introduction

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.
To define an image color in GD, you need to use the "ImageCreatTrueColor()" function. All shapes are shown in a browser as a PNG file.

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);

// set green color

$green = ImageColorAllocate($imagecolor, 0, 255, 0);

ImageFillToBorder($imagecolor, 0, 0, $green, $green);

ImagePNG($imagecolor);

//destroy the memory space

ImageDestroy($imagecolor);

?>

Output

shape1.jpg

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 the red colour

$red = ImageColorAllocate($imagecolor, 255, 0, 0);

//create a line

ImageLine($imagecolor, 10, 10, 250, 300, $red);

ImageDashedLine($imagecolor, 30, 10, 280, 300, $red);

ImagePNG($imagecolor);

//destroy memory space

ImageDestroy($imagecolor);

?>

Output

shape2.jpg

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);

// sets black and red colour

$black = ImageColorAllocate($imagecolor, 0, 0, 0);

$red = ImageColorAllocate($imagecolor, 255, 0, 0);

// sets the dimensions of the rectangle

$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);

//destoy the memory space

ImageDestroy($imagecolor);

?>

Output

shape3.jpg

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);

// sets background color white

$automatic = ImageColorAllocate($imagecolor, 255, 255, 255);

ImageFillToBorder($imagecolor, 0, 0, $automatic, $automatic);

// sets the black and red colour

$black = ImageColorAllocate($imagecolor, 0, 0, 0);

$red = ImageColorAllocate($imagecolor, 255, 0, 0);

// create empty circle

ImageEllipse($imagecolor, 180, 100, 100, 100, $black);

ImageFilledEllipse($imagecolor, 180, 220, 150, 50, $red);

ImagePNG($imagecolor);

// destroy the memory spaces

ImageDestroy($imagecolor);

?>

Output

shape4.jpg


Similar Articles