Page 1 of 1

barbaric counter [SOLVED]

Posted: Sat Apr 08, 2006 2:17 am
by s.dot
don't ask why im doing it this way, but for what im doing, it makes sense :-D

I have a counter like deal, that is attempting to take a number like 12345 and turn it into an image with the text 12345 on it

I dont' have the freetype extension installed/enabled so I can't use imagegettftext, so I'm trying something like this.

Code: Select all

$counts = "12345";
for($i=0;$i<strlen($counts);$i++){
    echo file_get_contents(substr($counts,$i,1).".gif");
}
Where it would attempt to read the 1.gif, 2.gif, 3.gif files to the browser

However, it will only show the first number, and all the others seem to be ignored.

I have also tried readfile(), but the same thing happened.

Posted: Sat Apr 08, 2006 2:21 am
by s.dot
oh, wow

i have fiddled with this thing for an hour,

and it seems echo substr($counts,$i,1).".gif"; is all I needed :|

I should be banned from ever writing php again.

Posted: Sat Apr 08, 2006 2:24 am
by s.dot
actually now my problem comes when using this in an img tag

Code: Select all

<img src="showcount.php?count=12345">
I just get a red X

Code: Select all

header("Content-type: image/gif");
for($i=0;$i<strlen($counts);$i++){
	echo substr($counts,$i,1).".gif";
}

Posted: Sat Apr 08, 2006 2:30 am
by Ree
For this to work I think you'll need to merge the images into one. Right now you're just printing out multiple images into a single <img> tag.

Posted: Sat Apr 08, 2006 2:31 am
by s.dot
Yeah but I can't do that without the freetype extension installed... if I'm correct.
I would use imagegettftext()... is there something similar to this?

I got it working using the image tag with this code

Code: Select all

header("Content-type: image/gif");
for($i=0;$i<strlen($counts);$i++){
    echo readfile(substr($counts,$i,1).".gif");
}
but again its only printing out the first number.

Posted: Sat Apr 08, 2006 2:38 am
by Ree
It should of course print only one. Btw, it's enough to have GD extension to merge images since you don't use FreeType functionality (you have ready made images).

You could also simply generate multiple image tags one by one and output them (5 number counter would result in 5 <img> tags).

Posted: Sat Apr 08, 2006 2:39 am
by s.dot
I see what you're saying. One image tag = one image. I'll look up on the GD image merging functions.

Thanks :)

Posted: Sat Apr 08, 2006 4:32 am
by s.dot
bleh, i just spent a good bit of time basically writing a function that imagestring() does :)

Posted: Sat Apr 08, 2006 7:14 am
by Oren

Code: Select all

header("Content-type: image/gif");
for($i=0;$i<strlen($counts);$i++){
    echo readfile(substr($counts,$i,1).".gif");
}
Why don't you do it like this?!:

Code: Select all

header("Content-type: image/gif");
for($i=0, $len = strlen($counts);$i<$len;$i++){
    echo readfile(substr($counts,$i,1).".gif");
}
You don't need to call to strlen() each time since the content of $counts remains the same :wink:

Posted: Sat Apr 08, 2006 8:10 am
by feyd
you don't need echo with readfile(), it sends the data directly to output. Not only that, but multiple calls to it with binary data such as images like these won't result in anything near desirable. The images must either be combined into a single image or send in separate streams apart from each other.

Posted: Mon Apr 10, 2006 1:29 am
by Oren
Oren wrote:

Code: Select all

header("Content-type: image/gif");
for($i=0;$i<strlen($counts);$i++){
    echo readfile(substr($counts,$i,1).".gif");
}
Why don't you do it like this?!:

Code: Select all

header("Content-type: image/gif");
for($i=0, $len = strlen($counts);$i<$len;$i++){
    echo readfile(substr($counts,$i,1).".gif");
}
You don't need to call to strlen() each time since the content of $counts remains the same :wink:
LOL... I'm quoting myself. Anyway, I've just read another post and in this post I found this link:
http://www.blueshoes.org/phpBench.php
Scroll down and then find "For-loop test" you will find out that my way to do the for() loop like in my previous post is actually 7 times faster than what scottayy did at first. So next time you guys use some loop - remember to do the calc before that and don't calc it each time again and again even though the result remains the same :wink: