• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

Graphical counter

Hi all


I made a php/mysql plaintext counter

It works OK, but I'd like to output images instead of text

So I made a function that eats a number and outs a html formatted counter


function imagecount($number=0,$minLength=6)
 {$output='';
  $input='000000';
  $l=6;
  $img='<img align="middle" style="margin:0px; padding:0px; spacing:0px" src="/pa2/images/counter/';
  $imgend='.gif" alt="Teller">';
 
  if(isset($number) && ($number>=0)) $input=$number;
  if(isset($minLength) && ($minLength>1)) $l=$minLength;
 
  $input=str_pad($input,$l,"0",STR_PAD_LEFT);
 

  for($c=0; $c<strlen($input); $c++)
  {$output.=$img.$input{$c}.$imgend;}
 
  return $output;}


But I like to output an image instead of a htmlstring.

I played around a bit with the imagecreate and all that but It keeps giving me errors...  I enabled the gd library in php.ini, so it isn't about that.


Can anybody tell me how to change the function so that it eats numbers and outputs an image instead of html code

All digits are 17,22

The counter's width should be (17*strlen($input))+10
's height: 32

1st image on 5,5



Kind regards





Ramses (x_terminator_3)
0
x_terminat_or_3
Asked:
x_terminat_or_3
  • 5
  • 4
1 Solution
 
suresh_aspCommented:
Try this,

<?php
header ("Content-type: image/png");

// calling function
imagecount(14567345,16);

function imagecount($number=0,$minLength=6)
 {$output='';
 
  $l=6;
   
  if(isset($number) && ($number>=0)) $input=$number;
  if(isset($minLength) && ($minLength>1)) $l=$minLength;
 
  $input=str_pad($input,$l,"0",STR_PAD_LEFT);
 
  for($c=0; $c<strlen($input); $c++)
  {$output.=$input{$c};}
 
$im = @imagecreate (17*strlen($input)+10, 32) or die ("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate ($im, 255, 255, 255);
$text_color = imagecolorallocate ($im, 233, 14, 91);
imagestring ($im, 1, 5, 5,$output, $text_color);
imagepng ($im);
imagedestroy ($im);
  }
?>
0
 
x_terminat_or_3Author Commented:
Thank you for your reply but I want to make one image based on the image digits I already have.

The example you provided creates the counter without loading images for the digits...
0
 
mcesCommented:
That's really easy too.  You use imagecreatefromgif in a for to load all your then digits.  Then use imagecopy function to copy digits into your final image.

If you want me to write the code, just give us the URL to your digits.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
x_terminat_or_3Author Commented:
/pa2/images/counter/digitx.gif  where x is a number in range 0-9
0
 
mcesCommented:
Umm, I meant the full path to digits so I can get them and test the code.
0
 
mcesCommented:
Here we go.  Replace the $digitsprefix with the absolute path of the digits prefix on your machine:

<?php
 
$digitsprefix = "http://www.bildung.hessen.de/media/font/";
 
for ($d = 0; $d < 10; $d++)
  $dig[$d] = imagecreatefromgif($digitsprefix.$d.".gif");
// calling function
imagecounter($_REQUEST['n'], $_REQUEST['l']);
 
function imagecounter($number=0, $len=0)
{
  global $dig;
  header ("Content-type: image/png");
 
  if ($len == 0)
    $len = 6;
 
  $str=str_pad($number,$len,"0",STR_PAD_LEFT);
 
  $im = @imagecreate (17*strlen($str)+10, 32) or die ("Cannot Initialize new GD image stream");
  $background_color = imagecolorallocate ($im, 255, 255, 255);
  for ($i = 0; $i < strlen($str); $i++)
    imagecopy ($im, $dig[(int)$str[$i]], 5 + $i * 17, 5, 0, 0, 17, 22);
  imagepng ($im);
  imagedestroy ($im);
}
?>
0
 
x_terminat_or_3Author Commented:
please be patient.  I just managed migrated to fedora.  Please allow a few days to get me going...
0
 
x_terminat_or_3Author Commented:
mces, I want the image generated in the body of an existing script, not in the header.  Is that possible?


I want to call a function that returns an image instead of text so that I can echo the image.
0
 
mcesCommented:
Well, you need to put this file somewhere on your server, and then in your other scripts, you write a function like this:

function imagecount($n, $l = 6) {
  return '<img src="/path/to/my/script/imagecounter.php?n='.$n.'&l='.$l;
}


Here you go!
0
 
mcesCommented:
Wel, the correct one is this:


function imagecount($n, $l = 6) {
  return '<img src="/path/to/my/script/imagecounter.php?n='.$n.'&l='.$l.'">;
}


There's no other way.  I mean, you need to put that code in another file.  The image data cannot go into your html.
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now