Home  >  Article  >  Backend Development  >  Generate thumbnail code in php_PHP tutorial

Generate thumbnail code in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:17:53895browse

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 graphics 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 Listing 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);
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 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
introduce 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 3 types of GIF, JPEG and PNG You can use this function to set the height and width of images on WWW 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() is to extract the image file code from a GIF
format. Get the corresponding image source code from the image file. The standard syntax is as follows:
Syntax: int imagecreatefromgif(string filename);
Return value: integer
Function type: Graphics processing
Content description: This function Used to take out a GIF format graphic, usually used as a background or basic canvas sample
. The parameter filename can be a local file or a network URL address. The return value is the file
code of GIF, 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 a new image 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 the 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 : Graphic processing
Content description: This function is used to create a GIF format graphic. The parameter im is the image code of
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 send the image before sending it. Content-type:image/gif header string (header) 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 use
ImageColorTransparent() to configure a transparent background first. The GIF image generated by this function has copyright issues, so
needs to be considered more when used commercially.

http://www.bkjia.com/PHPjc/325640.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325640.htmlTechArticleAlthough 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, certainly not...
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