Home  >  Article  >  Backend Development  >  php generate thumbnail code

php generate thumbnail code

高洛峰
高洛峰Original
2016-12-01 10:00:101774browse

Although in HTML you can scale the image arbitrarily by specifying the width and height of the image, this method will not reduce the number of pixels in the image. The size of the graphic file has not changed, and it certainly does not speed up image downloads. Of course, you can also manually generate thumbnails of images through graphics software, but for a large number of image displays, this workload will be huge. An automatic generation program for miniatures was designed for this purpose.
The imagecopyresized function provided in PHP can be used to generate real abbreviated images. The standard syntax of this function 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 picture and resize the picture. The parameters all have the purpose first and the source last. The parameters dst im and src_im are the handles of the image. The parameters dstX, dstY, srcX, and srcY are the coordinates of the destination and source respectively. The parameters dstW, dstH, srcW, and srcH are the width and height of the source and destination respectively. The size of the new image to be adjusted is configured here.
The following is an example to illustrate the usage of this function. The corresponding program thumb.php is shown in Program Listing 12-5.

Program List 12-5 thumb.php
Copy code The code is as follows:
// This function takes out 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 file
// $dstFile: target file
// $dstW: target image 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); ($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 the browser, then change the previous sentence to ImageJpeg($ni);
// If you need images in other formats, Just change the last sentence
}
?>

In this example, first obtain the source image through the getimagesize() function, and then use imagecreatefromgif(),
imagecreatefromjpeg() or imagecreatefrompng() to create a source bitmap $imgsrc, and then use the
imagecreate() function to create a target bitmap whose length and width are half of the source bitmap. Then call the imagecopyresized() function to reduce the source bitmap and copy it to the target bitmap, and finally use the imagegif() function to generate a thumbnail.
The graphics processing functions used here are provided by the installed GD library, and they are explained separately now. First
Introduction to the getimagesize() function, its 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 height and width of three types of WWW images: GIF, JPEG and PNG. You can use this function without installing the GD library. The returned array has 4 elements. The first element of the returned array (index value 0) is the height of the picture in pixels; the second element (index value 1) is the width of the picture; the third element (Index value 2) is the file format of the image, its value 1 is GIF format, 2 is JPEG/JPG format, and 3 is PNG format;
The fourth element (index value 3) is the height and width string of the image, height =xxx width=yyy.
Through the application of the getimagesize() function, various information about the image can be easily obtained. Let me give you an example of obtaining image width, height, format, and file size information to further understand the usage skills of the getimagesize() function.
The program imginfo is shown in Program Listing 12-6.

Program List 12-6 imginfo.php
Copy code The code is as follows:
function getImageInfo($img) //$img is 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 need to first create a blank canvas for drawing.
ImageCreate function can do this. It will return an identifier for the image, and you need to tell the function how big the canvas is in pixels
(x (width) vs. y (height)). The standard syntax of the image creation function imagecreate() used in Program 12-5 is as follows:
Syntax: int imagecreate(int x_size,int y_size);
Return value: integer
Function type: graphics processing
Content description: This Function is used to create a completely empty graph. The parameters x_size and y_size are the size of the graphic, and the unit is 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() extracts the corresponding image source from a GIF
format image file. 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 graphic, usually used as a background or basic canvas Sample usage
. The parameter filename can be a local file or a network URL address. The return value is the GIF file
code, which can be used by other functions.
When the source bitmap is reduced and copied to the target bitmap, the imagecopyresized() function is used. This function can
copy the new image and resize it. Its 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 dstH,int srcW,int srcH);
Return value: integer
Function type: graphics processing

Content description: This function can copy new pictures, and resize the image. For parameters, the purpose comes first and the source comes last. The parameters ddst_im and src_im are the handles of the image. The parameters dstX, dstY, srcX, and srcY are the coordinates of the destination and source respectively. The parameters dstW, dstH, srcW, and srcH are the width and height of the source and destination respectively. If you want to adjust the size of the new image, configure it here.
The standard syntax of the imagegif() function used in outputting images is as follows:
Syntax: int imagegif(int im,string [filename]);
Return value: integer
Function type: graphics processing
Content description: This Function used to create a GIF format graphic. The parameter im is the image code created using ImageCreate(). The parameter filename can be omitted. If there is no parameter filename, the image will be sent directly to the browser. Remember to use Content-type before sending the image: The header string of image/gif is sent to the browser to smoothly transmit the image. If you want to use a GIF image with a transparent background, which is the GIF89a format, you need to first configure the transparent background using
ImageColorTransparent(). Due to copyright issues, the GIF image generated by this function needs to be considered more when used commercially.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn