Although you can scale a picture arbitrarily by specifying the width and height of the picture in HTML, this method does not reduce the number of pixels in the picture. The size of the graphics file has not changed, of course, will not speed up the picture download speed. Of course, you can also manually generate a thumbnail image of the picture, but for a large number of picture display, this workload will be very large. The automatic generation program for this miniature map was designed.
The imagecopyresized function provided in PHP can be used to generate a real thumbnail bribery picture. The scaling of the function
The syntax is as follows:
Syntax: int imagecopyresized (int dst_im,int src_im,int dstx,int dsty,
int srcx,int srcy,int dstw,int dsth,int srcw,int SrcH);
return value: Integer
Function type: Graphics processing
Content Description: This function can copy a new diagram, and resize the picture size. The parameters are all in the previous, source in the back. Parameters DST im and src_im for pictures of handle. Parameters Dstx, Dsty, SRCX, Srcy are the coordinates of the destination and source respectively. Parameters DSTW, Dsth, SRCW, Srch are the source and purpose of the width and height, the new map to adjust the size of the configuration here.
Here's an example to illustrate the use of this function, the corresponding program thumb.php as shown in listing 12-5 of the program.
Program List 12-5 thumb.php
Copy Code code as follows:
?
This function extracts the image from the source file, sets it to the specified size, and outputs it to the destination file
Source file format: gif,jpg,png
Destination file format: GIF
$srcFile: Source Files
$dstFile: Destination file
$dstW: Target picture width
$dstH: Target file height
function Makethumb ($srcFile, $dstFile, $dstW, $dstH)
{
$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, $dstH);
Imagecopyresized ($ni, $IMGSRC, 0,0,0,0, $dstW, $dstH, $srcW, $srcH);
Imagegif ($ni, $dstFile);
If you need to output to a browser, change the previous sentence to imagejpeg ($ni);
If you need a picture in another format, change the last sentence.
}
?>
In this example, the source picture is first obtained through the getimagesize () function, and then the Imagecreatefromgif (),
Imagecreatefromjpeg () or imagecreatefrompng () creates a source bitmap $imgsrc, and then uses the
The Imagecreate () function creates a target bitmap that is half the length and width of the source bitmap. Then call imagecopyresized ()
function, the source bitmap is reduced and copied to the target bitmap, and then the Imagegif () function is used to generate the thumbnail.
The graphics processing function used here is provided by the installed GD Library, which is explained separately. First of all
Describes the getimagesize () function, whose standard syntax is as follows.
Syntax: Array getimagesize (String Filename,array [Imageinfo]);
return value: Array
Function type: Graphics processing
Content Description: This function can be used to obtain the GIF, JPEG and PNG 3 kinds of WWW pictures on the high and wide, do not need to install the GD library can use this function. The returned array has 4 elements, and the first element of the array (index value 0) is the height of the picture. The unit is pixel (pixel), the second element (index value 1) is the width of the picture, and the third Element (index 2) is the file format of the picture, its value 1 is GIF format, 2 is jpeg/jpg format, 3 for PNG format;
The fourth element (index value 3) is the high and wide string for the picture, Height=xxx width=yyy.
Through the application of getimagesize () function, can easily get the various information of the picture. Here's an example of how to get a picture of the width, height, format, and file size of the image to further understand the use of the getimagesize () function.
The program Imginfo as shown in listing 12-6 of the program.
Program List 12-6 imginfo.php
Copy Code code as follows:
<?php
function Getimageinfo ($img)//$img absolute path to 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 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 need to first create a blank canvas for the drawing.
The Imagecreate function can do this. It returns an identifier for an image and tells the function to use the pixel
How large the calculated canvas is (x (width) and y (height)). The graphical creation function used in program 12-5 imagecreate ()
The standard syntax is as follows:
Syntax: int imagecreate (int x_size,int y_size);
return value: Integer
Function type: Graphics processing
Description: This function is used to create a fully-empty graphic. Parameters X_size, y_size as the dimensions of the graphic, units
is a pixel (pixel).
If you want to remove the picture file code from an existing picture, you can use Imagecreatefromgif (),
Imagecreatefromjpeg () or imagecreatefrompng (), such as function imagecreatefromgif () is a GIF
Format of the picture file to take out the corresponding picture source code, its standard syntax is as follows:
Syntax: int imagecreatefromgif (string filename);
return value: Integer
Function type: Graphics processing
Content Description: This function is used to take out a GIF format graphics, usually as a background or basic canvas sample to make
Use. Parameter filename can be a local-side file, or it can be the URL of a network. Return a file with GIF value
Code that can be used by other functions.
The imagecopyresized () function is used when the source bitmap is shrunk and copied into the target bitmap, which can be
Copy the new diagram and resize it, with the following standard syntax:
Syntax: int imagecopyresized (int dst_im,int src_im,int dstx,int dsty,int srcx,int,
int dstw,int dsth,int srcw,int srcH);
return value: Integer
Function type: Graphics processing
Content Description: This function can copy a new diagram, and resize the picture size. parameter that is the purpose in front, come
Source in the post. Parameter Ddst_im and Src_im are the handle of the picture. Parameters Dstx, Dsty, SRCX, Srcy, respectively, for the purpose
and the coordinates of the source. Parameters DSTW, Dsth, SRCW, srch respectively as the source and the purpose of the wide and high, if you want to adjust the new map
The size is configured here.
Finally, the standard syntax for the imagegif () function used to output the image is as follows:
Syntax: int imagegif (int im,string [filename]);
return value: Integer
Function type: Graphics processing
Description: This function is used to create a graphic GIF format. parameter IM is built using imagecreate ()
Picture code, parameter filename can be omitted, if there is no this parameter filename, will be directly to the image of the browser side,
Remember to send a header string (header) using Content-type:image/gif to the browser before sending the picture
End to transfer the picture smoothly. To use a transparent background gif, which is the gif89a format, you need to first use the
Imagecolortransparent () configures a transparent background. The GIF generated by this function, because of the copyright problem, so
The use of business is more to be considered.