Hmm. Certainly seems as though there are problems writing the file. What happens if you delete the counter.txt file - does the script create it?
Maybe the “b” binary?
yes i deleted and now its working thanks your the man! let try the rest of the php
thank you both for your help i am just starting to understand the code, its weird all i had to do was delete counter.txt, i have moved on to the second part of the code, but still does not work, how do i test the second half can i put the debug lines in, and one other thing i don’t get i have resized all png files 0.png to 9.png but the code only uses 0 to 4 surly i have got to put the rest of the numbers in or is PHP that clever it adds them like 4.png + 1.png = 5.png
Probably by deleting counter.txt and allowing the script to create it, it would have a different owner.
I have just tried putting debug code in myself and it doesn’t work. Edit: Having played around with the script a bit more I have found that adding in any debug code will actually BREAK the script.
I’m not sure I can explain clearly but I’ll give it a go.
The line:
$chars = preg_split('//', $counterVal);
creates an array named $chars with each character in a different array element, so $char[1] is the first character, $char[2] the second character, etc. So “$chars[1].png” will be 0.png, 1,png … 9.png depending on the value of the 1st character.
I hope that hasn’t confused too much!
Yes, because it has no output until it’s built the image and tries to send headers after it’s done so, that won’t go well if the debug echos are still in place. Although I confess I didn’t really notice that, the original problem was that the counter was always zero, so it was more to see where it was going through that bit. So the echo statements won’t be possible here.
As @Gandalf noted, you’re building an array when you get the counter value, which will look something like this, assuming your counter value is, say, 123 (because the str_pad line adds the two leading zeroes):
$chars[0] = "0";
$chars[1] = "0";
$chars[2] = "1";
$chars[3] = "2";
$chars[4] = "3";
So when you load the graphics for each of those, you’re loading the corresponding pictures of each image. You only have five pictures loaded, but you need all ten digits to cover all values. So your code effectively says
$src4 = imagecreatefrompng("3.png");
for the example above.
Can you remove all the debug echos and show your complete current code? Also you’ve confirmed that you have canvas.png, and 0.png to 9.png in the correct format and an accessible location?
below is the code i am using, i think i am going wrong at the png bit because i read somewhere the code does not need changing but i think it does.
`
<?php session_start(); $debug = true; // Change this to false when you want to stop the extra echos $counter_name = "counter.txt"; // Check if a text file exists. If not create one and initialize it to zero. if (!file_exists($counter_name)) { $f = fopen($counter_name, "w"); fwrite($f,"0"); fclose($f); } // Read the current value of our counter file $f = fopen($counter_name,"r"); $counterVal = fread($f, filesize($counter_name)); fclose($f); // Has visitor been counted in this session? // If not, increase counter value by one if(!isset($_SESSION['hasVisited'])){ $_SESSION['hasVisited']="yes"; $counterVal++; $f = fopen($counter_name, "w"); fwrite($f, $counterVal); fclose($f); } $counterVal = str_pad($counterVal, 5, "0", STR_PAD_LEFT); $chars = preg_split('//', $counterVal); $im = imagecreatefrompng("canvas2.png"); $src1 = imagecreatefrompng("$chars[0].png"); $src2 = imagecreatefrompng("$chars[1].png"); $src3 = imagecreatefrompng("$chars[2].png"); $src4 = imagecreatefrompng("$chars[3].png"); $src5 = imagecreatefrompng("$chars[4].png"); imagecopymerge($im, $src1, 0, 0, 0, 0, 56, 75, 100); imagecopymerge($im, $src2, 60, 0, 0, 0, 56, 75, 100); imagecopymerge($im, $src3, 120, 0, 0, 0, 56, 75, 100); imagecopymerge($im, $src4, 180, 0, 0, 0, 56, 75, 100); imagecopymerge($im, $src5, 240, 0, 0, 0, 56, 75, 100); // Output and free from memory header('Content-Type: image/png'); echo imagepng($im); imagedestroy($im); ?>`
i am sure it should say:-
$chars[0] = “0”;
$src1 = imagecreatefrompng(“0.png”); and so on, or have i got this wrong
I think you have got it wrong.
$chars holds the single digits from a five digit number. If you force $chars[0] to be’0’ then when the number goes over 09999 it would reset to 00000 instead of continuing to 10000.
You’ve still got this code slightly wrong:
echo imagepng($im);
I believe it needs to read just
imagepng($im);
yes i read that somewhere so i removed the echo,
unfortuatly its still not working i can’t get it to show any images, just says “hit counter” on my webpage. does anyone have this working on a live webpage.
below is the code i am using:-
`
`
still not understanding if my counter went to 88 how does the script know to use the image file 8.png if its not in the code??
I can’t obviously see anything wrong with your code. It looks exactly as in the tutorial. You have got your canvas.png and digit image files in the same directory as your script, haven’t you?
I tried to explain this but didn’t do very well, @droopsnoot did a better job in post #40
thanks gandalf, yes i have canvas and 0.png to 9.png in the same folder, if the code is ok thne its got to be something to do with the images i have resize them and the canvas is transparent, any ideas ?? i did change the code to canvas2.png because i had 2 canvas files.
Droopsnoot say i should have this in my code:-
$src4 = imagecreatefrompng("3.png");
but mine say:-
$src4 = imagecreatefrompng("$chars[3].png");
thats the bit i am not clear on
You have just indicated something that IS wrong with your code which I failed to spot. You have, I think, got the following:
$src1 = imagecreatefrompng("$chars[0].png");
$src2 = imagecreatefrompng("$chars[1].png");
$src3 = imagecreatefrompng("$chars[2].png");
$src4 = imagecreatefrompng("$chars[3].png");
$src5 = imagecreatefrompng("$chars[4].png");
but the code (which works!) in the tutorial is
$src1 = imagecreatefrompng("$chars[1].png");
$src2 = imagecreatefrompng("$chars[2].png");
$src3 = imagecreatefrompng("$chars[3].png");
$src4 = imagecreatefrompng("$chars[4].png");
$src5 = imagecreatefrompng("$chars[5].png");
I’m not sure why you changed the original, but using the exact code from the tutorial should do the job.
ok i have changed it, i thought i needed to have the zero first, any its still not working, do you think its a file permission problem
I’ve been playing with this for a little while
First, the $chars[]
array key “number” is not the value of that array key.
(OK, that even confuses me and I just wrote it )
In other words, as an example, the “[1]” does not mean it’s the 1.png
it means it’s the second member of the array (most likely one of the pad added zeroes at this point).
As for [0]
it is true that the array starts with zero.
However, the preg_split returns an empty string - ‘’ - as the first and last array members.
If you are going to keep the preg_split, you will want to skip over the empty string and start off with [1]
.
My personal preference for something trivial is to use str_split, but that would require changing some code as no empty strings get returned
$counterVal = str_pad($counterVal, 5, "0", STR_PAD_LEFT);
//$chars = preg_split('//', $counterVal);
$chars = str_split($counterVal);
$im = imagecreatefrompng("canvas.png"); // 296 x 75
//$src1 = imagecreatefrompng($chars[1] . ".png"); // 56 x 75
//$src2 = imagecreatefrompng($chars[2] . ".png");
//$src3 = imagecreatefrompng($chars[3] . ".png");
//$src4 = imagecreatefrompng($chars[4] . ".png");
//$src5 = imagecreatefrompng($chars[5] . ".png");
$src1 = imagecreatefrompng($chars[0] . ".png"); // 56 x 75
$src2 = imagecreatefrompng($chars[1] . ".png");
$src3 = imagecreatefrompng($chars[2] . ".png");
$src4 = imagecreatefrompng($chars[3] . ".png");
$src5 = imagecreatefrompng($chars[4] . ".png");
*I also changed the variable - string bit. PHP parses variables inside double quotes, it’s just that I don’t like doing it that way. No offense intended @James_Hibbard
Given the problem you had when you created counter.txt rather than allowed the script to create it, perhaps the problem is the file ownership. If you check the ownership of counter.txt and change the ownership of the image files to the same…
No, I was saying that’s effectively what you have. Where your code has $chars[3] in that line, if the value of $chars[3] is 3, then it will be referencing 3.png, and so on. PHP will substitute the value of variable $chars[3] into the string.
OK, as it’s getting frustrating, I created some images and ran the original code on my WAMP server here. Out of the box, it works. I don’t quite understand why, but it does. For example, I don’t get why the preg_split line returns this, but then I don’t understand regular expressions to start with.
array (size=7) { this is $chars[] }
0 => string '' (length=0)
1 => string '0' (length=1)
2 => string '0' (length=1)
3 => string '0' (length=1)
4 => string '0' (length=1)
5 => string '1' (length=1)
6 => string '' (length=0)
string '00001' (length=5) { this is $counterVal }
when I var_dump($chars) followed by $counterVal. Hence the offsets for the array in the original code are correct, starting at 1 and going to 5, not base zero as I said . The echo before imagpng also doesn’t seem to cause any trouble - it works exactly the same whether I leave it in or remove it - I assume imagepng() outputs the image contents, then php follows it with true or false, which is ignored for some reason by the browser.
Backing up to your first post, you say " i have saved the following files on my web server in /srv/http" and then list the png files. Can you move them all to the same directory your php is in? That’s where they are for me - if you have them somewhere else, I think you’ll need to modify the lines to reference them, but I would expect you’d be getting error messages. Or are you still getting error messages? I think you’ll need to call /counter.php directly to see them.
(I’ve had a quick read down the thread and can’t see a point where you say you’ve copied them there, apologise if you already have and this is yet another confusion).
Yes, that’s the regex //
it “matches” the beginning and the ending non-character.
Definitely not a commonly seen pattern, but that’s what it does.
The PHP docs show an example that uses a Flag to deal with it
http://php.net/manual/en/function.preg-split.php
Example #2 Splitting a string into component characters
<?php
$str = 'string';
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($chars);
?>