barbaric counter [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

barbaric counter [SOLVED]

Post 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.
Last edited by s.dot on Sat Apr 08, 2006 4:33 am, edited 3 times in total.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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";
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post 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).
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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 :)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

bleh, i just spent a good bit of time basically writing a function that imagestring() does :)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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:
Post Reply