Picture of thermometer

As a first, download the web page, where is the measured temperature. Good example is the intelligent temperature sensor on the address http://senzor-12110-2.fsid.cvut.cz/ - it is important to visit the page to understand the following text.

We can read the page to one large string:

$temp = file_get_contents("http://senzor-12110-2.fsid.cvut.cz/index.htm");

Now, we should look for the "Temperature1=" text (see source code of this page); if we erase all text up to the temperature 1 value, we can use the rest as a text representation of the numerical variable:

$temp = strstr($temp, "Temperature1=");

The result still contains the "Temperature1=" text (on its start). Because we know the length of this, we can read the following temperature by:

$temp = substr($temp, 13, 6);

We copy just six characters, in most cases only the value of the temperature; using standard conversion, we can use it as a numerical variable (in real, this is still text). If there can be leading-in spaces, we can erase them -for example- by using the while cycle.

At this point it is important to test, what is the result of this function, and that this works. We can -for example- add some value and try to use it in an echo:

echo $temp."<br>";
$temp = 1.1 + $temp;
echo $temp."<br>";

If we have value, we can start to generate a picture (in the picture, there cannot be any debugging "echo" command). The method is the same, as with the bears; we use an appropriate picture to initialize our image. I have prepared the following two pictures; the third one has been created not from a photo, but using the Excel and the PrtScr key; this method can be used by anybody without a camera:

   

You have to upload the selected picture to your directory.

We will add a picture of the red ethanol column to the image; the second image is little corrected by erasing the original red column, so the white one part (above the column) is not needed (to be drawn); this is important to make the picture more realistic.

For drawing of the rectangle, we need the dimensions; the lower most position, the ratio and the zero position have to be read using some graphics editor; for example, for these pictures, the zero position is 247 for the first, 223 for the second and 482 for the third one (the last one is displayed in reduced size).

For the ratio, we need the value of "50" Centigrades - for these pictures, this is 88, 64, or 0 points from the top, respectively. The last value is the top of the bulb coordinate - this is 361, 337 or 676 (for these pictures). The left and right coordinates of the proposed red bar (x1, x2) will be 68 and 72, or 39 and 44, or 116-4 and 116+4 (= 112 and 120), respectively. For the left two pictures, I have detected the color as R,G,B=220,10,20.

The ratio has to be calculated from the length of the 50 degrees - for example, if 0°C is represented by 223 points and 50°C by 64 points, the distance is 223-64=159 points; temperature from the sensor should be divided by 50 and then multiplied by this distance; for drawing, we should use the "round" function, to draw the integer number of pixels.

Because of the y coordinate is negative (zero in the top left corner), we have to draw this as negative; typical solution is to subtract the value from the value of the zero position of the scale; the second end of the ethanol (red) column is constant; the whole solution follows:

<?php
header('Content-Type: image/jpeg');
// Create a new picture from the selected image of thermometer:
$im @imagecreatefromjpeg('teplomer1.jpg');

// Color preparation:
$white imagecolorallocate($im0xFF0xFF0xFF);
$black imagecolorallocate($im2201020);

// Preparing of the white background - can be skipped
imagefilledrectangle($im396444337$white);

// Obtaining of the data
$temp = file_get_contents("http://senzor-12110-2.fsid.cvut.cz/index.htm");
// The temperature value extraction
$temp = strstr($temp, "Temperature1=");
$temp = trim( substr($temp, 14, 6) );

// The upper coordinate of the ethanol (red) column evaluation
$y1 = 223 - round( $temp/50 *(223-64) );

// Drawing of the red bar
imagefilledrectangle($im39$y144337$black);

// The picture generation - without name, so direct to the browser:

imagejpeg($im);

// The used memory restore
imagedestroy($im);
?> 

This picture is used on http://www.fsid.cvut.cz/cz/u12110/dzs/ (on the bottom end of the page).