Archive for the ‘PHP Scripts’ category

Dynamic thermometer-image

September 18th, 2009

If you want to create a dynamic thermometer for your webpage PHP has some capability to create and modify images.

The script below can be called with http://domotica.ronnkvist.nu/temperature-scripts/termometer.php?temp=22 to create an image like this:
22

<?php
	// Should be in even 5's (20, 25, 30 and so on)
	$maxTemp = 30;
	$minTemp = -30;

	// Where should the bar be placed?
	$barStart = 8;
	$maxHeight = 172;
	$totalBarHeight = 164;

	// Colors and what background to use
    $tmpImg = @imagecreatefrompng("termometer.png");
	$colBar = imagecolorallocate($tmpImg, 208, 33, 33);
	$colBlack = imagecolorallocate($tmpImg, 0, 0, 0);

	// Temperature-lines
	imageline($tmpImg, 1, $barStart+(($totalBarHeight/6) * 0), 22, $barStart+(($totalBarHeight/6) * 0), $colBlack);
	imageline($tmpImg, 1, $barStart+(($totalBarHeight/6) * 1), 22, $barStart+(($totalBarHeight/6) * 1), $colBlack);
	imageline($tmpImg, 1, $barStart+(($totalBarHeight/6) * 2), 22, $barStart+(($totalBarHeight/6) * 2), $colBlack);
	imageline($tmpImg, 1, $barStart+(($totalBarHeight/6) * 3), 22, $barStart+(($totalBarHeight/6) * 3), $colBlack);
	imageline($tmpImg, 1, $barStart+(($totalBarHeight/6) * 4), 22, $barStart+(($totalBarHeight/6) * 4), $colBlack);
	imageline($tmpImg, 1, $barStart+(($totalBarHeight/6) * 5), 22, $barStart+(($totalBarHeight/6) * 5), $colBlack);
	imageline($tmpImg, 1, $barStart+(($totalBarHeight/6) * 6), 22, $barStart+(($totalBarHeight/6) * 6), $colBlack);

	// Text-strings by the lines
	imagestring($tmpImg, 1, 25, $barStart+(($totalBarHeight/6) * 0)-4, " 30", $colBlack);
	imagestring($tmpImg, 1, 25, $barStart+(($totalBarHeight/6) * 1)-4, " 20", $colBlack);
	imagestring($tmpImg, 1, 25, $barStart+(($totalBarHeight/6) * 2)-4, " 10", $colBlack);
	imagestring($tmpImg, 1, 25, $barStart+(($totalBarHeight/6) * 3)-4, "  0", $colBlack);
	imagestring($tmpImg, 1, 25, $barStart+(($totalBarHeight/6) * 4)-4, "-10", $colBlack);
	imagestring($tmpImg, 1, 25, $barStart+(($totalBarHeight/6) * 5)-4, "-20", $colBlack);
	imagestring($tmpImg, 1, 25, $barStart+(($totalBarHeight/6) * 6)-4, "-30", $colBlack);

	$temperature = trim($_GET['temp']);
	if ($temperature == "") {
		$temperature = 0;
	}

	// Calculate the bar-height
	$tempSpan = (0 - $minTemp) + $maxTemp;
	if ($temperature == 0) {
		$barHeight = ($totalBarHeight / 2);
	} elseif ($temperature > 0) {
		if ($temperature >= $maxTemp) {
			$barHeight = $totalBarHeight;
		} else {
			$barHeight = ($totalBarHeight / 2) + (($temperature / $maxTemp) * ($totalBarHeight / 2));
		}
	} else {
		if ($temperature <= $minTemp) {
			$barHeight = 0;
		} else {
			$barHeight = ($totalBarHeight / 2) - (($totalBarHeight / 2) * ((0-$temperature) / (0-$minTemp)));
		}
	}

	// Temperature-bar
	imagefilledrectangle($tmpImg, 7, $barStart+($totalBarHeight-$barHeight), 13, 172, $colBar);

	// Create the image
	header( "Content-type: image/png" );
	imagepng($tmpImg );
?>

The background image is found at http://domotica.ronnkvist.nu/temperature-scripts/termometer.png