当前位置:Gxlcms > PHP教程 > PHP生成图片缩略图类示例

PHP生成图片缩略图类示例

时间:2021-07-01 10:21:17 帮助过:47人阅读

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

  1. class App_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. public function __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. protected function getImageInfo($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. protected function getImageResource($fileName) {
  51. $image = null;
  52. if ( is_file($fileName) ) {
  53. switch ($this->imgMime) {
  54. case self::$imgMineList['jpeg']:
  55. $image = imagecreatefromjpeg($fileName);
  56. break;
  57. case self::$imgMineList['gif']:
  58. $image = imagecreatefromgif($fileName);
  59. break;
  60. case self::$imgMineList['png']:
  61. $image = imagecreatefrompng($fileName);
  62. break;
  63. case self::$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. protected function getSizeByScale($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. public function createImage($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. }

PS:这里再为大家推荐几款比较实用的图片处理工具供大家参考使用:

在线图片转换BASE64工具:
http://tools.jb51.net/transcoding/img2base64

ICO图标在线生成工具:
http://tools.jb51.net/aideddesign/ico_img

在线Email邮箱图标制作工具:
http://tools.jb51.net/email/emaillogo

在线图片格式转换(jpg/bmp/gif/png)工具:
http://tools.jb51.net/aideddesign/picext

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php面向对象程序设计入门教程》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

人气教程排行