Home  >  Article  >  Backend Development  >  Example of PHP scale image generation thumbnail function

Example of PHP scale image generation thumbnail function

WBOY
WBOYOriginal
2016-07-25 08:53:18894browse
  1. /*
  2. *@im //Picture resources that need to be scaled
  3. *@filetype //The thumbnail file type produced
  4. *@dstimW //The width of the scaled picture
  5. *@dstimH //Height of the zoomed picture
  6. *@thumbname //Thumbnail file name
  7. // WWW.JBXUE.COM
  8. function makethumb($im,$dstimW,$dstimH,$thumbname,$filetype){
  9. //Get im The width and height of
  10. $pic_W=imagesx($im);
  11. $pic_H=imagesy($im);
  12. $arr = array();
  13. swith($filetype){
  14. case 'jpg':
  15. $arr[$ filetype]="imagejpeg";
  16. break;
  17. case 'png';
  18. $arr[$filetype]="imagepng";
  19. break;
  20. case 'jif';
  21. $arr[$filetype]="imagegif";
  22. }
  23. if(($dstimgW && $dstimgW<$pic_W) || ($dstimgH && $dstimgH<$pic_H) ){
  24. if($dstimgW && $dstimgW<$pic_W){
  25. $dsimgWratio = $dstimgW / $pic_w ;
  26. $resizereagW =true;
  27. }
  28. if($dstimgH && $ $dstimgH <$pic_H){
  29. $dsimgHratio = $dstimgH/$pic_H;
  30. $resizerreagH =true;
  31. }
  32. //Thumbnail width and height For the original image aspect ratio, take the smallest one
  33. if($resizereagW && $resizerreagH){
  34. if($dsimgWratio<$dsimgHratio)
  35. $radio = $dsimgWratio;
  36. else
  37. $radio = $dsimgHratio;
  38. }
  39. if( $resizereagW && !$resizerreagH ){
  40. $radio = $dsimgWratio;
  41. }
  42. if(!$resizereagW && $resizerreagH){
  43. $radio = $dsimgHratio ;
  44. }
  45. $imgnewW = $pic_W * $radio;
  46. $imgnewH = $pic_H * $radio;
  47. if(function_exists("imgcopyresampled")){
  48. //Create the target resource canvas
  49. $dst = imagecreatetruecolor ($imgnewW, $imgnewH);
  50. imagecopyresampled ($dst,$im,0,0 ,0,0,$imgnewW,$imgnewH,$pic_W,$pic_H);
  51. }else{
  52. $dst=imagecreate($imgnewW, $imgnewH);
  53. imagecopyresized ($dst, $im,0,0,0, 0,$imgnewW,$imgnewH,$imgnewH,$pic_W,$pic_H);
  54. }
  55. $arr[$filetype]($dst,$thumbname.".$filetype");
  56. imagedestroy ($dst);
  57. } else{//The width and height of the thumbnail itself are already greater than the width and height of the original image
  58. //Then the width and height of the thumbnail are the width and height of the original image
  59. $arr[$filetype] ($im,$thumbname.".$filetype");
  60. imagedestroy();
  61. }
  62. }
  63. ?>
Copy code


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