Home  >  Article  >  Backend Development  >  php generates image thumbnails (supports: JPEG, GIT, PNG, BMP)

php generates image thumbnails (supports: JPEG, GIT, PNG, BMP)

WBOY
WBOYOriginal
2016-07-25 08:45:091184browse
  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. //If the size of the original image is larger than the size of the specified thumbnail, generate a thumbnail, otherwise copy the original file
  15. if ($srcWith <= $dstWidth && $srcHeight <= $dstHeight) {
  16. return copy($srcPath, $dstPath);
  17. }
  18. //Read the original image resource
  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. //Get the size of the thumbnail and generate a new image accordingly
  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


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