PHP image scaling function: achieves proportional non-distortion scaling

Source: Internet
Author: User
Tags php website
PHP blog, PHP image processing, PHP image scaling without distortion, PHP image scaling function, PHP image scaling without distortion, PHP technology blog

During PHP website development, if a website you create involves a large amount of image processing, it will inevitably involve Image uploading and scaling, and how to keep the images undistorted, it is a headache for many junior PHP website developers. Today, the PHP blog will share with you how to resize images. Before using this function, you must download and install the GD library to support PHP image processing. The following code is used to explain how to scale PHP images.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Function resizeImage ($ im, $ maxwidth, $ maxheight, $ name, $ filetype)
{
$ Pic_width = imagesx ($ im );
$ Pic_height = imagesy ($ im );

If ($ maxwidth & $ pic_width> $ maxwidth) | ($ maxheight & $ pic_height> $ maxheight ))
{
If ($ maxwidth & $ pic_width> $ maxwidth)
{
$ Widthratio = $ maxwidth/$ pic_width;
$ Resizewidth_tag = true;
}

If ($ maxheight & $ pic_height> $ maxheight)
{
$ Heightratio = $ maxheight/$ pic_height;
$ Resizeheight_tag = true;
}

If ($ resizewidth_tag & $ resizeheight_tag)
{
If ($ widthratio <$ heightratio)
$ Ratio = $ widthratio;
Else
$ Ratio = $ heightratio;
}

If ($ resizewidth_tag &&! $ Resizeheight_tag)
$ Ratio = $ widthratio;
If ($ resizeheight_tag &&! $ Resizewidth_tag)
$ Ratio = $ heightratio;

$ Newwidth = $ pic_width * $ ratio;
$ Newheight = $ pic_height * $ ratio;

If (function_exists ("imagecopyresampled "))
{
$ Newim = imagecreatetruecolor ($ newwidth, $ newheight );
Imagecopyresampled ($ newim, $ im, 0, 0, 0, $ newwidth, $ newheight, $ pic_width, $ pic_height );
}
Else
{
$ Newim = imagecreate ($ newwidth, $ newheight );
Imagecopyresized ($ newim, $ im, 0, 0, 0, $ newwidth, $ newheight, $ pic_width, $ pic_height );
}

$ Name = $ name. $ filetype;
Imagejpeg ($ newim, $ name );
Imagedestroy ($ newim );
}
Else
{
$ Name = $ name. $ filetype;
Imagejpeg ($ im, $ name );
}
}

 

Parameter description:

$ Im Image object. before using the function, you need to use imagecreatefromjpeg () to read image objects. if the PHP environment supports PNG and GIF, you can also use imagecreatefromgif () and imagecreatefrompng ();

$ Maxwidth defines the maximum width of the generated image (unit: pixels)

$ Maxheight: the maximum image height (unit: pixels)

Name of the image generated by $ name

$ Filetype: The Final generated Image (.jpg/. png/. gif)

Code comment:

3rd ~ Row 4: read the actual width and height of the image to be scaled.

8th ~ Row 26: calculate the actual image width and height and the compression ratio of the image width and height to determine whether the image is scaled based on the width or height. the current program scales the image based on the width. If you want to scale the image based on the height, you can change the statement of row 22nd to $ widthratio> $ heightratio.

28th ~ Row 31: if the actual image length or width is smaller than the specified length or width, the image is scaled based on the length or width.

33rd ~ Row 34: calculates the length and width of the scaled image.

36th ~ Row 45: the calculated image size is changed based on the length and width of the final generated image. There are two ways to change the image size: the ImageCopyResized () function is valid in all GD versions, however, the algorithm for scaling images is rough. ImageCopyResamples (), the edge of the image obtained by the pixel interpolation algorithm is smoother, but the speed of this function is slower than that of ImageCopyResized.

47th ~ Row 49: the processed image is generated. if you need to generate GIF or PNG, you need to change the imagejpeg () function to imagegif () or imagepng ()

51st ~ 56 rows: if the actual image length and width are smaller than the specified image length and width, the image remains unchanged. Similarly, if you need to generate GIF or PNG, you need () the function is changed to imagegif () or imagepng ().

Note:

The GD Library version 1.6.2 previously supported the GIF format, but because the GIF format uses the LZW algorithm to involve patent rights, the GIF format is not supported after GD1.6.2. If you are in a WINDOWS environment, you only need to enter PHP. in the INI file, find extension = php_gd2.dll, remove #, and restart APACHE. if you are in a Linux environment and want to support GIF, PNG, JPEG, you need to download libpng, zlib, and freetype fonts.

OK. PHP image compression function is complete. at last, we will give an overview of the entire process:

The final size of the generated image is calculated by calculating the scale ratio between the actual length and width of the image and the specified length and width of the image, then, the PHP image processing function is used to process the image and output the image.

The above is the description about how to compress the PHP image and keep the function intact. if you have any questions or good suggestions, please leave a message. next time I will share with you after the PHP website is developed and constructed, the idea of how to migrate images is not well planned.

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.