首頁  >  文章  >  後端開發  >  php產生影像縮圖(支援:JPEG,GIT,PNG,BMP)

php產生影像縮圖(支援:JPEG,GIT,PNG,BMP)

WBOY
WBOY原創
2016-07-25 08:45:091287瀏覽
  1. class Thumb
  2. {
  3. public function create($srcPath, $dstPath, $dstWidth, $dstHeight)
  4. if (!file_exists($srcPath)) {
  5. return false;
  6. }
  7. @$srcSize = getimagesize($srcPath);
  8. if (empty($srcSize)) {
  9. return false;
  10. }
  11. $srcWith = intval($srcSize[0]);
  12. $srcHeight = intval($srcSize[1]);
  13. $srcHeight = intval($srcSize[1]);
  14. //如果原始圖片的尺寸大於指定縮圖的尺寸,則產生縮圖,否則拷貝原始圖片
  15. if ($srcWith return copy($ srcPath, $dstPath);
  16. }
  17. //讀取原始圖片資源
  18. @$srcImage = imagecreatefromjpeg($srcPath);
  19. if (empty($srcImage)) { @$srcImage = imagecreatefromgif($srcPath);
  20. }
  21. if (empty($srcImage)) {
  22. @$srcImage = imagecreatefrompng($srcPath);
  23. }
  24. }
  25. );
  26. } @$srcImage = $this->_imageCreateFromBMP($srcPath);
  27. }
  28. if (empty($srcImage)) {
  29. if (empty($srcImage)) {
  30. if (empty($srcImage)) {
  31. }
  32. //取得縮圖的尺寸,並據此產生新的圖片
  33. $dstSize = $this->_getDstSize(
  34. $srcWith, $srcHeight, $dstWidth, $dstHeight
  35. );
  36. @$dstImage = imagecreatetruecolor(
  37. $dstSize['width'], $dstSize['height']
  38. );

    $dstImage, $srcImage , 0, 0, 0, 0,

  39. $dstSize['width'], $dstSize['height'],
  40. $srcWith, $srcHeight
  41. );
  42. $srcWith, $srcHeight
  43. );
  44. );
  45. return @imagepng($srcPath, $dstPath);
  46. }
  47. private function _imageCreateFromBMP($filePath)
  48. {
  49. $fileHandle = fopendfilePath, 'fileHandle ;
  50. if (empty($fileHandle)) {
  51. return false;
  52. }
  53. $file = unpack(
  54. 'vfile_type/Vfile_size/Vreserved/Vbitmap(
  55. 'vfile_type/Vfile_size/Vreserved/Vbitmap_offset' fread($fileHandle, 14)
  56. );
  57. if ($file['file_type'] != 19778) {
  58. return false;
  59. }
  60. 'Vheader_size/Vwidth/Vheight/vplanes/'.
  61. 'vbits_per_pixel/Vcompression/Vsize_bitmap/'.
  62. 'Vhoriz_resolution/Vcolor_resperatution/Vcolor_fidofsed(3)(3D), fidunc_resport) 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. } $color[1] = $palette[$color[1] 1] ; }else if ($bmp['bits_per_pixel'] == 1) {
  104. $color = unpack('n', $vide . substr($image, Floor($p), 1));
  105. switch ( ($p * 8) % 8) {
  106. 情況0:
  107. $color[1] = ($color[1] >> 7);
  108. 中斷;
  109. 情況1:
  110. $color[1] = ($color[1] & 0x40) >>> 6;
  111. 中斷;
  112. 情況2:
  113. $color[1] = ($color[1] & 0x20) > >> 5;
  114. 中斷;
  115. 情況3:
  116. $color[1] = ($color[1] & 0x10) >>> 4;
  117. 中斷;
  118. 情況4:
  119. $color[1] = ($color[1] & 0x8) >>> 3;
  120. 中斷;
  121. 情況5:
  122. $color[1] = ($color[1] & 0x4 ) >>> 2;
  123. 中斷;
  124. 情況6:
  125. $color[1] = ($color[1] & 0x2) >>> 1;
  126. 中斷;
  127. 情況7 :
  128. $color[1] = ($color[1] & 0x1);
  129. 中斷;
  130. }
  131. $color[1] = $palette[$color[1] 1];
  132. } else {
  133. return false;
  134. }
  135. imagesetpixel($res, $x, $y, $color[1]);
  136. $x ;
  137. $p = $bmp ['bytes_per_pixel'];
  138. }
  139. $y--;
  140. $p = $bmp['decal'];
  141. }
  142. fclose($fileHandle);
  143. return $ res;
  144. }
  145. private function _getDstSize($srcWith, $srcHeight, $dstWidth, $dstHeight)
  146. {
  147. $size = array('width' = 'ight ' => $srcHeight);
  148. if ($dstWidth > 0 && $dstHeight > 0) {
  149. if ($srcWith > 0 && $srcHeight > 0) {
  150. if ($srcWith / $srcHeight > 0) {
  151. if ($srcWith / $srcHeight > = $dstWidth / $dstHeight) {
  152. if ($srcWith >; $dstWidth) {
  153. $size['width'] = $dstWidth;
  154. $size['height'] = $srcHeight * $srcHeight * dstWidth / $srcWith;
  155. }
  156. } else {
  157. if ($srcHeight > $dstHeight) {
  158. $size['width'] = $srcWith * $dstHeight / $srcight; $size['height'] = $dstHeight;
  159. }
  160. }
  161. }
  162. }
  163. return $size;
  164. }
  165. }
複製程式碼

JPEG、PHP、GIT


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn