Use PHP to scale up or down images

Source: Internet
Author: User
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.

 

 
  1. Function resizeimage ($ srcfile, $ mySize ){
  2. $ Size = getimagesize ($ srcfile );
  3. Switch ($ size [2]) {
  4. Case 1:
  5. $ Img = imagecreatefromgif ($ srcfile );
  6. Break;
  7. Case 2:
  8. $ Img = imagecreatefromjpeg ($ srcfile );
  9. Break;
  10. Case 3:
  11. $ Img = imagecreatefrompng ($ srcfile );
  12. Break;
  13. }
  14. // Width and height of the source image
  15. $ OldImg ['w'] = imagesx ($ img );
  16. $ OldImg ['H'] = imagesy ($ img );
  17. If ($ oldImg ['w'] <= $ mySize ['w'] & $ oldImg ['H'] <156 ){
  18. $ Rate = 1;
  19. } Elseif ($ oldImg ['w']> $ mySize ['w'] & $ oldImg ['H'] <$ mySize ['H']) {
  20. $ Rate = $ mySize ['w']/$ oldImg ['w'];
  21. } Elseif ($ oldImg ['w'] <$ mySize ['w'] & $ oldImg ['H']> $ mySize ['H']) {
  22. $ Rate = $ mySize ['H']/$ oldImg ['H'];
  23. } Elseif ($ oldImg ['w']> $ mySize ['w'] & $ oldImg ['H']> $ mySize ['H']) {
  24. $ Rate1 = $ mySize ['w']/$ oldImg ['w'];
  25. $ Rate2 = $ mySize ['H']/$ oldImg ['H'];
  26. If ($ rate1 >$ rate2) {$ rate = $ rate2;} else {$ rate = $ rate1 ;}
  27. }
  28. $ NewImg ['w'] = $ oldImg ['w'] * $ rate;
  29. $ NewImg ['H'] = $ oldImg ['H'] * $ rate;
  30. Return "width =". $ newImg ['w']. "height =". $ newImg ['H'];
  31. }

 

Application instance

 

 
  1. $mySize=array('w'=>143,'h'=>156);
  2. $imgSize=resizeimage("22.jpg",$mySize);
  3. echo ".resizeimage("22.jpg",$mySize)."/>"; 

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.