Home >Backend Development >PHP Tutorial >Use PHP to enlarge and reduce images in equal proportions

Use PHP to enlarge and reduce images in equal proportions

WBOY
WBOYOriginal
2016-07-25 08:43:141251browse
  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. //源图片的宽度和高度
  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. }
复制代码

应用实例
  1. $mySize=array('w'=>143,'h'=>156);
  2. $imgSize=resizeimage("22.jpg",$mySize);
  3. echo "";
复制代码

大和, 等比例, PHP


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn