Home >Backend Development >PHP Tutorial >'Draw' Chinese using the point tracing method in PHP_PHP Tutorial

'Draw' Chinese using the point tracing method in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-15 13:22:571015browse

Foreword:
Now, more and more people like to go online, More and more people have their own personal homepage. With the emergence of various automated software tools, making web pages is becoming easier and easier. However, since special effects are widely available, there are fewer and fewer innovative things. Maybe, one day, you will find that the counter on a website is exactly the same as your own. The web page is becoming more and more mature. There are more and more things on the web page, and they are becoming richer and richer.
Question:

  • On a web page, if I want to add a counter:
    In the past, I would go to the space provider and ask for a link, or copy the address elsewhere. But these are always done by others. You don't have much say in whether they are good or not. You can only find them one by one.
  • On the web page, I want to publish some information:
    If the information is text, create a new page and add a link;
    is data, create a new page and add a link:
    But if these data are updated frequently, and may even change every hour or every minute, are you willing to stay in front of the computer, constantly modifying and uploading? (We are not a commercial website, no one is willing to spend money for you.)
    And message boards, chat rooms, forums, these cannot be done by HTML and JAVASCRIPT alone.

In order to achieve more automatic control, you can use CGI (Common Gateway Interface) programs to implement these functions.
Software requirements:
PHP: GD Library
Configure a server that supports PHP. I use OmniHTTPd Professional

For counters and real-time data statistics and publishing, we can use pictures to complete it. Output text in the picture.
In PHP, to create an image and display some content on it, the basic steps are as follows:

//http header, tell the browser that this is a GIF image
header ("Content-type: image/gif");
// To draw, you need to have floral fabric first, right? Create a 400×300 palette image
$im = imagecreate (400, 300);
$black = imagecolorallocate ($im, 0, 0, 0);
// Default black background.
//(Default refers to the first defined color. If another color is defined before this line of code, then the first defined color is the default background color.)
$red = imagecolorallocate ($im, 255, 0, 0);
//Red. If these two lines are swapped, you'll find that the background is red and the text is black.
$string="1234567890";
//Character to be drawn
imagestring ($im,12,10,10,$string,$red);
//In (10, 10 ) Start drawing string
imagepng ($im);
// Output in png format, you can also use imagejpeg($im); or magegif($im); but the latter, if the GD version is higher than 1.6 , it can no longer be used.
imagedestroy ($im);
//End, clear all occupied memory resources
?>


The above example, on a 400×300 picture, from point (10, 10 ), draw "1234567890" at 12 points. Did you notice that the size of this image is: 251 bytes! You can also try other output formats.
The size of the picture is related to the number of non-background pixels in the picture, and has nothing to do with the number of pixels output.

However, there is a problem.
You can use imagestring() to output the following information:
imagestring($im,1,0,0,"abcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+{}|:"?[]';,./",$red);
However, you cannot output Chinese correctly!!!
imagestring($im,1,0,0,"Ah",$red);
What you see is definitely garbled characters.
The default character set of PHP is GB2312.

How to solve it? To solve this problem, you can let PHP load the extension module php_iconv.dll (the suffix under UNIT is .SO). However, sometimes it may not work properly. Originally, I was going to put a piece of test code, but this time, No matter what I do, I still don’t put them up.

But the most fatal thing is if your space service provider turns off the extension module, or even prohibits loading the module’s DL ( ) function, then you can only use Chinese BYE-BYE.
Fortunately, there are other ways.
You can output the characters in the pre-converted code table. Need a code chart!
Or, how about drawing every dot in Chinese!

Come on, let’s draw together! To write words, you first need to know how to draw them.

Have you learned about simple functions in junior high school? Have you ever tried to draw a graph of a function? Calculate the coordinates of a certain point and then connect two adjacent points. This method is called drawing points. Method.

What we have to do is to calculate as many points as possible and then display them in the corresponding coordinates.

Have you ever heard of dot matrix printers and dot matrix Chinese characters? They are represented by dots

.

The function to display a point of a certain color at a certain coordinate is:
int imagesetpixel (resource image, int x, int y, int color)
Suppose I want to display at the coordinate (100,100) A white dot, then, just need the following code:

header ("Content-type: image/gif");
$image = imagecreate (400, 300);
$black = imagecolorallocate ($image, 0, 0, 0);
$white = imagecolorallocate ($image, 255, 255, 255); // Define white
imagesetpixel ($image, 100, 100, $white);
imagepng ($image);
imagedestroy ($image);
?>


In other words, we only need to obtain the information of all points of a certain Chinese character , we can use this function to output that Chinese character.

In the file chs16.fon, what is saved is the national standard location code table (National Standard Chinese Character Encoding Basic Character Set for Information Exchange GB-2312). It is a dot matrix font library for Chinese characters. (In WIN98 system, this file is under c:windowscommand. If you want to use it under UNIX system, please pay attention to the capitalization. If not, you can find the link at the end of the article.)
It is from the MSDOS era, However, good things should still be taken out and used.

From chs16.fon, we can read the dot matrix data of Chinese characters. Each Chinese character is composed of 16×16 dots. Where the stroke passes, the value of the point is 1, otherwise it is 0; each point occupies one bit, and every 8 points constitute a byte. Then, a Chinese character requires (16×16÷8=32) bytes.

The following example is to illustrate the representation method of character dot matrix.
Here, an 8×8 matrix is ​​defined, showing a letter C. The white square is represented by 0 and the black square is represented by 1. Then, the codes of these eight lines of graphics are:


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446925.htmlTechArticleForeword: Now, more and more people like to surf the Internet, and more and more people have their own Personal homepage. With the emergence of various automated software tools, making web pages is becoming easier and easier. But...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn