src.dualinventive.com/mtinfo/dist/webroot/rc-4.05/include/db_icon.php

313 lines
11 KiB
PHP

<?php
/** \file include\db_icon.php
* \brief DI webinterface icon functions
* \author Rob Schalken, Core|Vision
* \version $Revision: 26247 $
* \date $Date: 2016-02-29 10:40:22 +0100 (Mon, 29 Feb 2016) $
*
* This file return the requested data
*/
require_once("utilities.php");
require_once("db.php");
require_once("document.php");
define("ICON_HEIGHT" ,15);
define("ICON_WIDTH" ,15);
define("ICON_OFFSET" , 2);
define("ICON_IDCODE_WIDTH" ,(3 * ICON_WIDTH) + (4 * ICON_OFFSET) + (2 * ICON_OFFSET));
define("ICON_IDCODE_HEIGHT" ,12);
// Retrieve icon info
$icon = strtolower($_GET['icon']);
$output = (isset($_GET['output'])) ? $_GET['output'] : "png";
// Split-up warnings
$warnings = array();
if (isset($_GET['warning'])) {
$warnings = split(",", $_GET['warning']);
}
// Idcode/Realtime remark set?
$idcode = "";
$rtremark = "";
if (isset($_GET['idcode'])) {
// Idcode
if (strlen($_GET['idcode']) > 10) {
$idcode = substr($_GET['idcode'], 0, 10);
}
else {
$idcode = $_GET['idcode'];
}
if (isset($_GET['rtremark'])) {
// Realtime remark
if (strlen($_GET['rtremark']) > 10) {
$rtremark = substr($_GET['rtremark'], 0, 10);
}
else {
$rtremark = $_GET['rtremark'];
}
}
}
// Define total width image (1 warning => fixed width)
$n_warnings = count($warnings);
// only 1 icon for both (ext1/ext2) temperature warnings
if( in_array("temp_ext1", $warnings) && in_array("temp_ext2", $warnings) ) $n_warnings--;
$icon_width_alert = ($n_warnings > 0) ? (($n_warnings * ICON_WIDTH) + (($n_warnings + 1) * ICON_OFFSET)) : 0;
$icon_width_total = (strlen($idcode)) ? ((ICON_IDCODE_WIDTH > $icon_width_alert) ? ICON_IDCODE_WIDTH : $icon_width_alert) : $icon_width_alert;
if ((!isset($_GET['action'])) || (strtolower($_GET['action']) == "data")) {
// Retrieve icon data
$file = db_fetch_data("SELECT icon_" . $icon . ",icon_" . $icon . "_mimetype FROM device WHERE id='" . $_GET['device'] ."'");
// Create image from string
$src = imagecreatefromstring($file[0]["icon_" . $icon]);
// Check offsets src
$src_offsetx = (imagesx($src) < $icon_width_total) ? round(($icon_width_total - imagesx($src))/2) : 0;
$src_offsety = (!empty($warnings)) ? ICON_HEIGHT : 0;
// Create new image (including icons)
$height = imagesy($src);
$height += (!empty($warnings)) ? ICON_HEIGHT : 0;
$height += (strlen($idcode)) ? ICON_IDCODE_HEIGHT : 0;
$height += ((strlen($idcode)) && (strlen($rtremark))) ? ICON_IDCODE_HEIGHT : 0;
$width = (imagesx($src) < $icon_width_total) ? $icon_width_total : imagesx($src);
$dest = imagecreatetruecolor($width, $height);
// Define "white" as transparent color
$transparent = imagecolorallocate($dest, 255, 255, 254);
imagecolortransparent($dest, $transparent);
// Fill complete background
imagefilledrectangle($dest, 0, 0, imagesx($dest), imagesy($dest), $transparent);
// Copy and resample image
imagecopyresampled($dest, $src, $src_offsetx, $src_offsety, 0, 0, imagesx($src), imagesy($src), imagesx($src), imagesy($src));
if (!empty($warnings)) {
// Check icon offset
$icon_offsetx = round((imagesx($dest) - $icon_width_alert)/2) + ICON_OFFSET;
if(
in_array(($img = "gsm_none"), $warnings) ||
in_array(($img = "gsm_unknown"), $warnings) ||
in_array(($img = "gsm0"), $warnings) ||
in_array(($img = "gsm10"), $warnings) ||
in_array(($img = "gsm25"), $warnings) ||
in_array(($img = "gsm50"), $warnings) ||
in_array(($img = "gsm75"), $warnings) ||
in_array(($img = "gsm100"), $warnings)
) {
// gsm icons are 1 pixel wider than the other icons to accomodate the five bars
$img = imagecreatefrompng("../" . IMAGE_DIR . "icon_" . $img . ".png");
imagecopyresampled($dest, $img, $icon_offsetx, 0, 0, 0, ICON_WIDTH + 1, ICON_HEIGHT, imagesx($img), imagesy($img));
imagedestroy($img);
$icon_offsetx += ICON_WIDTH + 1 + ICON_OFFSET;
}
// Display warnings
if (in_array("batt", $warnings)) {
// Create battery warning icon
$batt = imagecreatefrompng("../" . IMAGE_DIR . "icon_batt.png");
imagecopyresampled($dest, $batt, $icon_offsetx, 0, 0, 0, ICON_WIDTH, ICON_HEIGHT, imagesx($batt), imagesy($batt));
imagedestroy($batt);
$icon_offsetx += ICON_WIDTH + ICON_OFFSET;
}
if (in_array("temp", $warnings) || in_array("temp_ext1", $warnings) || in_array("temp_ext2", $warnings)) {
// Create temperature warning icon
$temp = imagecreatefrompng("../" . IMAGE_DIR . "icon_temp.png");
imagecopyresampled($dest, $temp, $icon_offsetx, 0, 0, 0, ICON_WIDTH, ICON_HEIGHT, imagesx($temp), imagesy($temp));
imagedestroy($temp);
$icon_offsetx += ICON_WIDTH + ICON_OFFSET;
}
if (in_array("gps", $warnings)) {
// Create GPS warning icon
$gps = imagecreatefrompng("../" . IMAGE_DIR . "icon_gps.png");
imagecopyresampled($dest, $gps, $icon_offsetx, 0, 0, 0, ICON_WIDTH, ICON_HEIGHT, imagesx($gps), imagesy($gps));
imagedestroy($gps);
$icon_offsetx += ICON_WIDTH + ICON_OFFSET;
}
if (in_array("switch", $warnings)) {
// Create SWITCH warning icon
$switch = imagecreatefrompng("../" . IMAGE_DIR . "icon_switch.png");
imagecopyresampled($dest, $switch, $icon_offsetx, 0, 0, 0, ICON_WIDTH, ICON_HEIGHT, imagesx($switch), imagesy($switch));
imagedestroy($switch);
$icon_offsetx += ICON_WIDTH + ICON_OFFSET;
}
if (in_array("sleepmode", $warnings)) {
// Create sleepmode warning icon
$sleep = imagecreatefrompng("../" . IMAGE_DIR . "icon_sleepmode.png");
imagecopyresampled($dest, $sleep, $icon_offsetx, 0, 0, 0, ICON_WIDTH, ICON_HEIGHT, imagesx($sleep), imagesy($sleep));
imagedestroy($sleep);
$icon_offsetx += ICON_WIDTH + ICON_OFFSET;
}
}
if (strlen($idcode)) {
// Define colors
$white = imagecolorallocate($dest, 255, 255, 255);
$black = imagecolorallocate($dest, 0, 0, 0);
$icon_offsetx = ((imagesx($dest) - ICON_IDCODE_WIDTH) > 0) ? round((imagesx($dest) - ICON_IDCODE_WIDTH)/2) : 0;
// Define height
$height = ICON_IDCODE_HEIGHT;
$height += (strlen($rtremark)) ? ICON_IDCODE_HEIGHT : 0;
// Fill complete background (black) => Border
imagefilledrectangle($dest, $icon_offsetx, imagesy($dest) - $height - 1, $icon_offsetx + ICON_IDCODE_WIDTH - 1, imagesy($dest) - 1, $black);
// Fill complete background (white)
imagefilledrectangle($dest, $icon_offsetx + 1, imagesy($dest) - $height, $icon_offsetx + ICON_IDCODE_WIDTH - 2, imagesy($dest) - 2, $white);
// Draw idcode & Realtime remark
for($i=0; $i<2; $i++) {
$found = FALSE;
$font_size = $_RT_FONTSIZE;
$font = FONT_DIR . "verdana.ttf";
$text = (!$i) ? $idcode : $rtremark;
if (strlen($text)) {
while(!$found) {
// Define text width
$info = imagettfbbox($font_size, 0, $font, $text);
if (($info[2] - $info[0]) <= (ICON_IDCODE_WIDTH - 4)) {
// Calculate width
$width = ($info[2] - $info[0]);
// set flag
$found = TRUE;
}
else {
$text = substr($text, 0, (strlen($text) - 1));
}
}
// Define offsets
$icon_offsetx_box = $icon_offsetx + ((ICON_IDCODE_WIDTH - $width)/2);
$icon_offsety_box = imagesy($dest) - 3;
$icon_offsety_box -= ((!$i) && (strlen($rtremark))) ? ICON_IDCODE_HEIGHT : 0;
// Draw the text
imagettftext($dest, $font_size, 0, $icon_offsetx_box, $icon_offsety_box, $black, $font, $text);
}
}
}
// Destory image
imagedestroy($src);
// Turn on output buffering
ob_start();
// Get output
switch($output)
{
case "gif":
$mimetype = "image/gif";
imagegif($dest);
break;
case "jpg":
$mimetype = "image/jpg";
imagejpeg($dest);
break;
default:
$mimetype = "image/png";
imagepng($dest);
break;
}
// Adjust extension
$icon = RemoveExtension($icon) . "." . $output;
// Copy current image to document
$catched_image = ob_get_clean();
// Download icon
download_document(array(document => $catched_image, mimetype => $mimetype, filename => "icon_" . $icon));
// Destroy image
imagedestroy($dest);
}
else if (strtolower($_GET['action']) == "jssize") {
// Retrieve icon width
$file = db_fetch_data("SELECT icon_" . $icon . "_width,icon_" . $icon . "_height FROM device WHERE id='" . $_GET['device'] ."'");
// Define height/width
$height = $file[0]["icon_" . $icon . "_height"];
$height += (!empty($warnings)) ? ICON_HEIGHT : 0;
$height += (strlen($idcode)) ? ICON_IDCODE_HEIGHT : 0;
$height += ((strlen($idcode)) && (strlen($rtremark))) ? ICON_IDCODE_HEIGHT : 0;
$width = ($file[0]["icon_" . $icon . "_width"] < $icon_width_total) ? $icon_width_total : $file[0]["icon_" . $icon . "_width"];
echo "//iconsize_v1.0\niconsize=new Array(" . $width . "," . $height . ");";
}
else if (strtolower($_GET['action']) == "session") {
require_once("session.php");
if (isset($_GET['output'])) {
$src = imagecreatefromstring($_SESSION[$_PAGE_INFO['id']][$_GET['session']]["icon_" . $icon]);
$dest = imagecreatetruecolor(imagesx($src), imagesy($src));
// Define "white" as transparent color
$transparent = imagecolorallocate($dest, 255, 255, 254);
imagecolortransparent($dest, $transparent);
// Fill complete background
imagefilledrectangle($dest, 0, 0, imagesx($src), imagesy($src), $transparent);
// Copy and resample image
imagecopyresampled($dest, $src, $src_offsetx, $src_offsety, 0, 0, imagesx($src), imagesy($src), imagesx($src), imagesy($src));
// Destory image
imagedestroy($src);
// Turn on output buffering
ob_start();
// Get output
switch($output)
{
case "gif":
$mimetype = $_SESSION[$_PAGE_INFO['id']][$_GET['session']]["icon_" . $icon . "_mimetype"];
imagegif($dest);
break;
case "jpg":
$mimetype = $_SESSION[$_PAGE_INFO['id']][$_GET['session']]["icon_" . $icon . "_mimetype"];
imagejpeg($dest);
break;
default:
$mimetype = $_SESSION[$_PAGE_INFO['id']][$_GET['session']]["icon_" . $icon . "_mimetype"];
imagepng($dest);
break;
}
// Adjust extension
$icon = RemoveExtension($icon) . "." . $output;
// Copy current image to document
$catched_image = ob_get_clean();
imagedestroy($dest);
// Upload session icon
download_document(array(document => $catched_image,
mimetype => $mimetype,
filename => $icon));
}
else {
// Upload session icon
download_document(array(document => $_SESSION[$_PAGE_INFO['id']][$_GET['session']]["icon_" . $icon],
mimetype => $_SESSION[$_PAGE_INFO['id']][$_GET['session']]["icon_" . $icon . "_mimetype"],
filename => $icon));
}
}
?>