PHP Image Creation on RaspberryPi

PHP can harness the power of the GD Graphic Library, allowing the generation and manipulation of images.

You can create images based upon simple lines, rectangles, arcs and circles, as well an manipulating existing graphics files.

So I recently took my first steps in creating images in PHP on a RaspberryPi.

With PHP 5 installed on the RaspberryPi as described in this previous post, I needed to install the GD Graphics LIbrary:-

sudo apt-get install php5-gd

…and then restart the lighttpd service:-

sudo service lighttpd force-reload

In the RaspberryPi web server folder /var/www I created a new file called drawtest.php and wrote a few lines of code:-

<?php
$Image = imagecreate( 500, 500 );
#1st colour allocated is used for the image background
$backColour = imagecolorallocate( $Image, 250, 250, 90 );
#line colours
$colRed = imagecolorallocate( $Image, 250, 0, 0 );
$colBlue = imagecolorallocate( $Image, 0, 0, 250 );
#draw a cross
imageline( $Image, 10, 10, 490, 490, $colRed );
imageline( $Image, 490, 10, 10, 490, $colBlue );
header( “Content-type: image/png” );
imagepng( $Image );
imagedestroy( $Image );
?>

PHP variables have a leading “$” and comments are preceded by “#”.
PHP Image Creation on RaspberryPi
The first line of code uses the method “imagecreate” to create a blank 500×500 image.

I don't understand why imagecreate does not have a 3rd argument for image colour, instead of just accepting the very next colour declaration (imagecolorallocate) to set the image background colour.

The arguments for “imagecolorallocate” specify the red-green-blue values for the new image: $Image.

I also created two additional colours (red & blue) which I use to plot lines using the “imageline” method. This method has four arguments to specify the start and end points for the line in terms of x1, y1, x2, y2.

The top left corner of our image is x=0, y=0, and the bottom right is x=500, y=500.

The method “imagepng” outputs the newly created image in PNG file format, while “imagedestroy” releases the memory previously used by $Image.

So when I point a web browser to this file (e.g. http://192.168.0.31:9080/drawtest.php) the code just generates a yellow box with a red and blue cross:

Let's plot a graph

I thought I'd try to create a line graph using imageline. My primitive code looks like this:-

<?php
$xPoint=50;
$Image = imagecreate( 500, 500 );
#1st colour allocated is used for the image background
$backColour = imagecolorallocate( $Image, 250, 250, 90 );
#line colours
$colRed = imagecolorallocate( $Image, 250, 0, 0 );
$colBlue = imagecolorallocate( $Image, 0, 0, 250 );
#draw x & y axis
imageline( $Image, $xPoint, 470, 470, 470, $colBlue );
imageline( $Image, $xPoint, 470, $xPoint, 30, $colBlue );
imagestring( $Image, 3, 250, 475, “samples”, $colBlue);
imagestring( $Image, 3, 10, 30, “30'C”, $colBlue);
imagestring( $Image, 3, 10, 245, “20'C”, $colBlue);
imagestring( $Image, 3, 10, 460, “10'C”, $colBlue);
$y1Point=400;
$xinterval=8;
for ( $i = 1; $i <= 50; $i++ ) {
$xPoint=$xPoint + $xinterval;
#create dummy data
$y2Point=rand(100,400);
imageline( $Image, $xPoint, $y1Point, $xPoint + $xinterval, $y2Point, $colRed );
$y1Point=$y2Point;
}
header( “Content-type: image/png” );
imagepng( $Image );
imagedestroy( $Image );
?>

For this example, random data is created using the rand() function, with the range of values restricted to 100 – 400. The idea is to plot the first line between data point 1 and 2. The second line is plotted between data point 2 and 3, third line between points 3 and 4, and so on.

 

For more detail: PHP Image Creation on RaspberryPi


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top