Generate thumbnail code in php_PHP tutorial
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
// 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
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.
