Creating a dynamic image _php tutorial with PHP

Source: Internet
Author: User
As long as you install some third-party library files and have some geometrical knowledge, you can use PHP to create and manipulate images. Creating dynamic images with PHP is a pretty easy thing to do. Below, I will explain in detail how to implement.

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 the GD storage, and if no jpeg-6b is installed, errors will occur at compile time.

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

--WITH-GD=[/PATH/TO/GD]
--WITH-JPEG-DIR=[/PATH/TO/JPEG-6B]
--with-t1lib=[/path/to/t1lib]

After you finish adding the Make command and then execute the make install command, restart Apache and run Phpinfo () to check that the new settings are in effect. Now we can start the image creation work.

Depending on the version of the GD library file that you installed, you will be able to create a graphics file in GIF or PNG format. 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 the following example, we will create a PNG-formatted image file, 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 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. 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 established, release the image handle and the memory that is occupied:

Imagepng ($NEWIMG);
Imagedestroy ($NEWIMG);? >

In this way, all the code for creating 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 with a browser, we 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 following format:

Imagecopyresized ([New Image handle],[original image handle],[new image x], [New Image Y], [original image x], [Original IM Age Y], [new Image X], [New Image y], [original image x], [original image y]).
? /* Send a header to let the browser know what type of content The file contains */
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 an image in PNG format of 35x35 size.

http://www.bkjia.com/PHPjc/314700.html www.bkjia.com true http://www.bkjia.com/PHPjc/314700.html techarticle as long as you install some third-party library files and have some geometrical knowledge, you can use PHP to create and manipulate images. Creating dynamic images with PHP is a pretty easy thing to do. The next ...

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