6. Creation of dynamic images
As long as you install some third-party library files and have certain geometric knowledge, you can use PHP to create and process images. In fact, this does not require much knowledge of geometry, because I have not graduated from college and can still create images using PHP.
Before using basic image creation functions, you need to install the GD library file. If you want to use the image creation functions related to JPEG, you also need to install jpeg-6b, and if you want to use Type 1 fonts in your images, you must install t1lib.
Before establishing the image creation environment, some preparations need to be done. First, install t1lib; secondly, install jpeg-6b, and then install the GD library file. When installing, 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 still need to reconfigure PHP. This is one of the reasons why you are glad to use 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 completing the addition, execute the make command, and then execute the make install command. After restarting Apache, run phpinfo() to check whether the new settings have taken effect. Now you can start the image creation work.
Depending on the version of the GD library file installed, you may or may not be able to create graphic files in GIF or PNG format. If gd-1.6 or previous versions are installed, you can use GIF format files but The PNG format cannot be created. If a version of gd-1.6 or later is installed, the PNG file can be created but the GIF format file cannot be created.
Creating a simple image also requires the use of many functions, which we will explain step by step.
In this example, we will create an image file in PNG format. The following code is a MIME type header that contains the created image:
header (" Content-type: image/png");
Use ImageCreate() to create a variable representing a blank image. This function requires the parameter of the image size in pixels. Its format is ImageCreate(x_size, y_size) . If you want to create an image of size 250 Color to fill it. However, you need to first assign a name to this color using its RGB value using the ImageColorAllocate() function, which has the format 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 this with this color For images, there are several versions of the ImageFill() function, such as ImageFillRectangle(), ImageFillPolygon(), etc. 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 the memory occupied:
ImagePNG($newImg);
ImageDestroy($newImg); ?>
In this way, the entire code to create the image 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 you save this script file as skyblue.php and browse When you access it with a browser, you will see a sky blue 250X250 PNG format image.
We can also use image creation functions to process images, such as making a larger image into a smaller image:
Suppose you have an image and want to crop a 35X35 image from it . All you need to do is create a 35x35 blank image, create an image stream containing the original image, and then place a resized original image 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]).
/* Send a header to let the browser know the content type contained in the file*/
header("Content-type: image/png");
/* Create variables to save the height and width of the new image*/
$newWidth = 35;
$newHeight = 35;
/* Create a variable for New blank image with fixed height and width*/
$newImg = ImageCreate($newWidth,$newHeight);
/* Get data from the original larger image*/
$origImg = ImageCreateFromPNG("test.png");
/*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 desired image and release memory*/
ImagePNG($newImg);
ImageDestroy($newImg); ?>
If you replace this short paragraph Save the script as resized.php, and then access it with a browser, you will see a 35X35 PNG format graphic.
7. PHP-based user authentication
If you want to perform password protection on each script, you can use the header() statement, $PHP_AUTH_USER and $PHP_AUTH_PW to establish a basic authentication scheme. Usually 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 (Indicates Authorized User) string is returned to the user in the response header.
2. After the browser receives this response, a dialog box pops up asking the user to enter a username/password.
3. The user enters a username and password in the dialog box, and clicks 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 authentication will remain valid.
A simple PHP script file can imitate the HTTP question/response system by sending the user an appropriate HTTP header that causes the automatic display of the username/password dialog box. PHP puts the user in the username/ The information entered in the password dialog box is stored in $PHP_AUTH_USER and $PHP_AUTH_PW. Using these two variables, you can compare it with the username/password stored in text files, databases, etc.
This example uses two hard-coded values for authentication, but the principle is the same no matter 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 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 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 a 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 both values are correct, display Success message*/
echo "
Youre authorized!
";