Hit counter in php - php

I'm using simple php script to count visits. It's saved inside file. So there is a number inside a .txt file and I would like to put on my page in a form of some nice counter. So I need numbers to be represented as images in some way. So it can look nice. If I eventually had div where I can show this numbers, and 10 images of numbers from 0 to 9. 25x50px lets say and I would like to put them into tags is that possible?
I know my question is a bit dodgy and maybe not clear. But if someone understand what I'm asking for than if you could answer that would be great. Thx

Split the number into digits:
$a = str_split(1337, 1);
Use the array to create the counter:
foreach ($a as $n) {
print '<img src="'.$n.'.png">';
}

To get the number from the file:
$number = file_get_contents('visitors.txt');
To create an image, use the GD or Imagick library:
header("Content-type: image/png");
$im = imagecreatefrompng("images/button1.png");
// Add text to $im
imagepng($im);
To include your image in a page:
<img src="myImageScript.php?number=1234" />

Yes it is possible. First you need to get the number in char array and the proceed. Based on number display the images nest to each other.

If your problem is only showing the number as images, it's easy. Do something like:
$num = 1234;
for ($i = 0; $i < strlen($num); $i++)
echo "<img src=\"images/numbers/" . $num.{$i} . ".png\">";

have 10 images for the numbers 0-9 (0.png, 1.png, ...) and just convert every character in a string $counter="12345" into a corresponding image <img src="1.png" />
this is explained and implemented in javascript on this website.
you may also want to str_pad your $counter with 0.

Related

Random string to generate a new image each time page loads

What I want to do is have a new image generated when the page is loaded. I have the random string working, but the image won't generate. If I assign it a normal name, not a variable, it'll generate perfectly fine, permissions are set, etc.. Not exactly sure what to do to get it to create the image
Here's the code that works.
$chart->render("generated/chart.png");
// But it isn't dynamic, like I want it to be
print "<center><img src=generated/$char.png></center>";
Here's the code that doesn't work. So I had the idea of using a random string generator to make a random name for the image, and just do that.
// random string code
$length = 10;
$randomString = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
$chart->setTitle("Mini Mood Map");
// THIS is the line giving me problems, I've tried different quotes with no luck
$chart->render(generated/$randomString.png);
// Broken image
print "<center><img src=generated/$randomString.png></center>";
Sorry, I have a bad cold and my heads not on tight tonight. Thanks for all suggestions.
Note how the original is quoted:
$chart->render("generated/chart.png");
You need to do the same:
$chart->render("generated/" . $randomString . ".png");

Can anyone explain how this scrambling function works?

I'm working with a function taken from Corrupt (a web based piece of software used to get "glitchy" effects using jpeg images). This function can be found in the corrupt.php file on line 23. At the moment it's not making the files glitchy enough. I made this images to show you how I want the images to look. This was made by opening the jpeg in a text editor and cutting certain lines and pasting them in other places.
I want this function to do a similar thing but at the moment it doesn't. Any ideas? Is there a better way of doing this maybe?
function scramble($content, $size) {
$sStart = 10;
$sEnd = $size-1;
$nReplacements = rand(1, 30);
for($i = 0; $i < $nReplacements; $i++) {
$PosA = rand($sStart, $sEnd);
$PosB = rand($sStart, $sEnd);
$tmp = $content[$PosA];
$content[$PosA] = $content[$PosB];
$content[$PosB] = $tmp;
}
return($content);
}
It is randomly swapping information around in the data arrays loaded from your image. This causes a valid image to come out with invalid image information in some sectors. Also, image files sometimes contain additional information at the front/end of the file; this does not look like it takes that into account and could corrupt that information as well.
To increase the amount of swaps you will want to increase the number of replacements. The bit of code you are particularly interested in is rand(1, 30);; I would suggest increasing the minimum amount of scramble first and then the upper range if you still do not get the desired effect.
The function does random swaps between the elements of the array. The number of swaps is a randomly generated number from 1 to 30.

How can I make a simple path generator?

I have a link that I have to repeat 50 times, for each folder, and I have 15 folders.the link that I have to repeat looks like this:
now, the jpg files are named car 1- car 50. and I would really like to be able to generate this script so that I can input the path "update/images/Cars/" the picture title (car) and the input the number of times that I need this link, and then have it spit out something that looks like this:
and then it keeps repeating, I'm assuming this can be done with a counter, but I'm not sure. Thanks!
You can do it with a for loop:
$path = "update/images/Cars/";
$title = "car";
$times = 50;
for($i = 1; $i <= $times; $i++)
echo "\n";
I used $title for the lightbox argument since you didn't specify
Use a powerful text editor. ;-)
For example, in Vim, I can use the following sequence of keystrokes to create your required text:
i
Esc
qa (start recording macro into register a)
Y (yank (= copy) whole line)
p (paste into the following line)
/ ( Return (search for opening brace)
Space (advance cursor one character so it now sits on the number)
Ctrl+a (increment the number)
q (stop recording the macro)
49#a (invoke the macro 49 times)
If you're going to add or remove images from the folder, then you might get better results using the DirectoryIterator object from the Standard PHP Library. Using it would require PHP5, but there's an old-school way of handling it, too. This snippet assumes that all of the files in the directory are the images you want to list:
$link = '%s';
$dir = new DirectoryIterator("/path/to/update/images/Cars");
foreach($dir as $file) if(!$file->isDot()) echo sprintf($link, $file, $file);
Notice that I put the information about the anchor-element into the $link variable and then used sprintf to print those anchors to the screen. If you don't have PHP5 available to you, you'd want to do it this way:
$link = '%s';
$dir = opendir("/path/to/update/images/Cars");
while(($file = readdir($dir)!==false) if($file != "." && $file != "..") echo sprintf($link, $file, $file);
closedir($dir);
These would only be necessary if you're adding more car photos into the library and don't want to update the page that produces all the links. Both of these snippets should automatically search through the directory of car images and create the links you need.
You can also alter these snippets to search through sub-directories, so you could slam out the links to the images in all 15 folders all with a little bit more code. Let me know if you want to see that code, too.

(PHP) randomly insert a 10 word sentence into a large text document

I have large text files 140k or larger full of paragraphs of text and need to insert a sentence in to this file at random intervals only if the file contains more then 200 words.
The sentence I need to insert randomly throughout the larger document is 10 words long.
I have full control over the server running my LAMP site so I can use PHP or a linux command line application if one exists which would do this for me.
Any ideas of how best to tackle this would be greatly appreciated.
Thanks
Mark
You could use str_word_count() to get the number of words in the string. From there, determine if you want to insert the string or not. As for inserting it "at random," that could be dangerous. Do you mean to suggest you want to insert it in a couple random areas? If so, load the contents of the file in as an array with file() and insert your sentence anywhere between $file[0] and count($file);
The following code should do the trick to locate and insert strings into random locations. From there you would just need to re-write the file. This is a very crude way and does not take into account punctuation or anything like that, so some fine-tuning will most likely be necessary.
$save = array();
$words = str_word_count(file_get_contents('somefile.txt'), 1);
if (count($words) <= 200)
$save = $words;
else {
foreach ($words as $word) {
$save[] = $word;
$rand = rand(0, 1000);
if ($rand >= 100 && $rand <= 200)
$save[] = 'some string';
}
}
$save = implode(' ', $save);
This generates a random number and checks if it's between 100 and 200 inclusive and, if so, puts in the random string. You can change the range of the random number and that of the check to increase or decrease how many are added. You could also implement a counter to do something like make sure there are at least x words between each string.
Again, this doesn't take into account punctuation or anything and just assumes all words are separated by spaces. So some fine tuning may be necessary to perfect it, but this should be a good starting point.

Pixel Drawing Algorithm

I need an example algorithm that will draw pixels one at a time on a grid based (x,y) system, and also color them based on an rbg value based on binary data that is provided in some form. I am looking for anything written in php or a php like language such as C, but that does not use any sort of library or graphics card api, as i am coding in php.
Here is something that i wrote in php, that uses random color values but it takes 15 seconds to render in an html canvas:
<?php
$r_max = 240;
$c_max = 320;
$row = -1;//-1 to offset while
while ($row<$r_max){
++$row;
for($column=0; $column<=$c_max; ++$column)
{
echo 'ctx.fillStyle = "rgb(', rand()%255, ',', rand()%255, ',', rand()%255, ')";';
echo 'ctx.fillRect(', $column, ',', $row, ',1,1);';
}
}
?>
Not really sure i quite understand your question but .. PHP has GD functions that include image allocate and setpixel calls, line drawing etc .. check here
oh and yes imagemagick also for more exotic uses
It seem you are trying to output JavaScript commands for drawing on a <canvas> tag. A faster way to draw the pixels might be to use moveTo and lineTo. Btw, why isn't you outer loop a for loop as well?
Doesn't
for($row=0; $row<=$r_max; ++$row) {
for($column=0; $column<=$c_max; ++$column) {
# draw pixel
}
}
seem more natural?
The issue is that you're generating code for each pixel. Instead, why not have the code write the pixel info to your favorite image format, then display that in the page? That's the most reasonable (to me) algorithmic solution... I'm not sure if it'll fit into what you're trying to do.
I cant use an image format, because it is not efficient for my usage. I am looking for some example code where an image might be displayed based on data, just so I can get an idea of how to do what I am doing at a rate faster then 15 seconds per render. The nested loops I included above are way to slow.

Categories