Solved

Graphical counter

Posted on 2004-10-28
246 Views
Last Modified: 2006-11-17
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
Question by:x_terminat_or_3
    10 Comments
     
    LVL 2

    Expert Comment

    by:suresh_asp
    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
     
    LVL 2

    Author Comment

    by:x_terminat_or_3
    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
     
    LVL 1

    Expert Comment

    by:mces
    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
     
    LVL 2

    Author Comment

    by:x_terminat_or_3
    /pa2/images/counter/digitx.gif  where x is a number in range 0-9
    0
     
    LVL 1

    Expert Comment

    by:mces
    Umm, I meant the full path to digits so I can get them and test the code.
    0
     
    LVL 1

    Accepted Solution

    by:
    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
     
    LVL 2

    Author Comment

    by:x_terminat_or_3
    please be patient.  I just managed migrated to fedora.  Please allow a few days to get me going...
    0
     
    LVL 2

    Author Comment

    by:x_terminat_or_3
    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
     
    LVL 1

    Expert Comment

    by:mces
    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
     
    LVL 1

    Expert Comment

    by:mces
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: JavaScript Coding - Massive 12-Part Bundle

    Regardless of your programming skill level, you'll go from basics to advanced concepts in a vast array of JavaScript subjects including Sammy.js, Agility.js, Ember.js, Node.js, jQuery, AJAX, Extjs, AngularJS, Knockout.js, and JSON.

    Suggested Solutions

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now