• 技术文章 >后端开发 >php教程

    php生成图像缩略图(支持:JPEG,GIT,PNG,BMP)

    2016-07-25 08:45:09原创660
    1. class Thumb
    2. {
    3. public function create($srcPath, $dstPath, $dstWidth, $dstHeight)
    4. {
    5. if (!file_exists($srcPath)) {
    6. return false;
    7. }
    8. @$srcSize = getimagesize($srcPath);
    9. if (empty($srcSize)) {
    10. return false;
    11. }
    12. $srcWith = intval($srcSize[0]);
    13. $srcHeight = intval($srcSize[1]);
    14. //如果原始图片的尺寸大于指定缩略图的尺寸,则生成缩略图,否则拷贝原始文件
    15. if ($srcWith <= $dstWidth && $srcHeight <= $dstHeight) {
    16. return copy($srcPath, $dstPath);
    17. }
    18. //读取原始图片资源
    19. @$srcImage = imagecreatefromjpeg($srcPath);
    20. if (empty($srcImage)) {
    21. @$srcImage = imagecreatefromgif($srcPath);
    22. }
    23. if (empty($srcImage)) {
    24. @$srcImage = imagecreatefrompng($srcPath);
    25. }
    26. if (empty($srcImage)) {
    27. @$srcImage = $this->_imageCreateFromBMP($srcPath);
    28. }
    29. if (empty($srcImage)) {
    30. return false;
    31. }
    32. //获取缩略图的尺寸,并据此生成新的图像
    33. $dstSize = $this->_getDstSize(
    34. $srcWith, $srcHeight, $dstWidth, $dstHeight
    35. );
    36. @$dstImage = imagecreatetruecolor(
    37. $dstSize['width'], $dstSize['height']
    38. );
    39. @imagecopyresampled(
    40. $dstImage, $srcImage , 0, 0, 0, 0,
    41. $dstSize['width'], $dstSize['height'],
    42. $srcWith, $srcHeight
    43. );
    44. return @imagepng($srcPath, $dstPath);
    45. }
    46. private function _imageCreateFromBMP($filePath)
    47. {
    48. $fileHandle = fopen($filePath, 'rb');
    49. if (empty($fileHandle)) {
    50. return false;
    51. }
    52. $file = unpack(
    53. 'vfile_type/Vfile_size/Vreserved/Vbitmap_offset',
    54. fread($fileHandle, 14)
    55. );
    56. if ($file['file_type'] != 19778) {
    57. return false;
    58. }
    59. $bmp = unpack(
    60. 'Vheader_size/Vwidth/Vheight/vplanes/'.
    61. 'vbits_per_pixel/Vcompression/Vsize_bitmap/'.
    62. 'Vhoriz_resolution/Vvert_resolution/Vcolors_used/Vcolors_important',
    63. fread($fileHandle, 40)
    64. );
    65. $bmp['colors'] = pow(2, $bmp['bits_per_pixel']);
    66. if ($bmp['size_bitmap'] == 0) {
    67. $bmp['size_bitmap'] = $file['file_size'] - $file['bitmap_offset'];
    68. }
    69. $bmp['bytes_per_pixel'] = $bmp['bits_per_pixel'] / 8;
    70. $bmp['bytes_per_pixel2'] = ceil($bmp['bytes_per_pixel']);
    71. $bmp['decal'] = $bmp['width'] * $bmp['bytes_per_pixel'] / 4;
    72. $bmp['decal'] -= floor($bmp['width'] * $bmp['bytes_per_pixel'] / 4);
    73. $bmp['decal'] = 4 - (4 * $bmp['decal']);
    74. if ($bmp['decal'] == 4) {
    75. $bmp['decal'] = 0;
    76. }
    77. $palette = array();
    78. if ($bmp['colors'] < 16777216) {
    79. $palette = unpack(
    80. 'V' . $bmp['colors'],
    81. fread($fileHandle, $bmp['colors'] * 4)
    82. );
    83. }
    84. $image = fread($fileHandle, $bmp['size_bitmap']);
    85. $vide = chr(0);
    86. $res = imagecreatetruecolor($bmp['width'], $bmp['height']);
    87. $p = 0;
    88. $y = $bmp['height'] - 1;
    89. while ($y >= 0) {
    90. $x = 0;
    91. while ($x < $bmp['width']) {
    92. if ($bmp['bits_per_pixel'] == 24) {
    93. $color = unpack('V', substr($image, $p, 3) . $vide);
    94. } else if ($bmp['bits_per_pixel'] == 16) {
    95. $color = unpack('n', substr($image, $p, 2));
    96. $color[1] = $palette[$color[1]+1];
    97. } else if ($bmp['bits_per_pixel'] == 8) {
    98. $color = unpack('n', $vide . substr ($image, $p, 1));
    99. $color[1] = $palette[$color[1]+1];
    100. } else if ($bmp['bits_per_pixel'] ==4) {
    101. $color = unpack('n', $vide . substr($image, floor($p), 1));
    102. if (($p * 2) % 2 == 0) {
    103. $color[1] = ($color[1] >> 4);
    104. } else {
    105. $color[1] = ($color[1] & 0x0F);
    106. }
    107. $color[1] = $palette[$color[1] + 1];
    108. } else if ($bmp['bits_per_pixel'] == 1) {
    109. $color = unpack('n', $vide . substr($image, floor($p), 1));
    110. switch (($p * 8) % 8) {
    111. case 0:
    112. $color[1] = ($color[1] >> 7);
    113. break;
    114. case 1:
    115. $color[1] = ($color[1] & 0x40) >> 6;
    116. break;
    117. case 2:
    118. $color[1] = ($color[1] & 0x20) >> 5;
    119. break;
    120. case 3:
    121. $color[1] = ($color[1] & 0x10) >> 4;
    122. break;
    123. case 4:
    124. $color[1] = ($color[1] & 0x8) >> 3;
    125. break;
    126. case 5:
    127. $color[1] = ($color[1] & 0x4) >> 2;
    128. break;
    129. case 6:
    130. $color[1] = ($color[1] & 0x2) >> 1;
    131. break;
    132. case 7:
    133. $color[1] = ($color[1] & 0x1);
    134. break;
    135. }
    136. $color[1] = $palette[$color[1] + 1];
    137. } else {
    138. return false;
    139. }
    140. imagesetpixel($res, $x, $y, $color[1]);
    141. $x++;
    142. $p += $bmp['bytes_per_pixel'];
    143. }
    144. $y--;
    145. $p += $bmp['decal'];
    146. }
    147. fclose($fileHandle);
    148. return $res;
    149. }
    150. private function _getDstSize($srcWith, $srcHeight, $dstWidth, $dstHeight)
    151. {
    152. $size = array('width' => $srcWith, 'height' => $srcHeight);
    153. if ($dstWidth > 0 && $dstHeight > 0) {
    154. if ($srcWith > 0 && $srcHeight > 0) {
    155. if ($srcWith / $srcHeight >= $dstWidth / $dstHeight) {
    156. if ($srcWith > $dstWidth) {
    157. $size['width'] = $dstWidth;
    158. $size['height'] = $srcHeight * $dstWidth / $srcWith;
    159. }
    160. } else {
    161. if ($srcHeight > $dstHeight) {
    162. $size['width'] = $srcWith * $dstHeight / $srcHeight;
    163. $size['height'] = $dstHeight;
    164. }
    165. }
    166. }
    167. }
    168. return $size;
    169. }
    170. }
    复制代码

    JPEG, php, GIT


    php入门到就业线上直播课:查看学习

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    自己动手写 PHP MVC 框架:点击学习

    快速了解MVC架构、了解框架底层运行原理

    专题推荐:php生成图像缩略图(支持:JPEG,GIT,PNG,BMP)
    上一篇:php的文件上传示例代码 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• 你知道如何用PHP实现多进程吗• PHP中全面阻止SQL注入式攻击分析小结_PHP教程• smarty中英文多编码字符截取乱码问题解决方法• php实现设计模式中的单例模式详解_php技巧• YII Framework框架教程之使用YIIC快速创建YII应用详解_PHP
    1/1

    PHP中文网