A simple counter written with php3

Source: Internet
Author: User
Tags format define empty function prototype header install php php and strlen
PHP has an extremely powerful image processing capability, which makes it easy to dynamically generate web images.
This is a simple counter made using PHP.

1. General ideas:
Record the number of previous visitors in a text file, and when the page is accessed, open the file from the
and read out the previous number of visitors, plus 1, get the latest number of visitors, and the number of formats into
Standard format, and then call the image processing function, the number output to the picture, and then the new access number back to
Write to the record number of visitors to the file.

2. Description of the function used by the program:
A. Related file operations:
A. Open the file:
The prototype of the function: int fopen (string filename, string mode);
Return result: If the file is opened successfully, the function returns the file stream pointer, or FALSE (0).
Parameter description:
String filename--The name of the file to be opened must be a string.
For example, "Zzm.txt", ".. \zzm.txt "and so on.
String mode-the way to open a file must be a character form.
' R ', read-only form, file pointer to beginning of file
' r+ ', readable and writable, file pointer to beginning of file
' W ', write-only form, the file pointer points to the beginning of the file, the file length truncated to 0,
If the file does not exist, it will attempt to establish the file.
' w+ ', readable and writable, the file pointer to the beginning of the file, the file length truncated to 0,
If the file does not exist, it will attempt to establish the file.
' A ', append form (writable only), file pointer to end of file, if text
The piece does not exist and will attempt to create the file.
' A + ', readable and writable, and the file pointer points to the end of the file, if the file does not exist,
Will attempt to create the file.
Example: Open the "Zzm.txt" below the current directory in read-only form
$fp = fopen ("Zzm.txt", "R");

B. Close the file:
Function prototype: int fclose (int fp);
Return Result: Successful return 1, failure return 0
Parameter description: The int FP is the file stream pointer returned by the fopen function.
Example: Turn off the Zzm.txt file that you just opened with fopen
Fclose ($FP);

C. Read the document:
Function prototype: string fgets (int fp, int length);
Return result: Returns a string of length-1 length, if the end of the file is returned, EOF (ends of file)

Parameter description:
int FP--The file stream pointer to read data, the value returned by the fopen function
int length-The number of characters to be read, the actual number of characters read is length-1
Example: Reading 9 characters from a $fp
$str 1 = fgets ($fp, 10);

D. Write a document:
Function prototype: int fputs (int fp, string str, int [length]);
Return result: Same as Fclose
Parameter description:
int FP--The file stream pointer to write information, the value returned by the fopen function
String STR-the strings to be written to the file.
int length--The length of the write, optional, if length is not supplied, the entire string is written.
Otherwise, the length character is written.
Example: Write "0000000001" to $fp
Fput ($fp, "0000000001");

B. Related String Functions:
A. Calculate string length:
Function prototype: int strlen (string str);
Return result: Returns the length of the string
Parameter description:
String STR--strings to calculate the length
Example: Calculating the string length of "000000000"
$str 2 = "000000000";
$len 2 = strlen ($STR);

B. String addition: The simplest, with one. Connect the two strings together.
Example: Add $str 1 and $STR2
$str = $str 1. $str 2

C. Related graphic functions:
A. New Image:
Function prototype: int imagecreate (int x_size, int y_size);
Return result: Returns an empty image recognition number (imageID) with a x*y pixel size
Parameter description: X_size,y_size is the width and height (in pixels) of the new image, respectively
Example: Create an empty picture with a 88*31 pixel size
$ImageID = Imagecreate (88, 31);

B. Assigning a color to an image:
Function prototype: int imagecolorallocate (int im, int red, int green, int blue);
Return result: Returns an RGB color identification number to the image ($im)
Parameter description: int im image recognition number
int red, green, and blue are the components of the red, green and blue colors, taking a value range of 0-255
Example: Assign an identification number to the image $im as $white white color, and white RGB to (255,255,255)
$white = Imagecolorallocate ($im, 255, 255, 255);
C. To fill the color of the image:
function prototypes: int imagefill (int im, int x, int y, int col);
Return result: Successfully return 1, otherwise return 0
Parameter description: int IM, image identification number
int x, int y, fill color starting from image (x,y) coordinates
(0,0) represents the upper-left corner of the image
int col, color identification number
Example: Starting from the upper-left corner of the image (i.e. the entire picture) fill in the black (already using the Imagecolorallocate function
The black color identification number is defined as $black.
Imagefill ($im, 0, 0, $black);

D. Calculate the width of the image:
Function prototype: int imagesx (int im);
Return result: Returns the width of the image (in pixels)
Parameter description: int IM, image identification number.
Example: Calculating the width of an image $im
$px = Imagesx ($im);

E. Writing horizontal text in an image:
function prototypes: int imagestring (int im, int font, int x, int y, string s, int col)
;
Return result: Successfully return 1, otherwise return 0
Parameter description: int IM, image identification number
int font, font recognition number, built-in fonts 1 to 5, users can use Imageloadfont () themselves?
?
Load Fonts.
int X,int y to begin writing the coordinates of the font (0,0) to the upper-left corner of the picture.
string s, string to write
int col, font color identification number
Example: Write a font size of 3 in the position of the image (3,3) and the color is white (already with the imagecolorallocate () function
String "E&j Counter" with black color identification number $white) defined
Imagestring ($im, 3, 3, 3, "E&j Counter", $white);

F. Draw a straight line in the image:
function prototypes: int imageline (int im, int x1, int y1, int x2, int y2, int col);
Return result: Successfully return 1, otherwise return 0
Parameter description: int IM, image identification number
int X1,int Y1, underlined coordinates
int X2,int y2, underlined stop coordinates
int col, line color identification number
Example: a straight line in the image $im from (1,14) to (85,14) color to $white
Imageline ($im, 1, $white);

G. Output the image in GIF format:
Function prototype: int imagegif (int im, string filename);
Return result: Successfully return 1, otherwise return 0
Parameter description: int IM, image identification number
string filename, generate the name of the picture, optional, if filename is empty, then direct?
Neon su?
Picture, you need to use header ("Content-type:image/gif") to predefined PHP output in the inner
Capacity for pictures
Example: Image $im output to a picture with file name "Image1.gif"
Imagegif ($im, "image1.gif");

H. Releasing images:
Function prototype: int Imagedestroy (int im);
Return result: Successfully return 1, otherwise return 0
Parameter description: int IM, the image recognition number to be released. This function frees up the image and image of the ID im.
The system resources used.
Example: releasing an image $im
Imagedestroy ($im);

3. How to install this counter:
A. The system must have a PHP interpreter installed. PHP can be downloaded on the http://www.php.net/, and there is a
With
Technical data can be browsed or downloaded for reading. How to install PHP please refer to its own instructions.
B. Copy the following list of programs to a file and take the extension to PHP and put it in a directory that can run PHP scripts?
Brigade GUI?
and create a plain text file under the directory named Zzm.txt. The purpose of this document is to record the previous
Visiting people
Number of uses. You can set the initial value of the counter, for example, 5000.
C. How do I call this counter on a Web page? You can call it in the following ways:



Attached: complete list of programs
?
Header ("Content-type:image/gif");
Define output as Image type

$fp = fopen ("Zzm.txt", "R");
Open a document that records the number of previous visits in read form Zzm.txt

$str 1 = fgets ($fp, 10);
Reading 9 characters from a file, the maximum number of visitors to this counter is 999999999

$str 1++;
Counter Join

Fclose ($FP);
Close File

$fp = fopen ("Zzm.txt", "w");
Write a way to open a record of the number of visitors to the file Zzm.txt

Fputs ($fp, $str 1);
Write the latest number of visitors to the file

Fclose ($FP);
Close File

/*
The following is the format of the number of visitors to the output, if the number of visitors is not enough 9 digits, such as 5000 (4-bit),
The number of visitors will be converted to 000005000 of the form output. The method is to compute the number of digits of the visitor, and
Compare it to 000000000 digits (9 bits), get the difference in the number of digits, and then in front of the number is not photogenic
should be a 0. For example, 5000, and 2 of the length of the difference of 5, so to 5000 in front of 5 0.
*/

$len 1 = strlen ($str 1);
Count the number of digits visited

$str 2 = "000000000";
$len 2 = strlen ($STR);
Defines the maximum number of digits for a counter

$dif = $len 2-$len 1;
Calculates the difference between the digits of the two, that is, the number of 0 that is to be filled before

$rest = substr ($str 2, 0, $DIF);
Intercept 0 to fill.

$string = $rest. $str 1;
Front Patch 0

$font = 4;
Define font Size

$im = Imagecreate (88,31);
New Image

$black = Imagecolorallocate ($im, 0,0,0);
Define Black

$white = Imagecolorallocate ($im, 255,255,255);
Define White

Imagefill ($im, 0,0, $black);
Set the background of the counter to black

$PX = (Imagesx ($im) -8.3*strlen ($string))/2;
Calculates the horizontal coordinates at which the string begins to be written, based on the length of the string, to try to make the string horizontally

Imagestring ($im, 3, $px, 2, "E&j Counter", $white);
Write "E&j Counter" to the image

Imageline ($im, 1, $white);
Uniform Root horizontal line

Imagestring ($im, $font, $px, 15.5, $string, $white);
Number of written visits

Imagegif ($im);
Output the image into GIF format
Imagedestroy ($im);
Releasing the image
?>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.