PHP Tutorials. Experience Tips (bottom) _php

Source: Internet
Author: User
Keywords tutorials images files one if PHP create install use with
Tags password protection
10 Advanced Tips for PHP (bottom)

Vi. creation of dynamic images
As long as you install some third-party library files and have some geometrical knowledge, you can use PHP to create and manipulate images. In fact, this does not require much geometrical knowledge, because I have not graduated from university and can still create images using PHP.

Before using the basic image creation function, you need to install the GD library file. If you want to use JPEG-related image creation functions, you also need to install JPEG-6B, if you want to use the Type 1 font in the image, you must install T1lib.

Before creating an image creation environment, you need to do some preparatory work. First, install T1lib, then install 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 no jpeg-6b is installed, errors will occur at compile time.

After installing these three components, you'll also need to reconfigure PHP, which is one of the places where you're thankful for using DSO to install PHP. Run make clean, and then add the following in your current configuration:




Execute the Make command after you finish adding and then execute the make install command. After restarting Apache, run Phpinfo () to check to see if the new settings are in effect. Now it's time to start the image creation work.

Depending on the version of the GD library file that you installed, you might or may not be able to create a GIF or PNG-formatted graphic file, If you are installing gd-1.6 or a previous version, you can use a GIF-formatted file, but you cannot create a PNG format, and if you install 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'll explain step-by-step.

In this example, we will create a PNG-formatted image file, the following code is a header containing the MIME type of the image you created:

Use Imagecreate () to create a variable that represents a blank image, which requires an image-size parameter in pixels, in the format imagecreate (X_size, y_size). If you want to create an image with a size of 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 formatted as imagecolorallocate ([image], [red], [green], [blue]). If you want to define azure, you can use the following statement:

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

Next, you need to fill this image with this color using the Imagefill () function, which has several versions of the Imagefill () function, 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 is occupied:

Imagepng ($NEWIMG);

Imagedestroy ($NEWIMG);?>

In this way, all the code for creating the image looks like this:

$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 a skyblue.php and access it with your browser, you will see a turquoise 250x250 image in PNG format.

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

Suppose you have an image from which you want to crop a 35x35-sized image. All you need to do is create a blank image of 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 complete this task is imagecopyresized (), which requires the format as follows: 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 that holds the height and width of the new image */

$newWidth = 35;

$newHeight = 35;

/* Create a new blank image of the given height and width */

$NEWIMG = Imagecreate ($newWidth, $newHeight);

/* Get data from the original larger image */

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

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

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 using your browser, you will see a 35x35-sized PNG format.

Seven, PHP-based user authentication
If you want password protection on every piece of 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 (the authorized user) string is returned to the user at the head of the response.

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

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

4, if the user name 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 remain valid.

A simple php script file by sending the user an appropriate question/response system that can mimic HTTP with an HTTP header that automatically displays the Username/password dialog box, PHP stores the information entered by the user in the Username/Password dialog box in $php_auth_user and $php _AUTH_PW, using these two variables, you can compare them with usernames/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 user name 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 cause the dialog box to appear */

Header (' Www-authenticate:basic realm= ' My Private Stuff ');

Header (' http/1.0 401 Unauthorized ');

Echo ' Authorization Required. ';


} else if ((Isset ($PHP _auth_user)) && (Isset ($PHP _auth_pw)) {

/* have values in variables to check if they are correct */

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

/* If one of the user names and passwords entered is incorrect, send a header that will cause the dialog box to appear */

Header (' Www-authenticate:basic realm= ' My Private Stuff ');

Header (' http/1.0 401 Unauthorized ');

Echo ' Authorization Required. ';


} else if (($PHP _auth_user = = "Validname") | | ($PHP _AUTH_PW = = "Goodpassword")) {

/* If two values are correct, display the successful message */

echo "

You ' re authorized!





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

Viii. php and COM
If you like to take risks and run CGI, ISAPI, or Apache module versions of PHP on Windows, you can access COM's functions. Well, the detailed explanation of COM's work to Microsoft and many large books, in order to be able to simply understand the function of COM, here is a small section of common script.

This section of PHP script launches Microsoft Word processing in the backend, 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 the word that is currently in use

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

Set its visibility to 0 (false), use 1 (true) if you want to make it open in the front-end

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

Save the document in the Windows Temp directory

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

To close a connection to a COM component

$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 of the data, you can have PHP run the necessary SQL queries and format the output, then use COM to open Excel and convert the data into Excel-formatted data. The data is then saved on the user's desktop.

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

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

When you configure PHP, you need to add the With-java section to the configuration file and then modify part of the php.ini file, and the php.ini file is modified primarily to add the following:





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 functionality.

Here is an example of how to create a new Java object for a PHP script 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 "

Java Version = ". $system->getproperty ("Java.version"). "

echo "Java vendor =". $system->getproperty ("Java.vendor"). "



If you have Java knowledge, it will be a great help to the development work, the ability of this integration is the future of PHP acceptance and growth of the key.

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

Like Java and COM, XML support in PHP is also interesting and growing quickly, if you know expat or libxml, join this development.
  • Related Article

    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: 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.