PHP生成图片缩略图类示例

本文实例讲述了PHP生成图片缩略图类。分享给大家供大家参考,具体如下:

  1. classApp_image_helper {
  2. protected$imgFileName;
  3. protected$imgWidth;
  4. protected$imgHeight;
  5. protected$imgMime;
  6. protected$imgResource;
  7. static $imgMineList
  8. =array(
  9. 'jpeg'=>'image/jpeg',
  10. 'gif'=>'image/gif',
  11. 'png'=>'image/png',
  12. 'wbmp'=>'image/wbmp',
  13. );
  14. /**
  15. * 根据文件名,初始化图片,
  16. * 计算出给定图片的宽、高、图片类型,并获取图片的资源保存到内存,便于下次使用
  17. * App_image_helper constructor.
  18. *
  19. * @param $fileName
  20. */
  21. publicfunction__construct($fileName) {
  22. $this->imgFileName =$fileName;
  23. list($this->imgWidth,$this->imgHeight,$this->imgMime) =$this->getImageInfo($this->imgFileName);
  24. $this->imgResource =$this->getImageResource($this->imgFileName);
  25. }
  26. /**
  27. * 根据图片路径获取相关宽、高、MIME类型信息
  28. *
  29. * @param $fileName
  30. *
  31. * @return array|null
  32. */
  33. protectedfunctiongetImageInfo($fileName) {
  34. $result= null;
  35. if(is_file($fileName) ) {
  36. $tmpImageInfo=getimagesize($fileName);
  37. if($tmpImageInfo) {
  38. $result=array($tmpImageInfo[0],$tmpImageInfo[1],$tmpImageInfo['mime']);
  39. }
  40. }
  41. return$result;
  42. }
  43. /**
  44. * 将图片文件转为资源类类型
  45. *
  46. * @param $fileName
  47. *
  48. * @return null|resource
  49. */
  50. protectedfunctiongetImageResource($fileName) {
  51. $image= null;
  52. if(is_file($fileName) ) {
  53. switch($this->imgMime) {
  54. caseself::$imgMineList['jpeg']:
  55. $image= imagecreatefromjpeg($fileName);
  56. break;
  57. caseself::$imgMineList['gif']:
  58. $image= imagecreatefromgif($fileName);
  59. break;
  60. caseself::$imgMineList['png']:
  61. $image= imagecreatefrompng($fileName);
  62. break;
  63. caseself::$imgMineList['wbmp']:
  64. $image= imagecreatefromwbmp($fileName);
  65. break;
  66. default:
  67. break;
  68. }
  69. }
  70. return$image;
  71. }
  72. /**
  73. * 可根据固定宽,等比缩放图片;或根据百分比,等比缩放图片
  74. *
  75. * @param int $width
  76. * @param int $percent
  77. *
  78. * @return array|null
  79. */
  80. protectedfunctiongetSizeByScale($width= 360,$percent= 1) {
  81. $result= null;
  82. if($this->imgWidth &&$this->imgHeight ) {
  83. if($width) {
  84. $result=array($width,intval($width*$this->imgHeight /$this->imgWidth));
  85. }elseif($percent) {
  86. $result=array(intval($this->imgWidth *$percent),intval($this->imgHeight *$percent));
  87. }
  88. }
  89. return$result;
  90. }
  91. /**
  92. * 外调
  93. *
  94. * @param int $percentOrWidth int整数表示图片缩放为固定宽度,0.0~0.99999表示缩放百分比
  95. * @param null $fileName
  96. * @param int $quality
  97. * @param bool $reSample 重新采样图片,默认是
  98. *
  99. * @return bool
  100. */
  101. publicfunctioncreateImage($percentOrWidth= 1,$fileName= null,$quality= 75,$reSample= true) {
  102. $result= false;
  103. $fileName? header('Content-Type: '.$this->imgMime) : false;
  104. $size=$this->getSizeByScale(($percentOrWidth<= 1) ? null :$percentOrWidth,$percentOrWidth);
  105. if($size) {
  106. $thumb= imagecreatetruecolor($size[0],$size[1]);
  107. if($reSample) {
  108. imagecopyresampled($thumb,$this->imgResource, 0, 0, 0, 0,$size[0],$size[1],$this->imgWidth,$this->imgHeight);
  109. }else{
  110. imagecopyresized($thumb,$this->imgResource, 0, 0, 0, 0,$size[0],$size[1],$this->imgWidth,$this->imgHeight);
  111. }
  112. $result= imagejpeg($thumb,$fileName,$quality);
  113. }
  114. return$result;
  115. }
  116. }