PHP generated image thumbnails (support: jpeg,git,png,bmp)

Source: Internet
Author: User
Tags unpack
  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 picture is larger than the size of the specified thumbnail, the thumbnail is generated or the original file is copied
  15. if ($srcWith <= $dstWidth && $srcHeight <= $dstHeight) {
  16. return copy ($srcPath, $dstPath);
  17. }
  18. Read the original picture 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. Gets the size of the thumbnail and creates 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. }
Copy Code
JPEG, PHP, GIT
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.