PHP generated thumbnail program code and detailed introduction

Source: Internet
Author: User
Tags imagejpeg

12. 3 The generation of automatic miniature graphs
Because of the large amount of data in the picture, the transfer process is relatively long, so when users browse the picture, often want to first
Displays a thumbnail of a picture.
Although you can scale pictures at random 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. When
You can also manually generate thumbnails of pictures from graphics software, but for a large number of picture shows, 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. Parameters are intended to be in front, to
Source in the post. Parameters DST im and src_im for pictures of handle. 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 purpose of the wide and high, to adjust the new map
The size is configured 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

The code is as follows Copy Code
?
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
You can use this function if you need to install the GD library. The returned array has 4 elements, returning the first element of the array (the cable
The value 0 is the height of the picture, in pixels (pixel), and the second element (index value 1) is the width of the picture; the third element
The element (index value 2) is the file format of the picture, its value 1 is GIF format, 2 is jpeg/jpg format, 3 is 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. Let's give you an example of getting
Picture width, height, format, file size information example, to further understand the use of getimagesize () function
Skills. The program Imginfo as shown in listing 12-6 of the program.

Program List 12-6 imginfo.php

The code is as follows Copy Code

<?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.

The code is as follows Copy Code

<?php

$pic _name=date ("Dmyhis");

Build the width of the picture
$pic _width=$_post[' width '];

Height of the build picture
$pic _height=$_post[' length '];

function Resizeimage ($im, $maxwidth, $maxheight, $name) {
Get the current picture size
$width = Imagesx ($im);
$height = Imagesy ($im);
To generate the size of a thumbnail
if ($width > $maxwidth) | | ($height > $maxheight)) {
$widthratio = $maxwidth/$width;
$heightratio = $maxheight/$height;
if ($widthratio < $heightratio) {
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;

if (function_exists ("imagecopyresampled")) {
$newim = Imagecreatetruecolor ($newwidth, $newheight);
Imagecopyresampled ($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = Imagecreate ($newwidth, $newheight);
Imagecopyresized ($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
Imagejpeg ($newim, $name. ". jpg");
Imagedestroy ($newim);
}else{
Imagejpeg ($im, $name. ". jpg");
}
}

 if ($_files[' image '] [' size ']) {
  //echo $_files[' image ' [' type '];
  if ($_ files[' image ' [' type '] = = "Image/pjpeg" | | $_files[' image ' [' type '] = = "Image/jpg" | | $_files[' image ' [' type '] = = "Image/jpeg") {
    $im = imagecreatefromjpeg ($_files[' image '] [' Tmp_ Name ']);
  }elseif ($_files[' image '] [' type '] = = "Image/x-png") {
    $im = Imagecreatefrompng ( $_files[' image ' [' tmp_name ']);
  }elseif ($_files[' image '] [' type '] = = "Image/gif") {
    $im = imagecreatefromgif ($_ files[' image ' [' tmp_name ']);
  }
  if ($im) {
   if file_exists ($pic _name. '). JPG ') {
    unlink ($pic _name. '). JPG ');
   }
   resizeimage ($im, $pic _width, $pic _height, $pic _name);
   imagedestroy ($im);
  }
 }
?

' ><br><br>
<form enctype= ' multipart/form-data ' method= ' post ' action= ' Small_ picture.php ">
<br>
<input type=" file "name=" image "Size=" "value=" browse "><p>
Generate thumbnail width: <input type= "Text" name= "width" size= "5" ><P>
Generate thumbnail length: <input type= "text" name= "Length" size= "5" > <p>
<input type= "Submit" value= "Upload picture" >
</form>

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.