Home  >  Article  >  Backend Development  >  Examples of how to enlarge and reduce php images in equal proportions

Examples of how to enlarge and reduce php images in equal proportions

WBOY
WBOYOriginal
2016-07-25 08:59:111073browse
  1. /**

  2. * Image proportional enlargement and reduction
  3. * @resizeimage
  4. * @param $srcfile source file size
  5. * @param $mySize size to be converted
  6. * edit bbs.it-home.org
  7. * at 2013/6/7
  8. */
  9. function resizeimage($srcfile,$mySize){
  10. $size=getimagesize($srcfile);
  11. switch($size[2]){
  12. case 1:
  13. $img=imagecreatefromgif($srcfile);
  14. break;
  15. case 2:
  16. $img=imagecreatefromjpeg($srcfile);
  17. break;
  18. case 3:
  19. $img=imagecreatefrompng($srcfile);
  20. break;
  21. }
  22. //源图片的宽度和高度
  23. $oldImg['w']=imagesx($img);
  24. $oldImg['h']=imagesy($img);
  25. if ($oldImg['w']<=$mySize['w'] && $oldImg['h']<156){
  26. $rate=1;
  27. }elseif ($oldImg['w']>$mySize['w'] && $oldImg['h']<$mySize['h']){
  28. $rate=$mySize['w']/$oldImg['w'];
  29. }elseif ($oldImg['w']<$mySize['w'] && $oldImg['h']>$mySize['h']){
  30. $rate=$mySize['h']/$oldImg['h'];
  31. }elseif ($oldImg['w']>$mySize['w'] && $oldImg['h']>$mySize['h']){
  32. $rate1=$mySize['w']/$oldImg['w'];
  33. $rate2=$mySize['h']/$oldImg['h'];
  34. if ($rate1>$rate2){$rate=$rate2;}else{$rate=$rate1;}
  35. }
  36. $newImg['w']=$oldImg['w']*$rate;
  37. $newImg['h']=$oldImg['h']*$rate;
  38. return "width=".$newImg['w']." height=".$newImg['h'];
  39. }

  40. //调用示例:

  41. $mySize=array('w'=>143,'h'=>156);
  42. $imgSize=resizeimage("22.jpg",$mySize);
  43. echo "";
  44. ?>

复制代码


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