Home > Backend Development > PHP Tutorial > Creation of PHP dynamic images_PHP tutorial

Creation of PHP dynamic images_PHP tutorial

WBOY
Release: 2016-07-21 16:09:48
Original
889 people have browsed it


As long as you install some third-party library files and have certain geometric knowledge, you can use PHP to create and process images. Creating dynamic images using PHP is quite easy. Below, the author will introduce in detail how to achieve it.

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. If you want to use Type 1 fonts in your images, you must install t1lib.

Before setting up the image creation environment, some preparations need to be done. First, install t1lib, then 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 and storing it in the 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 add the following 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, then execute the make install command, restart Apache and run phpinfo( ) to check whether the new settings have taken effect. Now, we can start the image creation work.

Depending on the version of the GD library file installed will determine whether you can create graphic files in GIF or PNG format. If you install gd-1.6 or a previous version, you can use GIF format files but cannot create PNG files. If you install a gd-1.6 or later version, you can create PNG files but cannot create GIF format files.

Creating a simple image also requires the use of many functions, which we will explain step by step.

In the following example, we will create an image file in PNG format. The following code is a header containing 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 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×250, 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. You need to first assign a name to this color using its RGB value using the ImageColorAllocate() function. The format of this function is 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, release the image handle and the memory occupied after the image is created:

ImagePNG($newImg);
ImageDestroy($newImg) ; ?>

In this way, the entire code to create an 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 access it with a browser, we will see a sky blue 250×250 PNG format image.

We can also use the image creation function to process images, such as making a larger image into a smaller image:

Suppose you have an image and want to crop it into a 35×35 size image. All you need to do is create a 35×35 blank image, create an image stream containing the original image, and then place a resized version of the 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 and save a new Variables for image height and width*/
$newWidth = 35;
$newHeight = 35;
/* Create a new blank image with given height and width*/
$newImg = ImageCreate( $newWidth,$newHeight);
/* Get data from the original larger image*/
$origImg = ImageCreateFromPNG("test.png");
/*Copy the resized image, Use ImageSX() and ImageSY() to get the size of the original image in X and Y */
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 save this small script as resized.php and then access it with a browser, you will see a 35×35 PNG format image.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/314414.htmlTechArticleAs long as you install some third-party library files and have certain geometric knowledge, you can use PHP to create and process images . Creating dynamic images using PHP is quite easy. Next...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template