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)
LVL 2
x_terminat_or_3Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.