[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Graphical counter

Posted on 2004-10-28
10
Medium Priority
?
247 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
Comment
Question by:x_terminat_or_3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 2

Expert Comment

by:suresh_asp
ID: 12450694
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
ID: 12451756
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
ID: 12468969
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 2

Author Comment

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

Expert Comment

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

Accepted Solution

by:
mces earned 2000 total points
ID: 12470942
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
ID: 12494049
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
ID: 12503196
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
ID: 12503356
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
ID: 12503361
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

656 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