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 graphic 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 List 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); ($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
Introduction to 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 the height and width of three types of WWW images: GIF, JPEG and PNG. You can use this function 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() extracts the corresponding image source from a GIF
format image file. 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 graphic, usually used as a background or basic canvas Sample usage
. The parameter filename can be a local file or a network URL address. The return value is the GIF file
code, 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 new pictures, 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 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: graphics processing
Content description: This Function used to create a GIF format graphic. The parameter im is the image code 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 use Content-type before sending the image: The header string of image/gif is sent 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 first configure the transparent background using
ImageColorTransparent(). Due to copyright issues, the GIF image generated by this function needs to be considered more when used commercially.

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
