php generates thumbnail code

  • 2020-05-17 05:00:40
  • OfStack

Although in HTML you can scale the image at will by specifying the width and height of the image, this method does 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 downloading. Of course, you can also manually generate thumbnails of images through graphics software, but for a large number of images, this amount of work will be 10 minutes. For this purpose, the automatic micrograph generation program has been designed.
The imagecopyresized function provided in PHP can be used to generate a real contractile image. 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: graphic processing
This function can copy a new image and resize the image. The parameters are intended before the source. The parameters dst im and src_im are handle of the picture. 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 purpose respectively. The size of the new drawing to be adjusted is configured here.
An example of how to use this function is shown in listing 12 -- 5 of the program thumb.php.

Listing 12 -- 5 thumb.php
 
<? 
//  This function takes 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 : the source file  
// $dstFile:  The 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, you will 1 Instead of a sentence  ImageJpeg($ni); 
//  If you need an image in another format, change it at the end 1 A sentence will do  
} 
?> 

In this example, the source image is first obtained by the getimagesize() function, and then by imagecreatefromgif(),
imagecreatefromjpeg() or imagecreatefrompng() create a source bitmap $imgsrc and then use
The imagecreate() function creates a destination bitmap half the length and half the width of the source bitmap. And then imagecopyresized()
Function, copy the source bitmap to the destination bitmap, and finally generate the thumbnail using the imagegif() function.
The graphics functions used here are provided by the installed GD library and are described separately. First of all,
The getimagesize() function is introduced, with the standard syntax as follows.
Grammar: array getimagesize(string filename,array [imageinfo]);
Return value: array
Function type: graphic processing
Content description: this function can be used to obtain the height and width of the pictures on GIF, JPEG and PNG. This function can be used without installing GD library. The returned array has four elements. The first element (index value 0) of the returned array is the height of the image in pixels (pixel). The second element (index value 1) is the width of the image. The third element (index value 2) is the file format of the image, whose 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 getimagesize() function, all kinds of information about pictures can be easily obtained. Here's an example of how to get information about the width, height, format, and file size of an image. Take a step further and see how the getimagesize() function works.
The program imginfo is shown in listing 12-6 of the program.

Listing 12-6 imginfo.php
 
<?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, create a blank canvas for drawing.
The ImageCreate function does this. It will return an image identifier and need to tell the function to use pixels
Calculate how big the canvas is (x(width) and y(height)). The graphic creation function imagecreate() used in program 12-5
The standard grammar is as follows:
Syntax: int imagecreate(int x_size,int y_size);
Return value: integer
Function type: graphic processing
This function is used to create an empty graph. The parameters x_size and y_size are the dimensions and units of the graph
Is pixel (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 from 1 GIF
Take out the corresponding image source code from the image file of the format, and its standard syntax is as follows:
Grammar: int imagecreatefromgif(string filename);
Return value: integer
Function type: graphic processing
This function is used to extract a graphic in GIF format, usually as a background or basic canvas sample
To use. The parameter filename can be either a file on the local end or an URL address on the network. Returns a file with a value of GIF
Code that can be used by other functions.
The imagecopyresized() function is used when copying the source bitmap down to the destination bitmap
Copy the new image and resize it. The 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: graphic processing

This function can copy a new image and resize the image. Parameter that's the purpose before, come on
Source in the back. The parameters ddst_im and src_im are handle of the picture. The parameters dstX, dstY, srcX and srcY are for the purpose respectively
And the coordinates of the source. The parameters dstW, dstH, srcW and srcH are the width and height of the source and purpose respectively
The dimensions are configured here.
The standard syntax for the imagegif() function used to output the image is as follows:
Grammar: int imagegif(int im,string [filename]);
Return value: an integer
Function type: graphic processing
This function is used to create an GIF graphic. The parameter im is built using ImageCreate()
If there is no filename parameter, the image will be directly sent to the browser side.
Remember to send the image to the browser using Content-type :image/gif header string (header)
End to smooth image transfer. To use the GIF diagram with a transparent background, which is GIF89a format, you need to use it first
ImageColorTransparent() has a transparent background. This function generates the GIF diagram, due to copyright issues, therefore
Commercial use needs to be considered.

Related articles: