PHP Generate thumbnail code

Source: Internet
Author: User
Although you can resize a picture arbitrarily by specifying the width and height of the image in HTML, this method does not reduce the number of pixels in the image. The size of the graphics file does not change, and of course it does not speed up the download of the picture. Of course, you can also manually generate thumbnails of the pictures through the graphics software, but for a large number of picture display, this workload will be very large. The auto-generating program for this thumbnail is designed.
The imagecopyresized function provided in PHP can be used to generate a truly indented bribery image. 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
Description: This function copies the new diagram and resizes the size of the picture. Parameters are the purpose before, and the source is behind. Parameters for DST im and src_im for picture handle. The parameters dstx, Dsty, SRCX, Srcy are the coordinates of the purpose and the source respectively. Parameters DSTW, Dsth, SRCW, srch respectively are the source and the purpose of the width and height, the size of the new map to adjust is configured here.
Here is an example to illustrate the use of this function, the corresponding program thumb.php as shown in Listing 12-5.

Program Listing 12-5 thumb.php
Copy the code code 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: Destination 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 a browser, then change the previous sentence to imagejpeg ($ni);
If you need a picture in another format, change the last sentence.
}
?>

In this example, the getimagesize () function is first used to obtain the source picture, then the Imagecreatefromgif (),
Imagecreatefromjpeg () or imagecreatefrompng () creates a source bitmap $imgsrc, and then uses the
The Imagecreate () function creates a target bitmap whose length and width are half the source bitmap. Then call imagecopyresized ()
function, the source bitmap is reduced and copied to the target bitmap, and then the thumbnail is generated using the Imagegif () function.
The graphical processing functions used here are provided by the installed GD library and are now described separately. First of all
Describes the getimagesize () function, which has the following standard syntax.
Syntax: Array getimagesize (String Filename,array [Imageinfo]);
return value: Array
Function type: Graphics processing
Description: This function can be used to obtain GIF, JPEG and PNG 3 kinds of www slices of high and wide, do not need to install the GD library to use this function. The returned array has 4 elements, the first element of the returned array (index value 0) is the height of the picture, the unit is the pixel (pixel), the second element (index value 1) is the width of the picture, the third element (index value 2) is the file format of the picture, the value 1 is in GIF format, 2 is jpeg/jpg format, 3 for PNG format;
The fourth element (index value 3) is the height and width of the picture string, Height=xxx width=yyy.
Through the application of the getimagesize () function, all kinds of information of the picture can be easily obtained. Let's give you an example of getting the image width, height, format, file size information to further understand the getimagesize () function of the use of skills.
The program Imginfo as shown in Listing 12-6.

Program Listing 12-6 imginfo.php
Copy the code code as follows:
<?php
function Getimageinfo ($img)//$img absolute path for 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 drawing.
The Imagecreate function can do this. It returns an identifier for an image and needs to tell the function to use a pixel
How large the computed canvas is (x (width) and y (height)). Create a function imagecreate () in the graph 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: Graphics processing
Description: This function is used to create an all-empty graph. Parameters X_size, y_size are the dimensions of the graph, units
to pixels (pixel).

If you want to remove the image file code from a picture that already exists, you can use Imagecreatefromgif (),
Imagecreatefromjpeg () or imagecreatefrompng (), such as the function Imagecreatefromgif () is from a GIF
Format of the image file to remove the corresponding image source code, its standard syntax is as follows:
Syntax: int imagecreatefromgif (string filename);
return value: Integer
Function type: Graphics processing
Description: This function is used to remove a GIF format graphic, usually as a background or a basic canvas sample to make
Use. The parameter filename can be either a local-side file or a URL to the network. Returns a file with a GIF value
Code that can be used by other functions.
The imagecopyresized () function is used when the source bitmap is reduced and copied to the target bitmap, and this function can
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 Srcy,
int dstw,int dsth,int srcw,int srcH);
return value: Integer
Function type: Graphics processing

Description: This function copies the new diagram and resizes the size of the picture. parameter that is the purpose in front, come
Source in the back. The parameters 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 are the source and the purpose of the width and height, if you want to adjust the new map
The size is configured here.
The standard syntax for the imagegif () function that was 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 GIF format graphic. The parameter IM is built using imagecreate ()
The image code, parameter filename can be omitted, if there is no this parameter filename, the picture will be sent directly to the browser side,
Remember to send the Content-type:image/gif header string to the browser before sending the image
To smooth the transfer of pictures. To use a GIF graph with a transparent background, that is, the gif89a format, you need to first use the
Imagecolortransparent () Configure a transparent background. The GIF graph generated by this function, due to the issue of copyright, so
More consideration is needed in the commercial use.
  • Related Article

    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.