PHP has extremely powerful image processing capabilities, and it can easily and dynamically generate web images.
The following is a simple counter made using php.
1. General idea:
Record the number of previous visitors in a text file. When the web page is visited, open the file
and read the number of previous visitors from it, add 1 to get the latest number of visitors, and Format the number into a standard format, then call the image processing function to output the number into a picture, and then write the new visit number back to the file that records the number of visitors.
2. Function description used by the program:
A. Related file operations:
a. Open file:
Function prototype: int fopen(string filename, string mode);
Return result: If the file is opened successfully, the function returns File stream pointer, otherwise FALSE(0) is returned.
Parameter description:
string filename -- The name of the file to be opened must be in the form of a string.
For example, "zzm.txt", "..zzm.txt", etc. O String Mode -The way to open the file must be character form.
'r', read-only form, the file pointer points to the beginning of the file . out being out out out out out out out Over to Truncated to 0,
If the file does not exist, an attempt will be made to create the file. '' W+', readable and written, the file pointer points to the beginning of the file, cut the file length into 0,
If the file does not exist, try to establish a file.
'a', append form (can only be written), the file pointer points to the end of the file, if the file
does not exist, an attempt will be made to create the file.
'a+', readable and writable, the file pointer points to the end of the file, if the file does not exist,
will try to create the file.
Example: Open "zzm.txt" under the current directory in read-only form
$fp = fopen("zzm.txt", "r");
b. Close the file:
Function prototype: int fclose(int fp) ;
Return result: 1 is returned on success, 0 is returned on failure
Parameter description: int fp is the file stream pointer returned by the fopen function.
Example: Close the zzm.txt file just opened with fopen
fclose($fp);
c. Read the file:
Function prototype: string fgets(int fp, int length);
Return result: return length -1 length string, if it reaches the end of the file, return EOF (End Of File)
Parameter description:
int fp – the file stream pointer to read the data, the value returned by the fopen function
int length – the number of characters read Number, the actual number of characters read is length -1
Example: Read 9 characters from $fp
$str1 = fgets($fp,10);
d. Write file:
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 -- to write The string entered into the file.T int length -the length of the writing, optional, if not provides a length, the entire string will be written, otherwise, write the length of the length.
Example: Write "0000000001" to $fp
fput($fp, "0000000001");
B. Related string functions:
a. Calculate string length:
Function prototype: int strlen(string str);
Return result: Return the length of the string
Parameter description:
String str -- the string whose length is to be calculated
Example: Calculate the string length of "000000000"
$str2 = "000000000";
$len2 = strlen($ str);
b. Adding strings: It’s the simplest, use a . to connect two strings.
Example: Add $str1 and $str2
$str = $str1.$str2
C. Related graphics functions:
a. Create a new image:
Function prototype: int imagecreate(int x_size, int y_size);
Return Result: Return an empty image identification number (ImageID) with size of Image
$ImageID = imagecreate(88, 31);
b. Assign a color to the image:
Function prototype: int imagecolorallocate(int im, int red, int green, int blue); Return result: Image im) returns an RGB color identification number
Parameter description: int im image identification number
’ s ’ s ’ s ’ s ’ s ’ ’’ ’ ’ s ’ ’ s ’ ’ s ’ ’ s 1 -- m m- rg The identification number is $white white color, and the white RGB is (255,255,255)
, int x, int y, int col);
Return result: Return 1 successfully, otherwise return 0
Parameter description: int im, the identification number of the image
Int x, int y, start filling the color from the (x, y) coordinates of the image
(0,0) represents the upper left corner of the image
int col, the identification number of the color
Example: starting from the upper left corner of the image (i.e. the entire picture) fill in black (the imagecolorallocate function
has been used to define the color identification number of black as $black).
Imagefill($im, 0, 0, $black);
d. Calculate the width of the image:
Function prototype: int imagesx(int im);
Return result: Return the width of the image (unit is pixel)
Parameter description: int im, the identification number of the image.
Example: Calculate the width of image $im
$px = imagesx($im);
e. Write horizontal text in the image:
Function prototype: int imagestring(int im, int font, int x, int y, string s, int col)
Return result: Return 1 if successful, otherwise return 0
Parameter description: int im, image identification number
Int font, font identification number, built-in fonts 1 to 5, users can use imageloadfont() themselves?
??? , String S, string to write
int color, color recognition number of the font
Example: In the image (3,3) position, the font size is 3, and the color is white The color identification number is $white)'s string "E&J Counter"
‐ ‐ ‐ ‐ ‐ ‐ imageString($im, 3, 3, 3, "E&J Counter", $white);
f. Draw a straight line in the image:
Function prototype: int imageline(int im, int x1, int y1, int x2, int y2, int col);
Return result: Return 1 successfully, otherwise return 0
Parameter description: int im, the identification number of the image
Int x1, int y1, The starting coordinate of the line is int x2, int y2, the ending coordinate of the line is int col, and the color identification number of the line is
Example: Draw a line from (1,14) to (85,14) in the image $im with the color $white’s straight line
imageline($im, 1, 14, 85, 14, $white); Output the image into GIF format: ‐ out out ‐ ‐ ‐‐‐‐ �� � � �
� � Function prototype: int imagegif(int im, string filename); �
� Return result: Returns 1 successfully, otherwise returns 0
Parameter description: int im, image identification number
String filename, generate the name of the picture, optional, if filename is empty, directly?
氖涑?
Picture, you need to use Header ("Content-type: image/gif") Predefine the content output by php as a picture
Example: Output the image $im into a picture with the file name "image1.gif"
Imagegif($im, "image1.gif" ");
h. Release the image:
Function prototype: int imagedestroy(int im);
Return result: Return 1 if successful, otherwise return 0
Parameter description: int im, the image identification number to be released. This function will release the image of the identification number im and the system resources occupied by the image.
Example: Release image $im
Imagedestroy($im);
3. How to install this counter:
A. The system must have a PHP interpreter installed. PHP can be downloaded at http://www.php.net/. There is also a lot of detailed technical information on this site that can be browsed or downloaded for reading. Please refer to its own instructions for how to install PHP.
B. Copy the following program list into a file with the extension php, put it into a directory that can run php scripts?
Lvqiu?
And create a plain text file under the directory with the name zzm.txt .The purpose of this file is to record the number of visitors in the past. You can preset the initial value of the counter, for example 5000.
C. How to call this counter on the web page? You can call it in the following way:
Attachment: The complete program list
Header("Content-type: image/gif");
/ /Define the output as image type
$fp = fopen("zzm.txt", "r");
//Open the file zzm.txt that records the number of previous visitors in read form
$str1 = fgets($fp,10) ; 文 // Read 9 characters from the file. The maximum number of visits recorded in this counting machine is 999999999
$ STR1 ++;
// counter adding
fclose ($ fp); "zzm.txt", "w");
//Open the file zzm.txt that records the number of visitors by writing
fputs($fp, $str1);
//Write the latest number of visitors to the file
fclose ($fp);
//Close the file
/*
The following is the formatted output of the number of visitors. If the number of visitors is less than 9 digits, such as 5000 (4 digits),
Then convert the number of visitors into the form of 000005000 output. The method is to calculate the number of digits in the number of visitors, and compare it with the number of digits in 000000000 (9 digits) to get the difference in digits, and then add a 0 in front of the number. For example, the lengths of 5000 and 000000000 differ by 5, so five zeros must be added in front of 5000.The maximum number of counting digits
$dif = $len2 - $len1;
//Calculate the difference between the two digits, that is, the number of 0s to be added in front
$rest = substr($str2, 0, $dif);
//Interception 0 to be added
$string = $rest.$str1;
//Add 0 in front $font = 4;
//Define font size
$im = imagecreate(88,31);
//New image
$ black = ImageColorAllocate($im, 0,0,0);
//Define black , 0,0,$black);
//Set the background color of the counter to black
$px = (imagesx($im)-8.3*strlen($string))/2;
//Calculate the level at which the string starts to be written based on the length of the string coordinates, the purpose is to try to center the string horizontally
ImageString($im,3,$px,2,"E&J Counter",$white);
//Write "E&J Counter" to the image
imageline($im , 1, 14, 85, 14, $white);
//Draw a horizontal line
ImageString($im,$font,$px,15.5,$string,$white);
//Write the number of visitors
ImageGif($ IM); 把 // output the image gif format
ImageDestroy ($ im);
// Release the image
? & gt;
The above introduces the decimal counter. A simple counter written in php3, including the content of the decimal counter. I hope it will be helpful to friends who are interested in PHP tutorials.