Advanced PHP programming skills in Linux (3)

Source: Internet
Author: User
Tags password protection
6. to create dynamic images, you only need to 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. Tips for using basic images to create Linux


   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 = [/path/to/gd]
-- With-jpeg-dir = [/path/to/jpeg-6b]
-- With-t1lib = [/path/to/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:
  
     
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 ([image], [red], [green], [blue]). 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 ([image], [start x point], [start y point], [color])
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:
   $ 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 ([new image handle], [original image handle], [new image X], [new Image Y], [original image X], [original image Y], [new image X], [new image Y], [original image X], [original image Y])
  
   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 text files, databases, 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"

You're authorized!

";
}
}
?>
  
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}
";
// 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 [1]-> 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

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.