Scripts
cijfers omzetten in 7 segmenten
Met behulp van de GD library cijfers omzetten naar 7 segmenten zoals gebruikt op klokken, multi_meters e.d. Zie ook de tutorial http://www.kiekkast.nl/Tutorial
cijfers-omzetten-in-7-segmenten
<?php
$value = '75832'; // shown value max 9 characters
// set the dimension of the character
$a =6; // smaller then 3 gives a poor result. this value is the width of a segment in pixels
// set the correction of the dimension of the image. 0 is default
$width_corr = '0'; // correction of the width of the image in pixels + or -
$height_corr = '0'; // correction of the height of the image in pixels + or -
// set the correction of the first (right) character. 0 is default
$hor_corr = 0; // horizontal correction for the first (right) character in pixels + or -
$vert_corr = 0; // vertical correction for the first (right) character in pixels + or -
if($value == '')
{
$width = 100;
$height = 30;
}
elseif (strlen($value) > 9)
{
$width = 110;
$height = 50;
}
else
{
$num_characters = strlen($value);
$width = $a*7.5*$num_characters+$width_corr;
$height= $a*12+$height_corr;
}
// make the image
$image = ImageCreate($width,$height);
// background color
$bgcolor = ImageColorAllocate($image, 10, 10, 10);
// color of the character
$color = imagecolorallocate($image, 0,255 ,0 );
$hor_1 = $width-($a*6)+$hor_corr;
$vert = $height-($height*0.8)+$vert_corr;
if($value == '')
{
ImageString($image, 10, 15, 5, "no input", $color);
}
elseif (strlen($value) > 9)
{
ImageString($image, 5, 15, 5, "value out ", $color);
ImageString($image, 5, 15, 20, "of range", $color);
}
else
{
if (strlen($value) == 1) // if there is one character
{
$value_1 = substr($value,0,1);
}
if (strlen($value) == 2) // if there are two characters
{
$value_1 = substr($value,1,1);
$value_2 = substr($value,0,1);
}
if (strlen($value) == 3) // if there are three characters
{
$value_1 = substr($value,2,1);
$value_2 = substr($value,1,1);
$value_3 = substr($value,0,1);
}
if (strlen($value) == 4)
{
$value_1 = substr($value,3,1);
$value_2 = substr($value,2,1);
$value_3 = substr($value,1,1);
$value_4 = substr($value,0,1);
}
if (strlen($value) == 5)
{
$value_1 = substr($value,4,1);
$value_2 = substr($value,3,1);
$value_3 = substr($value,2,1);
$value_4 = substr($value,1,1);
$value_5 = substr($value,0,1);
}
if (strlen($value) == 6)
{
$value_1 = substr($value,5,1);
$value_2 = substr($value,4,1);
$value_3 = substr($value,3,1);
$value_4 = substr($value,2,1);
$value_5 = substr($value,1,1);
$value_6 = substr($value,0,1);
}
if (strlen($value) == 7)
{
$value_1 = substr($value,6,1);
$value_2 = substr($value,5,1);
$value_3 = substr($value,4,1);
$value_4 = substr($value,3,1);
$value_5 = substr($value,2,1);
$value_6 = substr($value,1,1);
$value_7 = substr($value,0,1);
}
if (strlen($value) == 8)
{
$value_1 = substr($value,7,1);
$value_2 = substr($value,6,1);
$value_3 = substr($value,5,1);
$value_4 = substr($value,4,1);
$value_5 = substr($value,3,1);
$value_6 = substr($value,2,1);
$value_7 = substr($value,1,1);
$value_8 = substr($value,0,1);
}
if (strlen($value) == 9)
{
$value_1 = substr($value,8,1);
$value_2 = substr($value,7,1);
$value_3 = substr($value,6,1);
$value_4 = substr($value,5,1);
$value_5 = substr($value,4,1);
$value_6 = substr($value,3,1);
$value_7 = substr($value,2,1);
$value_8 = substr($value,1,1);
$value_9 = substr($value,0,1);
}
$b = $a/2;
$c = $a*2;
$d = $a*3;
$y = 2*$d;
$z = 3*$a+3;
for($i=0;$i<$num_characters;$i++)
{
$hor = $hor_1 -($i*7*$a+5);
$segm_1 = array(
$a+2+$hor , $vert, // Point 1 (x, y)
$b+2+$hor , $b+$vert, // Point 2 (x, y)
$a+2+$hor , $a+$vert, // Point 3 (x, y)
$a+2+$c+$hor , $a+$vert, // Point 4 (x, y)
$a+2+$c+$b+$hor, $b+$vert, // Point 5 (x, y)
$a+2+$c+$hor , $vert, // Point 6 (x, y)
);
$segm_7 = array(
$a+2+$hor , $d+$vert, // Point 1 (x, y)
$b+2+$hor , $b+$d+$vert, // Point 2 (x, y)
$a+2+$hor , $a+$d+$vert, // Point 3 (x, y)
$a+2+$c+$hor , $a+$d+$vert, // Point 4 (x, y)
$a+2+$c+$b+$hor, $b+$d+$vert, // Point 5 (x, y)
$a+2+$c+$hor , $d+$vert, // Point 6 (x, y)
);
$segm_4 = array(
$a+2+$hor , $y+$vert, // Point 1 (x, y)
$b+2+$hor , $b+$y+$vert, // Point 2 (x, y)
$a+2+$hor , $a+$y+$vert, // Point 3 (x, y)
$a+2+$c+$hor , $a+$y+$vert, // Point 4 (x, y)
$a+2+$c+$b+$hor, $b+$y+$vert, // Point 5 (x, y)
$a+2+$c+$hor , $y+$vert, // Point 6 (x, y)
);
$segm_2 = array(
$b+$hor , $b+1+$vert, // Point 1 (x, y)
$hor , $a+1+$vert, // Point 2 (x, y)
$hor , $d-1+$vert, // Point 3 (x, y)
$b+$hor , $b-1+$d+$vert, // Point 4 (x, y)
$a+$hor , $d-1+$vert, // Point 5 (x, y)
$a+$hor , $a+1+$vert, // Point 6 (x, y)
);
$segm_3 = array(
$b+$hor , $b+1+$d+$vert, // Point 1 (x, y)
$hor , $a+1+$d+$vert, // Point 2 (x, y)
$hor , $d-1+$d+$vert, // Point 3 (x, y)
$b+$hor , $b-1+$d+$d+$vert, // Point 4 (x, y)
$a+$hor , $d-1+$d+$vert, // Point 5 (x, y)
$a+$hor , $a+1+$d+$vert, // Point 6 (x, y)
);
$segm_5 = array(
$b+1+$z+$hor , $b+1+$vert, // Point 1 (x, y)
$z+1+$hor , $a+1+$vert, // Point 2 (x, y)
$z+1+$hor , $d-1+$vert, // Point 3 (x, y)
$b+1+$z+$hor , $b-1+$d+$vert, // Point 4 (x, y)
$a+1+$z+$hor , $d-1+$vert, // Point 5 (x, y)
$a+1+$z+$hor , $a+1+$vert, // Point 6 (x, y)
);
$segm_6 = array(
$b+1+$z+$hor , $b+1+$d+$vert, // Point 1 (x, y)
$z+1+$hor , $a+1+$d+$vert, // Point 2 (x, y)
$z+1+$hor , $d-1+$d+$vert, // Point 3 (x, y)
$b+1+$z+$hor , $b-1+$d+$d+$vert, // Point 4 (x, y)
$a+1+$z+$hor , $d-1+$d+$vert, // Point 5 (x, y)
$a+1+$z+$hor , $a+1+$d+$vert, // Point 6 (x, y)
);
if($i == '0'){$char = $value_1;}
if($i == '1'){$char = $value_2;}
if($i == '2'){$char = $value_3;}
if($i == '3'){$char = $value_4;}
if($i == '4'){$char = $value_5;}
if($i == '5'){$char = $value_6;}
if($i == '6'){$char = $value_7;}
if($i == '7'){$char = $value_8;}
if($i == '8'){$char = $value_9;}
if($char == '0'|$char == '2'|$char == '3'|$char == '5'|$char == '6'|$char == '7'|$char == '8'|$char == '9') {imagefilledpolygon($image, $segm_1, 6, $color);}
if($char == '0'|$char == '4'|$char == '5'|$char == '6'|$char == '8'|$char == '9') {imagefilledpolygon($image, $segm_2, 6, $color);}
if($char == '0'|$char == '2'|$char == '6'|$char == '8') {imagefilledpolygon($image, $segm_3, 6, $color);}
if($char == '0'|$char == '2'|$char == '3'|$char == '5'|$char == '6'|$char == '8') {imagefilledpolygon($image, $segm_4, 6, $color);}
if($char == '0'|$char == '1'|$char == '2'|$char == '3'|$char == '4'|$char == '7'|$char == '8'|$char == '9') {imagefilledpolygon($image, $segm_5, 6, $color);}
if($char == '0'|$char == '1'|$char == '3'|$char == '4'|$char == '5'|$char == '6'|$char == '7'|$char == '8'|$char == '9') {imagefilledpolygon($image, $segm_6, 6, $color);}
if($char == '2'|$char == '3'|$char == '4'|$char == '5'|$char == '6'|$char == '8'|$char == '9') {imagefilledpolygon($image, $segm_7, 6, $color);}
}
}
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
En nu nog een script met een afbeelding als achtergrond
<?php
$value = '84'; // shown value max 2 characters
if($value == ''){$empty ="yes"; } else {$empty = "no";}
$length = strlen($value); // numbers of caracters
// set the dimension of the character
$a =5; // smaller then 3 gives a poor result. this value is the width of a segment in pixels
// set the correction of the first (right) character. 0 is default
$hor_1 = 72;
$vert = 37;
// make the image
$image = imagecreatefrompng("http://www.kiekkast.nl/Indicators/img_digi/gele_meter_2_klein.png");
// background color
$bgcolor = ImageColorAllocate($image, 10, 10, 10);
// color of the character
$color = imagecolorallocate($image, 50,50 ,50 );
if($empty == 'yes')
{
ImageString($image, 4, 34, 40, "no input", $color);
}
elseif ($length > 2)
{
ImageString($image, 4, 34, 35, "value out ", $color);
ImageString($image, 4, 34, 50, "of range", $color);
}
else
{
$b = $a/2;
$c = $a*2;
$d = $a*3;
$y = 2*$d;
$z = 3*$a+3;
for($i=0;$i<$length;$i++)
{
$hor = $hor_1 -($i*6*$a);
$segm_1 = array(
$a+2+$hor , $vert, // Point 1 (x, y)
$b+2+$hor , $b+$vert, // Point 2 (x, y)
$a+2+$hor , $a+$vert, // Point 3 (x, y)
$a+2+$c+$hor , $a+$vert, // Point 4 (x, y)
$a+2+$c+$b+$hor, $b+$vert, // Point 5 (x, y)
$a+2+$c+$hor , $vert, // Point 6 (x, y)
);
$segm_7 = array(
$a+2+$hor , $d+$vert, // Point 1 (x, y)
$b+2+$hor , $b+$d+$vert, // Point 2 (x, y)
$a+2+$hor , $a+$d+$vert, // Point 3 (x, y)
$a+2+$c+$hor , $a+$d+$vert, // Point 4 (x, y)
$a+2+$c+$b+$hor, $b+$d+$vert, // Point 5 (x, y)
$a+2+$c+$hor , $d+$vert, // Point 6 (x, y)
);
$segm_4 = array(
$a+2+$hor , $y+$vert, // Point 1 (x, y)
$b+2+$hor , $b+$y+$vert, // Point 2 (x, y)
$a+2+$hor , $a+$y+$vert, // Point 3 (x, y)
$a+2+$c+$hor , $a+$y+$vert, // Point 4 (x, y)
$a+2+$c+$b+$hor, $b+$y+$vert, // Point 5 (x, y)
$a+2+$c+$hor , $y+$vert, // Point 6 (x, y)
);
$segm_2 = array(
$b+$hor , $b+1+$vert, // Point 1 (x, y)
$hor , $a+1+$vert, // Point 2 (x, y)
$hor , $d-1+$vert, // Point 3 (x, y)
$b+$hor , $b-1+$d+$vert, // Point 4 (x, y)
$a+$hor , $d-1+$vert, // Point 5 (x, y)
$a+$hor , $a+1+$vert, // Point 6 (x, y)
);
$segm_3 = array(
$b+$hor , $b+1+$d+$vert, // Point 1 (x, y)
$hor , $a+1+$d+$vert, // Point 2 (x, y)
$hor , $d-1+$d+$vert, // Point 3 (x, y)
$b+$hor , $b-1+$d+$d+$vert, // Point 4 (x, y)
$a+$hor , $d-1+$d+$vert, // Point 5 (x, y)
$a+$hor , $a+1+$d+$vert, // Point 6 (x, y)
);
$segm_5 = array(
$b+1+$z+$hor , $b+1+$vert, // Point 1 (x, y)
$z+1+$hor , $a+1+$vert, // Point 2 (x, y)
$z+1+$hor , $d-1+$vert, // Point 3 (x, y)
$b+1+$z+$hor , $b-1+$d+$vert, // Point 4 (x, y)
$a+1+$z+$hor , $d-1+$vert, // Point 5 (x, y)
$a+1+$z+$hor , $a+1+$vert, // Point 6 (x, y)
);
$segm_6 = array(
$b+1+$z+$hor , $b+1+$d+$vert, // Point 1 (x, y)
$z+1+$hor , $a+1+$d+$vert, // Point 2 (x, y)
$z+1+$hor , $d-1+$d+$vert, // Point 3 (x, y)
$b+1+$z+$hor , $b-1+$d+$d+$vert, // Point 4 (x, y)
$a+1+$z+$hor , $d-1+$d+$vert, // Point 5 (x, y)
$a+1+$z+$hor , $a+1+$d+$vert, // Point 6 (x, y)
);
$char = substr($value, $length-1-$i ,1);
if($char == '0'|$char == '2'|$char == '3'|$char == '5'|$char == '6'|$char == '7'|$char == '8'|$char == '9') {imagefilledpolygon($image, $segm_1, 6, $color);}
if($char == '0'|$char == '4'|$char == '5'|$char == '6'|$char == '8'|$char == '9') {imagefilledpolygon($image, $segm_2, 6, $color);}
if($char == '0'|$char == '2'|$char == '6'|$char == '8') {imagefilledpolygon($image, $segm_3, 6, $color);}
if($char == '0'|$char == '2'|$char == '3'|$char == '5'|$char == '6'|$char == '8') {imagefilledpolygon($image, $segm_4, 6, $color);}
if($char == '0'|$char == '1'|$char == '2'|$char == '3'|$char == '4'|$char == '7'|$char == '8'|$char == '9') {imagefilledpolygon($image, $segm_5, 6, $color);}
if($char == '0'|$char == '1'|$char == '3'|$char == '4'|$char == '5'|$char == '6'|$char == '7'|$char == '8'|$char == '9') {imagefilledpolygon($image, $segm_6, 6, $color);}
if($char == '2'|$char == '3'|$char == '4'|$char == '5'|$char == '6'|$char == '8'|$char == '9') {imagefilledpolygon($image, $segm_7, 6, $color);}
}
}
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
René
Reacties
0