PHP Picture scaling function: Achieve equal scale without distortion scaling

Source: Internet
Author: User
Tags format final imagejpeg php web development

In the PHP Web development process, if you set up a site involving a large number of image processing, must involve picture upload, zoom, and how to keep the picture is not distorted, is a lot of beginners PHP Web developers more headaches, today, David and you share how to zoom picture. Before use you need to download and install the GD library to support PHP image processing. Below we combine the code to explain the specific PHP image scaling processing ideas.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
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,0, $newwidth, $newheight, $pic _width, $pic _height);


        }


Else


        {


$newim = imagecreate ($newwidth, $newheight);


imagecopyresized ($newim, $im, 0,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 a Picture object, you need to read the image object with Imagecreatefromjpeg () before applying the function, and if the PHP environment supports Png,gif, you can also use Imagecreatefromgif (), Imagecreatefrompng ();

$maxwidth define the maximum width (in pixels) of the resulting picture

$maxheight The maximum height of a picture to be generated (in pixels)

$name the generated picture name

$filetype the resulting picture type (. jpg/.png/.gif)

code Comments :

Line 3rd to 4th: Read the picture that needs to be scaled. Actual width height

Line 8th to 26th: by calculating the actual picture width and the need to generate a picture of the width of the high compression ratio of the final picture scaling is based on the width or height of the zoom, the current program is based on the width of the picture scaling. if you want to scale the picture according to the height, you can change the statement in line 22nd to $widthratio> $heightratio

Line 28th to 31st: If the actual picture's length or width is less than the length or width of the resulting picture, either the picture is scaled according to the length, or the picture is scaled according to its width.

Line 33rd to 34th: Calculates the long width of the picture generated by the final scaling.

Line 36th to 45th: According to the calculation of the final generated picture of the length of the image to change the size of the picture, there are two ways to change the size of the picture : imagecopyresized () function in all GD version of the effective, but its scaling image algorithm is relatively rough. Imagecopyresamples (), its pixel interpolation algorithm gets the edge of image smoother, but the function is slower than imagecopyresized ().

Line 47th to 49th: The resulting processed picture, if you need to generate GIF or PNG, you need to change the imagejpeg () function to Imagegif () or imagepng ( )

Line 51st to 56th: if the actual picture's length is smaller than the length of the specified picture, keep the picture intact, and similarly, if you need to generate GIF or PNG, you need to change the imagejpeg () function to Imagegif () or imagepng ().

Special Note :

The 1.6.2 version of the GD library previously supported the GIF format, but the GIF format was not supported after the GD1.6.2 version because the LZW algorithm involved patent rights. If you are Windows environment, you just go into the php.ini file to find Extension=php_gd2.dll, will # Remove, restart Apache, if you are a Linux environment, and want to support gif,png,jpeg, you need to download libpng , zlib, and FreeType fonts and install.

ok,php picture compression function complete, finally we outline the whole process of thinking:

By calculating the length and width of the actual picture, the scaling ratio between the long width of the picture is generated, according to the actual demand (according to the width or height of the picture scaling) to calculate the size of the resulting picture, and then apply the PHP image processing function to the picture processing, the final output picture.

The above is on the PHP picture processing How to compress the picture and keep the function of not distortion, there are questions or good suggestions welcome to my message, the next time I will share in the PHP site development and construction completed, because the picture catalog is not well planned, how do we move the image of the idea.

  Note : PHP Web Development Tutorials-leapsoul.cn Copyright, reproduced in the form of links to indicate the original source and this statement, thank you.







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.