Php generates a thumbnail code

Source: Internet
Author: User
Tags image identifier

Although the width and height of an image can be specified in HTML to scale the image at will, this method does not reduce the number of pixels of the image. The size of the image file has not changed, and of course the image download speed will not be accelerated. Of course, you can also manually generate image thumbnails through graphic software, but for a large number of image presentations, this workload will be very huge. For this reason, the Automatic Generation Program of the thumbnail is designed.
The imagecopyresized function provided in PHP can be used to generate real scaled-down images. Push the Function
Syntax:
Syntax: int imagecopyresized (int dst_im, int src_im, int dstX, int dstY,
Int srcX, int srcY, int dstW, int dsomething, int srcW, int srcH );

Return Value: integer
Function Type: Graphic Processing
Description: This function allows you to copy and resize a new image. All parameters are aimed at the beginning and the source is later. The dst im and src_im parameters are the handle of the image. The dstX, dstY, srcX, and srcY parameters are the coordinates of the destination and source respectively. The dstW, DTH, srcW, and srcH parameters are the source and target width and height respectively. The size of the New Graph to be adjusted is configured here.
The following is an example to describe how to use this function. The corresponding program thumb. php is shown in the program list 12-5.

Program list 12-5 thumb. phpCopy codeThe Code is as follows: <?
// This function extracts the image from the source file, sets it to the specified size, and outputs it to the target file.
// Source file format: gif, jpg, and png
// Target file format: gif
// $ SrcFile: source file
// $ DstFile: target file
// $ DstW: Target Image Width
// $ DTH: height of the target file
Function makethumb ($ srcFile, $ dstFile, $ dstW, $ dsomething)
{
$ Data = GetImageSize ($ srcFile, & $ info );
Switch ($ data [2])
{
Case 1:
$ Imgsrc = @ ImageCreateFromGIF ($ srcFile );
Break;
Case 2:
$ Imgsrc = @ ImageCreateFromJPEG ($ srcFile );
Break;
Case 3:
$ Imgsrc = @ ImageCreateFromPNG ($ srcFile );
Break;
}
$ SrcW = ImageSX ($ imgsrc );
$ SrcH = ImageSY ($ imgsrc );
$ Ni = ImageCreate ($ dstW, $ dsomething );
ImageCopyResized ($ ni, $ imgsrc, 0, 0, 0, $ dstW, $ DTH, $ srcW, $ srcH );
Imagegif ($ ni, $ dstFile );
// If You Need to output data to the browser, change the previous sentence to ImageJpeg ($ ni );
// If you need images in other formats, you can change the last sentence.
}
?>

In this example, first obtain the source image using the getimagesize () function, and then use imagecreatefromgif (),
Imagecreatefromjpeg () or imagecreatefrompng () creates a source bitmap $ imgsrc, and then uses
The imagecreate () function creates a target bitmap. Its length and width are half of the source bitmap. Then call imagecopyresized ()
Function to zoom out the source bitmap and copy it to the target bitmap. Then, use the imagegif () function to generate a thumbnail.
The image processing functions used here are provided by the installed GD library and are described separately. First
This section describes the getimagesize () function. The standard syntax is as follows.
Syntax: array getimagesize (string filename, array [imageinfo]);
Returned value: Array
Function Type: Graphic Processing
Description: This function can be used to obtain the height and width of GIF, JPEG, and png www slices. You can use this function without installing the GD library. The returned array has four elements. The first element (index value 0) of the returned array is the image height, in pixels, and the second element (index value 1) is the image width. The third element (index value 2) is the image file format. The value 1 is GIF, 2 is JPEG/JPG, and 3 is PNG;
The fourth element (index value 3) is the image's high and wide string, height = xxx width = yyy.
Through the application of the getimagesize () function, various image information can be easily obtained. The following is an example of how to obtain the image width, height, format, and file size to further understand how to use the getimagesize () function.
The program imginfo is shown in the program list 12-6.

Program list 12-6 imginfo. phpCopy codeThe Code is as follows: <? Php
Function getImageInfo ($ img) // $ img indicates the absolute path of the image file.
{
$ Img_info = getimagesize ($ img );
Switch ($ img_info [2])
{
Case 1:
$ Imgtype = "GIF ";
Break;
Case 2:
$ Imgtype = "JPG ";
Break;
Case 3:
$ Imgtype = "PNG ";
Break;
}
$ Img_type = $ imgtype. "image ";
$ Img_size = ceil (filesize ($ img)/1000). "k"; // get the file size

$ New_img_info = array (
"Width" => $ img_info [0],
"Height" => $ img_info [1],
"Type" => $ img_type,
"Size" => $ img_size
);
Print "width ";
Print $ img_info [0];
Print "height ";
Print $ img_info [1];
Print "format ";
Print $ img_type;
Print "size ";
Print $ img_size;
Print $ new_img_info;
}

$ Img = "/www/htdocs/images/jf.gif ";
GetImageInfo ($ img );
?>

To create a thumbnail in program 12-5, you must first create a blank canvas for drawing.
The ImageCreate function can do this. It returns an image identifier and needs to tell the function to use pixels.
The size of the calculated canvas (x (width) and y (height )). The image creation function imagecreate () used in program 12-5 ()
The standard syntax is as follows:
Syntax: int imagecreate (int x_size, int y_size );
Return Value: integer
Function Type: Graphic Processing
Description: This function is used to create an empty graph. The x_size and y_size parameters are the image sizes, unit:
Is pixel (pixel ).

If you want to extract the image file code from an existing image, you can use imagecreatefromgif (),
Imagecreatefromjpeg () or imagecreatefrompng (). For example, the function imagecreatefromgif () is
The source code of the corresponding image is extracted from the format image file. The standard syntax is as follows:
Syntax: int imagecreatefromgif (string filename );
Return Value: integer
Function Type: Graphic Processing
Description: This function is used to retrieve a GIF image, which is usually used as a background or basic canvas sample.
. The filename parameter can be a local file or a network URL. The file whose return value is GIF
Code, which can be used by other functions.
When downgrading the source bitmap and copying it to the target bitmap, The imagecopyresized () function is used. This function can
Copy A New Graph and resize it. The standard syntax is as follows:
Syntax: int imagecopyresized (int dst_im, int src_im, int dstX, int dstY, int srcX, int srcY,
Int dstW, int dsomething, int srcW, int srcH );
Return Value: integer
Function Type: Graphic Processing

Description: This function allows you to copy and resize a new image. The parameter is intended first.
After the source is located. The ddst_im and src_im parameters are the handle of the image. DstX, dstY, srcX, and srcY Parameters
And source coordinates. The dstW, DTH, srcW, and srcH parameters are the source and target widths and heights.
The dimension is configured here.
Finally, the standard syntax of the imagegif () function used in image output is as follows:
Syntax: int imagegif (int im, string [filename]);
Return Value: integer
Function Type: Graphic Processing
Description: This function is used to create a GIF image. The im parameter is created using ImageCreate ()
The image code. The filename parameter can be omitted. If this parameter is not available, the image will be sent directly to the browser,
Remember to send the header string (header) Using Content-type: image/gif to the browser before sending the image.
To transmit images smoothly. To use a GIF image with a transparent background, that is, the GIF89a format, you must first use
ImageColorTransparent () is used to configure the transparent background. The GIF image generated by this function is copyrighted.
Commercial use must be considered.

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.