6. Creation of 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 doesn't require much geometry knowledge, as I haven't graduated from college and can still create images with PHP.
Before using basic image creation functions, 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 Type 1 fonts in your image, you must install t1lib.
Before creating an image creation environment, some preparations are needed. First, install t1lib; secondly, install jpeg-6b, and then install the GD library file. During installation, you must install it in the order given here, because jpeg-6b will be used when compiling GD as a library. If jpeg-6b is not installed, an error will occur during compilation.
After installing these three components, you also need to reconfigure PHP, which is one of the things you are grateful for using DSO to install PHP. Run make clean, and then 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, execute the make command, and then execute the make install command. After restarting Apache, run phpinfo() to check whether the new settings are effective. Now you can start image creation.
Depending on the version of the installed GD library file, you may or cannot create GIF or PNG format graphics files. If you are installed in gd-1.6 or previous versions, you can use GIF format files but cannot create PNG format. If you are installed in gd-1.6 or later versions, you can create PNG files but cannot create GIF format files.
Creating a simple image also requires many functions, which we will explain step by step.
In this example, we will create a PNG image file, and the following code is a header of the MIME type of the created image:
<? header ("Content-type: image/png");
Use ImageCreate() to create a variable representing a blank image. This function requires parameters of the image size in pixels, and its format is 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);
Since the image is still blank, you may 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. This function is in the format 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 fill the image with this color. There are several versions of the ImageFill() function, such as ImageFillRectangle(), ImageFillPolygon(), etc. For simplicity, we use the ImageFill() function through 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 the memory occupied:
ImagePNG($newImg);
ImageDestroy($newImg); ?>
In this way, all the codes for creating the image are 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 this script file as and access it with a browser, you will see an azure 250X250 PNG format image.
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 of 35X35 size. All you need to do is create a 35X35-sized blank image, create an image stream containing the original image, and then place the resized original image into the 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 Y],[new image Y]).
<? /* Send a header so that the browser knows the type of content contained in the file*/
header("Content-type: image/png");
/* Create variables that save the height and width of the new image*/
$newWidth = 35;
$newHeight = 35;
/* Create a new blank image of a given height and width*/
$newImg = ImageCreate($newWidth,$newHeight);
/* Get data from the original larger image*/
$origImg = ImageCreateFromPNG("");
/*Copy the resized image and use ImageSX() and ImageSY() to get the original image in X and Y size */
ImageCopyResized($newImg,$origImg,0,0,0,0,$newWidth,$newHeight,ImageSX($origImg),ImageSY($origImg));
/*Create the image you want to get and free up memory */
ImagePNG($newImg);
ImageDestroy($newImg); ?>
If you save this small script as and access it with a browser, you will see a 35X35-sized PNG format graph.
7. PHP-based user authentication
If you want to password protection on each script, you can use the header() statement, $PHP_AUTH_USER and $PHP_AUTH_PW to establish a basic authentication scheme. The usual server-based question/response order is as follows:
1. The user requests a file from the server. If this file is protected on the server, a 401 string (indicates an authorized user) is returned to the user at the head of the response.
2. After the browser receives this response, a dialog box that asks the user to enter the user name/password pops up.
3. The user enters a user name and password in the dialog box, and click the OK button 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. As long as the user is still using the file, the authentication will be valid.
A simple PHP script file can imitate the HTTP question/response system by sending an appropriate HTTP header that can cause the user to automatically display the username/password dialog box. PHP stores the information entered by the username/password dialog box in $PHP_AUTH_USER and $PHP_AUTH_PW. Using these two variables, it can be compared with the username/password stored in text files, databases and other files.
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 can cause the dialog 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 if they are correct*/
if (($PHP_AUTH_USER != "validname") || ($PHP_AUTH_PW != "goodpassword")) {
/* If one of the entered username and password is incorrect, send a header that can cause the dialog 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 both values are correct, the successful information will be displayed */
echo "<P>You're authorized!</p>";
}
}
?>
It should be noted 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, and if you think it can protect all files in a given directory, your understanding needs to change.
8. PHP and COM
If you are adventurous and run CGI, ISAPI, or Apache module versions of PHP on Windows, you can access COM's functions. Okay, let me leave the work of explaining COM in detail to Microsoft and many large-scale books. In order to simply understand the functions of COM, here is a small piece of common script.
This PHP script starts Microsoft's word processing Word on the backend, opens a new document, enters some text, saves the document, and closes Word.
<?
// Create an index pointing to the new COM component
$word = new COM("") or die("Can't start Word!");
// Show the version number of Word currently in use
echo "Loading Word, v. {$word->Version}<br>";
// Set its visibility to 0 (false), if you want it to be turned on 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 the new document
$word->Selection->TypeText("Testing 1-2-3...");
//Save the document in a temporary Windows directory
$word->Documents[1]->SaveAs("/Windows/temp/");
// Close the connection with the COM component
$word->Quit();
// Show other information on the screen
echo "Check for the file...";
?>
If you have an intranet website and the data is stored in SQL Server and the user needs the Excel format of this data, you can ask PHP to run the necessary SQL queries and format the output, then use COM to open Excel, convert the data into Excel format data, and then save the data on the user's desktop computer.
9. PHP and Java
Another interesting feature of PHP is that it can call methods in existing Java objects, allowing you to integrate PHP in Java-based applications. If you want to promote PHP applications at work, this feature is very useful, and the result you get is, "Everything here is based on Java."
To take advantage of this feature, you must have a JVM (Java Virtual Machine) installed on your server. If you are installing the JDK from Sun, Kaffe, IBM or Blackdown, you already have the JVM installed.
When configuring PHP, you need to add the with-java section to the configuration file and then modify a part of the file. The main thing to modify the file is to add the following content:
[Java]
=/path/to/library
=/classpath/
extension_dir=/path/to/extensions
extension=libphp_java.so
It should be noted that the modifications are related to your installation type. You need to read the README file in the ext/java directory in the PHP installation directory and learn how to configure Java functions.
Here is an example of how to create a new Java object in PHP script that will access and display some Java properties on the display. It's just as interesting as the COM example and should give us some inspiration.
<?
$system = new Java("");
echo "<P>Java version = " . $system->getProperty("") . "<br>";
echo "Java vendor = " . $system->getProperty("") . "</p>";
?>
If you have Java knowledge, it will be of great help to development work. This integration capability is the key to future PHP acceptance and growth.
10. PHP and XML
PHP contains an optional XML extension that supports Expat parsing. Using XML-related functions in PHP, you can create a parsing program to process valid XML documents. If you are using Apache software with version 1.3.7 or later, you don't need to install additional library files. All you need to do is configure with-xml in PHP.
Like Java and COM, the support for XML in PHP is also interesting and growing very quickly. If you know Expat or LibXML, please join this development.
Previous page123Read the full text