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

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

JPEG, php, GIT


Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage