Ec (2); automatic thumbnail generation & nbsp; the transfer process is relatively long due to the large amount of image data, therefore, when users browse images, they often want to display the thumbnail of an image first. & Nbsp; although you can adjust the image width and height in HTML, 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 use script ec (2); script
Automatic thumbnail generation
Because the image data volume is large and the transfer process is relatively long, users often want
Displays a thumbnail of an image.
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 an image. The size of the image file has not changed, and of course the image download speed will not be accelerated. When
However, you can also manually generate image thumbnails through the graphic software, but for a large number of image presentations, this workload
It will be 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. Parameters are all aimed at the beginning.
After the source is located. The dst 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 respectively.
The dimension 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. php
// 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
Content Description: this function can be used to obtain the height and width of the GIF, JPEG, and png www slices.
You need to install GD library to use this function. The returned array has four elements and returns the first element of the array (
0) indicates the Image Height, in pixels. The second element (index value 1) indicates the image width. The third element
Element (index value 2) is the image file format. Its value 1 is in GIF format, 2 is in JPEG/JPG format, and 3 is in PNG format;
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.
An example of the image width, height, format, and file size to further understand the use of the getimagesize () function
Tips. The program imginfo is shown in the program list 12-6.
Program list 12-6 imginfo. 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.