8 Tips for PHP scripting (4) _php tutorial

Source: Internet
Author: User
Creating images dynamically

After installing some third-party libraries, combined with your graphics processing skills, you can create and manipulate images in PHP. In fact, you don't need much knowledge of geometry. When I was in high school, this subject always failed, and now I will create an image with PHP! Before using the basic image creation function, you need to install the GD library. If you want to use a JPEG-related image creation function You will also need to install jpeg-6b. You must also install T1lib when using the Type 1 font in the image.

Here you will also need to make further adjustments to your system. First, you must install T1lib to provide image processing support, and then install JPEG-6B. The third step is to install the GD function library. You have to finish these three jobs in order, because you need to compile the GD library to use the JPEG-6B library, if the jpeg-6b step first install, compile will be wrong, then you are busy and no way.

After installing the above three libraries, you will have to reconfigure PHP. This is what you do when you install the DSO version of PHP Oh! Then execute make clean, command, and then add the following code to the current configuration instruction character:

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

Execute the make, make install command in the final order to complete the configuration task. Restart Apache, run the phpinfo () function to check if the new feature is functioning properly.

Related to the GD library you installed, you might or may not have the ability to create GIF or PNG images. The point is: if you've installed gd-1.6 or an earlier version, you can handle GIF but not PNG. If gd-1.6 or later versions are installed, you can handle PNG but not GIF.

Creating a simple image takes several functions. I will follow the steps to take you to learn this process:

Output a file header that contains the MIME type of the image you created, in our case PNG.

Use Imagecreate () to create a variable to hold a blank image. The function requires an image size in pixels. The format is imagecreate (X_size, Y_size), and for images of 250-x-250 pixels, use the following:

$NEWIMG = Imagecreate (250,250);

Because your image is still blank, you have to try to fill it with some color, but first you need to assign a name to each color by the RGB value of the color, which uses the Imagecolorallocate () function. The format of the function is imagecolorallocate ([image], [red], [green], [blue]). If it is turquoise, the specific code is as follows:

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

Next, you need to call the Imagefill () function to fill the image with the above color. Imagefill (), functions have several versions, such as Imagefillrectangle (), Imagefillpolygon (), and so on. For the sake of simplicity, we use the Imagefill () function for color fills, in the following format:

Imagefill ([Image], [Start x Point], [Start y point], [color])
Imagefill ($NEWIMG, 0,0, $skyblue);

Finally, you create an image and destroy the image stream to free up memory:

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

The exact code looks like this:

$NEWIMG = Imagecreate (250,250);
$skyblue = Imagecolorallocate ($newImg, 136,193,255);
Imagefill ($NEWIMG, 0,0, $skyblue);
Imagepng ($NEWIMG);
Imagedestroy ($NEWIMG);
?>

If you call this script skyblue.php and use your browser to access it, you will see a large blue png image of 250-x-250 pixels.

You can also use image creation functions to manipulate images, such as creating thumbnails of large images.

Suppose you're going to make a thumbnail of a 35-x-35 pixel size for a picture. All you have to do is create a new image with a size of 35 pixels, produce an image stream containing the contents of its original image, change the size of the original image, and place it in a new, blank image.

The key function used to achieve this is imagecopyresized (), the format of the function is 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], [Origin Al image Y]);

The following is a code comment.

Header ("Content-type:image/png");

/* Set up variables to hold the height and width of your new image */
$newWidth = 35;
$newHeight = 35;

/* Create a blank, new image of the given new height and width */
$NEWIMG = Imagecreate ($newWidth, $newHeight);

/* Get the data from the original, large image */
$ORIGIMG = Imagecreatefrompng ("Test.png");

/* Copy the resized image. Use the Imagesx () and Imagesy functions to get the X and y sizes of the orginal image. */
Imagecopyresized ($NEWIMG, $ORIGIMG, 0,0,0,0, $newWidth, $newHeight, Imagesx ($ORIGIMG), Imagesy ($ORIGIMG));

/* Create final image and free up the memory */
Imagepng ($NEWIMG);
Imagedestroy ($NEWIMG);?>

If you call the above script resized.php and use your browser to access it, you should be able to see a miniature PNG image of 35-x-35 pixel size.


http://www.bkjia.com/PHPjc/532525.html www.bkjia.com true http://www.bkjia.com/PHPjc/532525.html techarticle create images dynamically once you have installed some third-party libraries, combined with your graphics skills, you can create and manipulate them in PHP. In fact, you don't need too much geometry ...

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