PHP Tutorials. Experience Tips (bottom)

Source: Internet
Author: User
Tags format functions header install php variables php and variable valid
Tips | Tutorial PHP's 10 Advanced Tips (Next)

Vi. creation of dynamic images
As long as you install some Third-party library files and have some knowledge of geometry, you can use PHP to create and process images. In fact, this does not require a lot of geometry knowledge, because I did not graduate from college, still can use PHP to create images.

You need to install the GD library file before you use the basic image creation function. If you want to use JPEG-related image creation functions, you also need to install jpeg-6b, and if you want to use Type 1 fonts in the image, you must install T1lib.

Before you create an image creation environment, you need to do some preparation work. First, install the t1lib, then install the jpeg-6b, and then install the GD library file. Be sure to install it in the order given here, because jpeg-6b is used when compiling GD as a library, and if jpeg-6b is not installed, an error occurs at compile time.

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

--WITH-GD=[/PATH/TO/GD]

--WITH-JPEG-DIR=[/PATH/TO/JPEG-6B]

--with-t1lib=[/path/to/t1lib]

Execute the Make command when the addition is complete, and then execute the make install command. After you restart Apache, run phpinfo () to check to see if the new settings are in effect. You are now ready to start the image creation process.

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

Creating a simple image also requires a number of functions, which we will explain step-by-step.

In this example, we will create an image file in PNG format, which is a MIME type header containing the image created:

? Header ("Content-type:image/png");

Use Imagecreate () to create a variable that represents a blank image, which requires an image size parameter in pixels, in the form of Imagecreate (X_size, y_size). If you want to create an image of size 250x250, you can use the following statement:

$NEWIMG = Imagecreate (250,250);

Because the image is still blank, you might want to fill it with some color. However, you need to first use the Imagecolorallocate () function to specify a name for this color with its RGB value, which is in the form of imagecolorallocate ([image], [red], [green], [blue]). If you want to define sky-blue, you can use the following statement:

$skyblue = Imagecolorallocate ($newImg, 136,193,255);

Next, you need to use the Imagefill () function to populate the image with this color, and the Imagefill () function has several versions, such as Imagefillrectangle (), Imagefillpolygon (), and so on. For the sake of 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 the image is created, the image handle is released and the memory occupied:

Imagepng ($NEWIMG);

Imagedestroy ($NEWIMG);?>

This way, all the code to create the image looks like this:

? 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 this script file as skyblue.php and access it in a browser, you will see an image in the Azure 250x250 png format.

We can also use image creation functions to process images, such as making a larger image into a small image:

Suppose you have an image that you want to crop out a 35x35 size image from. All you need to do is create a blank image of the 35x35 size, create an image stream containing the original image, and then place a resized original image in a new, blank image.

The key function to accomplish this task is imagecopyresized (), which requires a format that looks like this: 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]).

? /* Send a header so that the browser knows what type of content the file contains.

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);

* * from the original larger image to get the data * *

$ORIGIMG = Imagecreatefrompng ("Test.png");

/* Copy resized image, use Imagesx (), Imagesy () to get the original image in the X, y size

Imagecopyresized ($NEWIMG, $ORIGIMG, 0,0,0,0, $newWidth, $newHeight, Imagesx ($ORIGIMG), Imagesy ($ORIGIMG));

/* Create desired image, free memory * *

Imagepng ($NEWIMG);

Imagedestroy ($NEWIMG);?>

If you save this little script as resized.php, and then access it in a browser, you will see a 35x35 size PNG format graphic.

Seven, user authentication based on PHP
If you want password protection on every script, you can use the header () statement, $PHP _auth_user, and $PHP_AUTH_PW to establish a Basic authentication scheme, and the usual 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 (authorized user) string is returned to the user in the head of the response.

2. When the browser receives this response, it pops up the dialog box requiring the user to enter a username/password.

3, the user entered a user name and password in the dialog box, click OK button to return the information to the server for authentication use.

4, if the user name and password is valid, the protected file will be open to the user, as long as the user is still using the file, the certification will always be valid.

A simple php script file can mimic the HTTP query/response system by sending a proper HTTP header to the user to generate an automatic display username/password dialog box, and PHP stores the information entered in the User Name/password dialog box in $php_auth_user and $php In _AUTH_PW, these two variables can be compared to user names/passwords stored in files such as text files, databases, and so on.

This example uses two hard-coded values for authentication, but the principle is the same regardless of where the username and password are placed.

?

/* 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 will be able to cause the dialog box to appear.

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)) {

/* There are values in the variables, check that they are correct.

if (($PHP _auth_user!= "Validname") | | ($PHP _auth_pw!= "Goodpassword")) {

/* If one of the username and password entered is incorrect, send a header that will be able to cause the dialog box to appear.

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 two values are correct, display successful information.

echo "<p>you ' re authorized!</p>";

}

}

?>

It should be noted that if you are using file-based protection, it does not guarantee the security of all files in the directory. It can protect most files, and if you think it protects all the files in a given directory, your understanding needs to change.

Eight, PHP and COM
If you like to take risks and run a CGI, ISAPI, or Apache version of PHP on Windows, you can access COM functions. Well, the detailed explanation of the work of COM to Microsoft and many large books, in order to have a simple understanding of the functionality of COM, the following is a small common script.

This section of PHP script starts Microsoft Word processing word on the back end, opens a new document, enters some text, saves the document, and closes word.

?

Establish an index to the new COM component

$word = new COM ("Word.Application") or Die ("Can ' t start word!");

Displays the version number of Word that is currently in use

echo "Loading Word, v. {$word->version}<br>";

Set its visibility to 0 (false), if you want it to open at 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 Temp directory

$word->documents[1]->saveas ("/windows/temp/comtest.doc");

Turn off connection to COM components

$word->quit ();

Display additional information on the screen

echo "Check for the file ...";

?>

If you have an intranet site where the data is stored in SQL Server, and the user needs the Excel format for the data, you can have PHP run the necessary SQL queries and format the output, and then use COM to open Excel and convert the data into Excel format data. The data is then stored on the user's desktop.

Nine, PHP and Java
Another interesting feature of PHP is that it can invoke methods in existing Java objects so that you can integrate PHP into java-based applications. This is useful if you want to promote PHP applications at work, and you get the result that "everything here is Java based." ”

To take advantage of this feature, you must have a JVM (Java Virtual machine) installed on your server. If you install a JDK from Sun, Kaffe, IBM, or Blackdown, you have the JVM installed.

When you configure PHP, you need to add the With-java section to the configuration file, and then modify a portion of the php.ini file, and the php.ini file will be modified primarily by adding the following:

[Java]

Java.library.path=/path/to/library

java.class.path=/classpath/

Extension_dir=/path/to/extensions

Extension=libphp_java.so

Note that the changes are related to your installation type and you need to read the Readme file in the Ext/java directory under the PHP installation directory to learn how to configure Java features.

Here is an example of how to create a PHP script for a new Java object that will access and display some Java properties on the display. It's just as interesting as the COM example, and it 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 very helpful to the development work, this kind of integration ability is the future that PHP accepts and grows key.

X. PHP and XML
PHP contains an optional XML extension that supports expat parsing, using XML-related functions in PHP to create an analytic program to handle valid XML documents. If you are using a 1.3.7 version or a higher version of the Apache software, you do not need to install additional library files, you need to do is to configure the PHP with-xml.

Like Java and COM, PHP support for XML is also interesting and very fast, if you know expat or libxml, please join in the development of this aspect.





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.