Php generates image thumbnails (supported: JPEG, GIT, PNG, BMP)

Source: Internet
Author: User
Php generates image thumbnails (supported: JPEG, GIT, PNG, BMP)

  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 greater than the size of the specified thumbnail, a thumbnail is generated. Otherwise, the original file is copied.
  15. If ($ srcWith <= $ dstWidth & $ srcHeight <= $ dstHeight ){
  16. Return copy ($ srcPath, $ dstPath );
  17. }
  18. // Read original image resources
  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. // Obtain the thumbnail size 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 ['desc'] = $ bmp ['width'] * $ bmp ['bytes _ per_pixel ']/4;
  72. $ Bmp ['desc']-= floor ($ bmp ['width'] * $ bmp ['bytes _ per_pixel ']/4 );
  73. $ Bmp ['demo'] = 4-(4 * $ bmp ['demo']);
  74. If ($ bmp ['desc'] = 4 ){
  75. $ Bmp ['desc'] = 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 ['demo'];
  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

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.