Ten advanced skills in PHP (below)
Sixth, the creation of moving images As long as you install some third-party library files and have certain geometries, you can use PHP to create and process images. In fact, this doesn't need too much geometries, because our university does not graduate, still can use PHP to create images. You need to install the GD library file before using the basic image creation function. If you want to create a function related to JPEG, you will need to install JPEG-6B. If you want to use the Type 1 type font in the image, you must install T1LIB. Before establishing an image creation environment, you need some preparations. First, install T1LIB; secondly install JPEG-6B, then install the GD library file. Be sure to install in the order here, because JPEG-6B is used when compiling GDs is used, and if JPEG-6B is installed, it will be wrong when compiling. After installing these three components, you also need to reconfigure PHP, which is one of you who is featured by using DSO way to install PHP. Run Make Clean, then add the following content in the current configuration: --with-gd = [/ path / to / gd] --with-jpeg-dir = [/ patH / to / jpeg-6b] --with -t1lib = [/ path / to / t1lib] After the addition is added, execute the make command, then execute the make install command. After restarting apache, run PHPINFO () to check if the new settings have taken effect. You can start the image creation work now. Depending on the version of the installed GD library file, you may or cannot create a graphic file in GIF or PNG format. If you install GD-1.6 or previous version, you can use the GIF format file but cannot create a PNG format, if Installing the version after GD-1.6, you can create a PNG file but cannot create a file in GIF format. Creating a simple image also requires many functions, we will explain step by step. In this example, we will create an image file in a PNG format. The following code is a header that contains the MIME type of the image created: Header ("Content-Type: Image / PNG"); use imagecreate Create a variable representing a blank image, which requires the image size of the image in pixels, which is imagecreate (x_size, y_size). If you want to create an image size of 250x250, you can use the following statement: $ newimg = imagecreate (250, 250); because the image is still blank, you may want to fill it with some colors. However, you need to first use the ImageColoralLocate () function to specify a name with its RGB value for this color, the format of this function is imagecolorallocate ([Image], [Red], [Green], [Blue]). If you want to define the sky, you can use the following statement: $ skyblue = ImageColoralLocate ($ Newimg, 136, 193, 255); Next, you need to use the imagefill () function to populate this image with this color, and the imagefill () function has several versions, for example ImageFillRectangle (), ImageFillPolygon (), etc.
For simplicity, we use imagefill () functions through the following format: ImageFill ([Image], [Start X Point], [Start Y Point], [Color]) ImageFill ($ Newimg, 0,0, $ SkyBlue); Finally, establish an image to release the image handle and the memory: ImagePng ($ newimg); ImageDestroy; "This, all code to create images 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 this script file is saved as SkyBlue.php, you will see an image of a blue 250x250 PNG format with a browser. We can also use image creation functions to process images, such as making a larger image into a small image: assuming that you have an image, you want to cut out a 35x35 size image from it. What you need is to create a blank image of a 35x35 size, create an image stream containing the original image, and then put a resized original image in a new blank image. To accomplish this task is imageCopyResized (), which requires the format as follows: ImageCopyResize, [Original Image Handle], [New Image X], [New Image Y], [Original Image x], [Original Image Y], [New Image Y], [Original Image X], [Original Image Y]) / * Send a head to let the browser know the file The content type included * / header ("content-type: image / png"); / * Establish a variable for saving new image height and width * / $ newwidth = 35; $ newHeight = 35; / * Establish a given height and Width new blank image * / $ newimg = imagecreate ($ newwidth, $ newheight); / * Get data from the original image * / $ igimg = ImageCreateFromPng ("Test.png"); / * Copy adjustment after the image using ImageSX (), ImageSY () to obtain the original image in the X, Y-size in terms of * / ImageCopyResized ($ newImg, $ origImg, 0,0,0,0, $ newWidth, $ newHeight, ImageSX ( $ Origimg), ImageeSy ($ Origimg)); / * Create a desired image, release memory * / imagepng ($ newimg); ImageDestroy ($ newimg);> If this short script is saved as resized.php, then Use the browser to access it, you will see graphics of a 35x35 size PNG format.
Seven, PHP-based user authentication If you want to make password protection on each script, you can use the Header () statement, $ PHP_AUTH_USER, and $ PHP_AUTH_PW to establish basic authentication schemes, and the usual server-based question / response order is as follows As shown: 1. The user requests a file from the server. If this file is protected on the server, a 401 (emptive user) string is returned to the user in the header of the response. 2. After the browser receives this response, the dialog box that requires the user to enter the username / password. 3. Users Enter a username and password in the dialog, and click the OK button to return the information to the server for authentication. 4. If the username and password are valid, the protected file will be open to the user, as long as the user is still using the file, the certification will be effective. A simple PHP script file can simulate the HTTP's question / response system by sending an appropriate HTTP header that can cause the automatic display user name / password dialog box, PHP inputs the user in the User Name / Password dialog box. Information is stored in $ PHP_AUTH_USER and $ PHP_AUTH_PW, using these two variables, you can compare with usernames / password stored in files such as text files, databases. This example uses two hard-coded values for authentication, but no matter where the username and password are placed, the principles are the same. / * Check the value of $ PHP_AUTH_USER and $ PHP_AUTH_PW * / if (($ PHP_AUTH_USER)) || (iSSet ($ PHP_AUTH_PW))) {/ * If there is no value, send a dialog box The head * / head ('www-automate: Basic realm = "my private stuff"); Header (' http / 1.0 401 unauthorized "); Echo 'Authorization Required.'; EXIT;} else f ((iSset) ($ PHP_AUTH_USER)) && ($ PHP_AUTH_PW)))) {/ * Value in the variable, check if they correct * / if (($ PHP_AUTH_USER! = "ValidName") || ($ PHP_AUTH_PW! = "Goodpassword") ) {/ * If there is an incorrect in the input username and password, send a header * / header ('WWW-Authenticate: Basic Realm = "My Private Stuff") that can be thrown the dialog box; Header (' HTTP / 1.0 401 unauthorized '); Echo' Authorization Required. '; Exit;} else IF ($ PHP_AUTH_USER == "ValidName") || ($ PHP_AUTH_PW == "GoodPassword")) {/ * If two values Correct, displayed information * / echo "
you're authorized! P>";}}?> Need to note that if you use a file-based protection mechanism, it does not guarantee the directory All the security of all files. It can protect most of the files, if you think it can protect all files in a given directory, your understanding needs to be changed.
Eight, PHP and COM If you like to take risks, and in Windows, you can access the function of the CGI, ISAPI, or Apache module version. Ok, explain in detail the work of COM and give Microsoft and many most books. In order to simply understand the COM's function, the following is a common script. This PHP script starts Microsoft word processing Word on the backend, open a new document, enter some text, save the document, and close the word. // Establish an index pointing to the new COM component $ Word = New Com ("Word.Application") or Die ("Can't Start Word!"); // Shows the version number echo of the Word currently in use Loading Word, V. {$ Word-> Version}
"; // Set its visibility to 0 (fake), if you want to open it on the front end, use 1 (true) // to open the Application In the Forefront, Use 1 (TRUE) $ WORD-> Visible = 0; // Create a new document in Word $ Word-> Documents-> add (); // Add text to a new document $ Word-> Selection-> Typetext ("Testing 1-2-3 ..."); // Save the document in the Windows Temporary Directory $ Word-> Documents [1] -> Saves ("/ Windows / Temp / Comtest.doc" ); // Close the connection between the COM components $ Word-> quit (); // Display additional information on the screen Echo "Check for the file ...";> If you have an intranet website, data storage In SQL Server, the user needs these data's Excel format, you can make PHP run the necessary SQL query and format the output, then use COM to turn the Excel, turn the data into the Excel format, then save the data in the user The desktop. Nine, PHP and Java PHP Another interesting feature is that it can call the method in the existing Java object so that you can integrate PHP in Java-based applications. If you want to promote PHP applications in your work, this feature is very useful. The result you get is that "everything here is based on Java." To take this feature, you must have JVM on your server ( Java virtual machine). If you installed the JDK from Sun, Kaffe, IBM, or BlackDown, JVM has been installed. When configuring PHP, you need to add a WITH-JAVA section in the configuration file, then modify part of the php.ini file, the modification of the php.ini file is mainly needed to add the following: [java] java.library.path = / path / to / library java.class.path = / classpath / extension_dir = / path / to / extensions extension = libphp_java.so Need to note that the modification made is related to your installation type, you need to read the PHP installation directory Readme file in the ext / java directory, learn how to configure Java features.