There was nothing to do at home during the National Day. I was bored to look at some of the code I wrote before and found that there was always something unsatisfactory. today I found that a website is distorted because the image size on that page is different, to prevent page deformation, I restrict the image size in HTML.
There was nothing to do at home during the National Day. I was bored to look at some of the code I wrote before and found that there was always something unsatisfactory. today I found that a website is distorted because the image size on that page is different, in order to prevent page deformation, I restricted the image size in HTML. suddenly, is there any way to limit the image size and prevent Image deformation, I checked the php information about image processing. The following is the source code, which is easy to write.
- Function resizeimage ($ srcfile, $ mySize ){
-
- $ Size = getimagesize ($ srcfile );
-
- Switch ($ size [2]) {
-
- Case 1:
-
- $ Img = imagecreatefromgif ($ srcfile );
-
- Break;
-
- Case 2:
-
- $ Img = imagecreatefromjpeg ($ srcfile );
-
- Break;
-
- Case 3:
-
- $ Img = imagecreatefrompng ($ srcfile );
-
- Break;
-
- }
- // Width and height of the source image
-
- $ OldImg ['w'] = imagesx ($ img );
-
- $ OldImg ['H'] = imagesy ($ img );
-
- If ($ oldImg ['w'] <= $ mySize ['w'] & $ oldImg ['H'] <156 ){
-
- $ Rate = 1;
-
- } Elseif ($ oldImg ['w']> $ mySize ['w'] & $ oldImg ['H'] <$ mySize ['H']) {
-
- $ Rate = $ mySize ['w']/$ oldImg ['w'];
-
- } Elseif ($ oldImg ['w'] <$ mySize ['w'] & $ oldImg ['H']> $ mySize ['H']) {
-
- $ Rate = $ mySize ['H']/$ oldImg ['H'];
-
- } Elseif ($ oldImg ['w']> $ mySize ['w'] & $ oldImg ['H']> $ mySize ['H']) {
-
- $ Rate1 = $ mySize ['w']/$ oldImg ['w'];
-
- $ Rate2 = $ mySize ['H']/$ oldImg ['H'];
-
- If ($ rate1 >$ rate2) {$ rate = $ rate2;} else {$ rate = $ rate1 ;}
-
- }
-
- $ NewImg ['w'] = $ oldImg ['w'] * $ rate;
-
- $ NewImg ['H'] = $ oldImg ['H'] * $ rate;
-
- Return "width =". $ newImg ['w']. "height =". $ newImg ['H'];
-
- }
Application instance
- $mySize=array('w'=>143,'h'=>156);
- $imgSize=resizeimage("22.jpg",$mySize);
- echo ".resizeimage("22.jpg",$mySize)."/>";