Ten advanced PHP skills (Part 2)

Source: Internet
Author: User
Tags password protection
6. Create dynamic images
As long as you install some third-party library files and have some geometric knowledge, you can use PHP to create and process images. In fact, this does not require much geometric knowledge, because I have not graduated from college and can still use PHP to create images.

Install the GD library file before using basic image creation functions. If you want to create a function using JPEG-related images, you also need to install the jpeg-6b, and if you want to use the type 1 Font in the image, you must install t1lib.

Before creating an image creation environment, make some preparations. First, install t1lib; then install the jpeg-6b, and then install the GD library file. Be sure to install it in the order given here, because the jpeg-6b is used when compiling GD as the Library, and errors occur during compilation if the jpeg-6b is not installed.

After installing these three components, you also need to reconfigure PHP, which is also one of the places you are glad to install PHP using DSO. Run make clean and add the following content to the current configuration:

-- With-Gd =
-- With-JPEG-Dir =
-- With-t1lib =
After adding the package, run the make command, and then run the make install command. Restart Apache and run phpinfo () to check whether the new settings have taken effect. Now you can start image creation.

Depending on the version of the installed GD library file, you may or may not be able to create GIF or PNG format graphics files if installed with a gd-1.6 or a previous version, you can use a file in GIF format, but you cannot create a PNG file. If you are installing a version later than the gd-1.6, you can create a PNG file but not a file in GIF format.

Many functions are required to create a simple image.

In this example, we will create an image file in PNG format. The following code is a header containing the MIME type of the created image:

<? Header ("Content-Type: image/PNG ");

Use imagecreate () to create a variable that represents a blank image. This function requires a parameter of the image size in pixels. The format is imagecreate (x_size, y_size ). To create an image of X in size, use the following statement:

$ Newimg = imagecreate (250,250 );

Because the image is still blank, you may want to fill it with some color. However, you must first use the imagecolorallocate () function to specify a name for this color with its RGB value. The format of this function is imagecolorallocate (,,,). To define the sky blue, use the following statement:

$ Skyblue = imagecolorallocate ($ newimg, 136,193,255 );

Next, you need to use the imagefill () function to fill the image with this color. The imagefill () function has several versions, such as imagefillrectangle () and imagefillpolygon. For simplicity, we use the imagefill () function in the following format:

Imagefill (,,,)
Imagefill ($ newimg, 0, 0, $ skyblue );

Finally, after creating the image, release the image handle and occupied memory:

Imagepng ($ newimg );
Imagedestroy ($ newimg);?>

The code for creating an image is as follows:

<? Header ("Content-Type: image/PNG ");
$ Newimg = imagecreate (250,250 );
$ Skyblue = imagecolorallocate ($ newimg, 136,193,255 );
Imagefill ($ newimg, 0, 0, $ skyblue );
Imagepng ($ newimg );
Imagedestroy ($ newimg );
?>

If you save the script file as skyblue. php and access it with a browser, you will see an image in PNG format of the sky blue 250x250.

We can also use the image creation function to process the image. For example, we can make a large image into a small image:
Suppose you have an image and want to crop A 35x35 image. What you need to do is to create a 35x35 blank image, create an image stream containing the original image, and then put the original image after the adjusted size into the new blank image.
The key function to complete this task is imagecopyresized (), which requires the following format:

Imagecopyresized (,
,
,
,
,
,
,,
,
)

<? /* Send a header to let the browser know the content type contained in the file */
Header ("Content-Type: image/PNG ");
/* Create a variable to save the height and width of the new image */
$ Newwidth = 35;
$ Newheight = 35;
/* Create a new blank image with a given height and width */
$ Newimg = imagecreate ($ newwidth, $ newheight );
/* Obtain data from the original large image */
$ Origimg = imagecreatefrompng ("test.png ");
/* Copy the adjusted image and use imagesx () and imagesy () to obtain the size of the original image in terms of x and y */
Imagecopyresized ($ newimg, $ origimg, 0, 0, 0, $ newwidth, $ newheight, imagesx ($ origimg), imagesy ($ origimg ));
/* Create the desired image and release the memory */
Imagepng ($ newimg );
Imagedestroy ($ newimg);?>

If you save this short script as resized. php and access it using a browser, you will see a 35x35 PNG image.

7. PHP-based user authentication

If you want to implement password protection on each script, you can use the header () Statement, $ php_auth_user, and $ php_auth_pw to establish a basic authentication scheme, generally, the server-based question/response sequence is as follows:

1. the user requests a file from the server. If the file is protected on the server, a 401 (indicating authorized users) string is returned to the user in the response header.
2. After the browser receives the response, a dialog box asking the user to enter the user name/password is displayed.
3. Enter a user name and password in the dialog box and click OK to return the information to the server for authentication.
4. If the user name and password are valid, the protected file will be open to the user. Authentication will always be valid as long as the user is still using the file.

A simple PHP script file sends an appropriate HTTP header that can automatically display the user name/Password dialog box to imitate the HTTP question/response system, PHP stores the user input information in the username/password dialog box in $ php_auth_user and $ php_auth_pw. These two variables are used, it can be compared with the user name/password stored in the document, database, and other files.

In this example, two hard-coded values are used for authentication. However, no matter where the user name and password are stored, the principles are the same.

<?
/* Check the values in $ php_auth_user and $ php_auth_pw */
If ((! Isset ($ php_auth_user) | (! Isset ($ php_auth_pw ))){
/* If there is no value, send a header that can trigger the dialog box */
Header (@ # www-Authenticate: Basic realm = "my private stuff "@#);
Header (@ # HTTP/1.0 401 unauthorized @#);
Echo @ # authorization required .@#;
Exit;
} Else if (isset ($ php_auth_user) & (isset ($ php_auth_pw ))){
/* Check whether the variables have values */
If ($ php_auth_user! = "Validname") | ($ php_auth_pw! = "Goodpassword ")){
/* If the entered username and password are incorrect, send a header that can be displayed in the dialog box */
Header (@ # www-Authenticate: Basic realm = "my private stuff "@#);
Header (@ # HTTP/1.0 401 unauthorized @#);
Echo @ # authorization required .@#;
Exit;
} Else if ($ php_auth_user = "validname") | ($ php_auth_pw = "goodpassword ")){
/* If both values are correct, the information displayed successfully */
Echo "<p> You @ # Re authorized! </P> ";
}
}
?>

Note that if you are using a file-based protection mechanism, it does not guarantee the security of all files in the directory. It may protect most files. If you think it can protect all files in a given directory, you need to change your understanding.

8. php and COM
If you are adventurous and run CGI, ISAPI, or Apache module version PHP on Windows, you can access COM functions. Well, I will give a detailed explanation of COM work to Microsoft and many big part books. In order to get a simple understanding of COM functions, the following is a short common script.

This PHP script starts Microsoft Word processing on the backend, opens a new document, enters some text, saves the document, and closes the word.
<?
// Create an index pointing to the new COM Component
$ Word = new COM ("word. application") or die ("Can @ # t start word! ");
// Display the version number of the word currently in use
Echo "loading word, V. {$ word-> Version} <br> ";
// Set its visibility to 0 (false). If you want to enable it at the frontend, use 1 (true)
// To open the application in the forefront, use 1 (true)
$ Word-> visible = 0;
// Create a document in Word
$ Word-> documents-> Add ();
// Add text to the new document
$ Word-> selection-> typetext ("testing 1-2-3 ...");
// Save the document in the temporary windows directory
$ Word-> documents-> saveas ("/Windows/temp/comtest.doc ");
// Close the connection with COM components
$ Word-> quit ();
// Display other information on the screen
Echo "check for the file ...";
?>
If you have an Intranet website where data is stored in SQL Server and you need the Excel format of the data, you can run the necessary SQL query in PHP and format the output, then, use com to open excel, convert the data into Excel format, and save the data on your desktop.

9. php and Java
Another interesting function of PHP is that it can call methods in existing Java objects so that you can integrate PHP in Java-based applications. If you want to promote PHP applications at work, this function is very useful. The result you get is, "everything here is based on Java ."
To use this function, you must have a JVM (Java VM) installed on your server ). If the JDK is installed by Sun, kaffe, IBM, or Blackdown, the JVM is installed.

When configuring PHP, add the with-Java section to the configuration file, and then modify a part of the php. ini file. The following content must be added to modify the php. ini file:

  
Java. Library. Path =/path/to/Library
Java. Class. Path =/classpath/
Extension_dir =/path/to/extensions
Extension = libphp_java.so

Note that the modification is related to your installation type. You need to read the README file in the EXT/Java directory of the PHP installation directory and learn how to configure the Java function.

The following is an example of how to create a PHP script for a new Java object. This script will access and display some java attributes on the display. It is as interesting as the example of COM and should give us some inspiration.
<?
$ System = new Java ("Java. Lang. system ");
Echo "<p> JAVA version =". $ system-> getproperty ("Java. Version"). "<br> ";
Echo "Java vendor =". $ system-> getproperty ("Java. Vendor"). "</P> ";
?>
If you have Java knowledge, it will be of great help to the development work. This integration capability is critical to the acceptance and growth of PHP in the future.

10. php and XML
PHP contains an optional XML extension that supports expat parsing. Using XML-related functions in PHP, you can create an analytic program to process valid XML documents. If you are using apache version 1.3.7 or later, you do not need to install additional library files. All you need to do is configure with-XML in PHP.

Like java and COM, XML support in PHP is also very interesting and growing rapidly. If you know expat or libxml, please join in development in this area.

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.