当前位置:首页 > PHP教程 > php函数 > 列表

php将bmp格式图片转换成jpg格式程序

发布:smiling 来源: PHP粉丝网  添加日期:2014-09-09 15:30:35 浏览: 评论:0 
  1. function imagebmp($img,$file="",$rle=0) 
  2. { 
  3.  
  4.  
  5. $colorcount=imagecolorstotal($img); 
  6.  
  7. $transparent=imagecolortransparent($img); 
  8. $istransparent=$transparent!=-1; 
  9.  
  10.  
  11. if($istransparent) $colorcount--; 
  12.  
  13. if($colorcount==0) {$colorcount=0; $bitcount=24;}; 
  14. if(($colorcount>0)and($colorcount<=2)) {$colorcount=2; $bitcount=1;}; 
  15. if(($colorcount>2)and($colorcount<=16)) { $colorcount=16; $bitcount=4;}; 
  16. if(($colorcount>16)and($colorcount<=256)) { $colorcount=0; $bitcount=8;}; 
  17.  
  18.  
  19.                 $width=imagesx($img); 
  20.                 $height=imagesy($img); 
  21.  
  22.                 $zbytek=(4-($width/(8/$bitcount))%4)%4; 
  23.  
  24.                 if($bitcount<24) $palsize=pow(2,$bitcount)*4; 
  25.  
  26.                 $size=(floor($width/(8/$bitcount))+$zbytek)*$height+54; 
  27.                 $size+=$palsize; 
  28.                 $offset=54+$palsize; 
  29.  
  30.                 // bitmap file header 
  31.                 $ret = 'bm';                        // header (2b) 
  32.                 $ret .= int_to_dword($size);        // size of file (4b) 
  33.                 $ret .= int_to_dword(0);        // reserved (4b) 
  34.                 $ret .= int_to_dword($offset);        // byte location in the file which is first byte of image (4b) 
  35.                 // bitmap info header 
  36.                 $ret .= int_to_dword(40);        // size of bitmapinfoheader (4b) 
  37.                 $ret .= int_to_dword($width);        // width of bitmap (4b) 
  38.                 $ret .= int_to_dword($height);        // height of bitmap (4b) 
  39.                 $ret .= int_to_word(1);        // biplanes = 1 (2b) 
  40.                 $ret .= int_to_word($bitcount);        // bibitcount = {1 (mono) or 4 (16 clr ) or 8 (256 clr) or 24 (16 mil)} (2b) 
  41.                 $ret .= int_to_dword($rle);        // rle compression (4b) 
  42.                 $ret .= int_to_dword(0);        // width x height (4b) 
  43.                 $ret .= int_to_dword(0);        // bixpelspermeter (4b) 
  44.                 $ret .= int_to_dword(0);        // biypelspermeter (4b) 
  45.                 $ret .= int_to_dword(0);        // number of palettes used (4b) 
  46.                 $ret .= int_to_dword(0);        // number of important colour (4b) 
  47.                 // image data 
  48.  
  49.                 $cc=$colorcount; 
  50.                 $sl1=strlen($ret); 
  51.                 if($cc==0) $cc=256; 
  52.                 if($bitcount<24) 
  53.                    { 
  54.                     $colortotal=imagecolorstotal($img); 
  55.                      if($istransparent) $colortotal--; 
  56.  
  57.                      for($p=0;$p<$colortotal;$p++) 
  58.                      { 
  59.                       $color=imagecolorsforindex($img,$p); 
  60.                        $ret.=inttobyte($color["blue"]); 
  61.                        $ret.=inttobyte($color["green"]); 
  62.                        $ret.=inttobyte($color["red"]); 
  63.                        $ret.=inttobyte(0); //reserved 
  64.                      }; 
  65.  
  66.                     $ct=$colortotal; 
  67.                   for($p=$colortotal;$p<$cc;$p++) 
  68.                        { 
  69.                       $ret.=inttobyte(0); 
  70.                       $ret.=inttobyte(0); 
  71.                       $ret.=inttobyte(0); 
  72.                       $ret.=inttobyte(0); //reserved 
  73.                      }; 
  74.                    }; 
  75.  
  76.  
  77. if($bitcount<=8) 
  78. { 
  79.  
  80.  for($y=$height-1;$y>=0;$y--) 
  81.  { 
  82.   $bwrite=""; 
  83.   for($x=0;$x<$width;$x++) 
  84.    { 
  85.    $color=imagecolorat($img,$x,$y); 
  86.    $bwrite.=decbinx($color,$bitcount); 
  87.    if(strlen($bwrite)==8) 
  88.     { 
  89.      $retd.=inttobyte(bindec($bwrite)); 
  90.      $bwrite=""; 
  91.     }; 
  92.    }; 
  93.  
  94.   if((strlen($bwrite)<8)and(strlen($bwrite)!=0)) 
  95.     { 
  96.      $sl=strlen($bwrite); 
  97.      for($t=0;$t<8-$sl;$t++) 
  98.       $sl.="0"; 
  99.      $retd.=inttobyte(bindec($bwrite)); 
  100.     }; 
  101.  for($z=0;$z<$zbytek;$z++) 
  102.    $retd.=inttobyte(0); 
  103.  }; 
  104. }; 
  105.  
  106. if(($rle==1)and($bitcount==8)) 
  107. { 
  108.  for($t=0;$t<strlen($retd);$t+=4) 
  109.   { 
  110.    if($t!=0) 
  111.    if(($t)%$width==0) 
  112.     $ret.=chr(0).chr(0); 
  113.  
  114.    if(($t+5)%$width==0) 
  115.    { 
  116.      $ret.=chr(0).chr(5).substr($retd,$t,5).chr(0); 
  117.      $t+=1; 
  118.    } 
  119.    if(($t+6)%$width==0) 
  120.     { 
  121.      $ret.=chr(0).chr(6).substr($retd,$t,6); 
  122.      $t+=2; 
  123.     } 
  124.     else 
  125.     { 
  126.      $ret.=chr(0).chr(4).substr($retd,$t,4); 
  127.     }; 
  128.   }; 
  129.   $ret.=chr(0).chr(1); 
  130. } 
  131. else 
  132. { 
  133. $ret.=$retd; 
  134. }; 
  135.  
  136.  
  137.                 if($bitcount==24) 
  138.                 { 
  139.                 for($z=0;$z<$zbytek;$z++) 
  140.                  $dopl.=chr(0); 
  141.  
  142.                 for($y=$height-1;$y>=0;$y--) 
  143.                  { 
  144.                  for($x=0;$x<$width;$x++) 
  145.                         { 
  146.                            $color=imagecolorsforindex($img,imagecolorat($img,$x,$y)); 
  147.                            $ret.=chr($color["blue"]).chr($color["green"]).chr($color["red"]); 
  148.                         } 
  149.                  $ret.=$dopl; 
  150.                  }; 
  151.  
  152.                  }; 
  153.  
  154.   if($file!="") 
  155.    { 
  156.     $r=($f=fopen($file,"w")); 
  157.     $r=$r and fwrite($f,$ret); 
  158.     $r=$r and fclose($f); 
  159.     return $r; 
  160.    } 
  161.   else 
  162.   { 
  163.    echo $ret; 
  164.   }; 
  165. }; 
  166.  
  167.  
  168. /* 
  169. *------------------------------------------------------------ 
  170. *                    imagecreatefrombmp 
  171. *------------------------------------------------------------ 
  172. *            - reads image from a bmp file 
  173. * 
  174. *         parameters:  $file - target file to load 
  175. * 
  176. *            returns: image id 
  177. */ 
  178.  
  179. function imagecreatefrombmp($file) 
  180. { 
  181. global  $currentbit, $echomode; 
  182.  
  183. $f=fopen($file,"r"); 
  184. $header=fread($f,2); 
  185.  
  186. if($header=="bm") 
  187. { 
  188.  $size=freaddword($f); 
  189.  $reserved1=freadword($f); 
  190.  $reserved2=freadword($f); 
  191.  $firstbyteofimage=freaddword($f); 
  192.  
  193.  $sizebitmapinfoheader=freaddword($f); 
  194.  $width=freaddword($f); 
  195.  $height=freaddword($f); 
  196.  $biplanes=freadword($f); 
  197.  $bibitcount=freadword($f); 
  198.  $rlecompression=freaddword($f); 
  199.  $widthxheight=freaddword($f); 
  200.  $bixpelspermeter=freaddword($f); 
  201.  $biypelspermeter=freaddword($f); 
  202.  $numberofpalettesused=freaddword($f); 
  203.  $numberofimportantcolors=freaddword($f); 
  204.  
  205. if($bibitcount<24) 
  206.  { 
  207.   $img=imagecreate($width,$height); 
  208.   $colors=pow(2,$bibitcount); 
  209.   for($p=0;$p<$colors;$p++) 
  210.    { 
  211.     $b=freadbyte($f); 
  212.     $g=freadbyte($f); 
  213.     $r=freadbyte($f); 
  214.     $reserved=freadbyte($f); 
  215.     $palette[]=imagecolorallocate($img,$r,$g,$b); 
  216.    }; 
  217.  
  218.   
  219.  
  220.  
  221. if($rlecompression==0) 
  222. { 
  223.    $zbytek=(4-ceil(($width/(8/$bibitcount)))%4)%4; 
  224.  
  225. for($y=$height-1;$y>=0;$y--) 
  226.     { 
  227.      $currentbit=0; 
  228.      for($x=0;$x<$width;$x++) 
  229.       { 
  230.          $c=freadbits($f,$bibitcount); 
  231.        imagesetpixel($img,$x,$y,$palette[$c]); 
  232.       }; 
  233.     if($currentbit!=0) {freadbyte($f);}; 
  234.     for($g=0;$g<$zbytek;$g++) 
  235.      freadbyte($f); 
  236.      }; 
  237.  
  238.  }; 
  239. }; 
  240.  
  241.  
  242. if($rlecompression==1) //$bi_rle8 
  243. { 
  244. $y=$height; 
  245.  
  246. $pocetb=0; 
  247.  
  248. while(true) 
  249. { 
  250. $y--; 
  251. $prefix=freadbyte($f); 
  252. $suffix=freadbyte($f); 
  253. $pocetb+=2; 
  254.  
  255. $echoit=false; 
  256.  
  257. if($echoit)echo "prefix: $prefix suffix: $suffix<br>"; 
  258. if(($prefix==0)and($suffix==1)) break; 
  259. if(feof($f)) break; 
  260.  
  261. while(!(($prefix==0)and($suffix==0))) 
  262. { 
  263.  if($prefix==0) 
  264.   { 
  265.    $pocet=$suffix; 
  266.    $data.=fread($f,$pocet); 
  267.    $pocetb+=$pocet; 
  268.    if($pocetb%2==1) {freadbyte($f); $pocetb++;}; 
  269.   }; 
  270.  if($prefix>0) 
  271.   { 
  272.    $pocet=$prefix; 
  273.    for($r=0;$r<$pocet;$r++) 
  274.     $data.=chr($suffix); 
  275.   }; 
  276.  $prefix=freadbyte($f); 
  277.  $suffix=freadbyte($f); 
  278.  $pocetb+=2; 
  279.  if($echoit) echo "prefix: $prefix suffix: $suffix<br>"; 
  280. }; 
  281.  
  282. for($x=0;$x<strlen($data);$x++) 
  283.  { 
  284.   imagesetpixel($img,$x,$y,$palette[ord($data[$x])]); 
  285.  }; 
  286. $data=""; 
  287.  
  288. }; 
  289.  
  290. }; 
  291.  
  292.  
  293. if($rlecompression==2) //$bi_rle4 
  294. { 
  295. $y=$height; 
  296. $pocetb=0; 
  297.  
  298. /*while(!feof($f)) 
  299.  echo freadbyte($f)."_".freadbyte($f)."<br>";*/ 
  300. while(true) 
  301. { 
  302. //break; 
  303. $y--; 
  304. $prefix=freadbyte($f); 
  305. $suffix=freadbyte($f); 
  306. $pocetb+=2; 
  307.  
  308. $echoit=false; 
  309.  
  310. if($echoit)echo "prefix: $prefix suffix: $suffix<br>"; 
  311. if(($prefix==0)and($suffix==1)) break; 
  312. if(feof($f)) break; 
  313.  
  314. while(!(($prefix==0)and($suffix==0))) 
  315. { 
  316.  if($prefix==0) 
  317.   { 
  318.    $pocet=$suffix; 
  319.  
  320.    $currentbit=0; 
  321.    for($h=0;$h<$pocet;$h++) 
  322.     $data.=chr(freadbits($f,4)); 
  323.    if($currentbit!=0) freadbits($f,4); 
  324.    $pocetb+=ceil(($pocet/2)); 
  325.    if($pocetb%2==1) {freadbyte($f); $pocetb++;}; 
  326.   }; 
  327.  if($prefix>0) 
  328.   { 
  329.    $pocet=$prefix; 
  330.    $i=0; 
  331.    for($r=0;$r<$pocet;$r++) 
  332.     { 
  333.     if($i%2==0) 
  334.      { 
  335.       $data.=chr($suffix%16); 
  336.      } 
  337.      else 
  338.      { 
  339.       $data.=chr(floor($suffix/16)); 
  340.      }; 
  341.     $i++; 
  342.     }; 
  343.   }; 
  344.  $prefix=freadbyte($f); 
  345.  $suffix=freadbyte($f); 
  346.  $pocetb+=2; 
  347.  if($echoit) echo "prefix: $prefix suffix: $suffix<br>"; 
  348. }; 
  349.  
  350. for($x=0;$x<strlen($data);$x++) 
  351.  { 
  352.   imagesetpixel($img,$x,$y,$palette[ord($data[$x])]); 
  353.  }; 
  354. $data=""; 
  355.  
  356. }; 
  357.  
  358. }; 
  359.  
  360.  
  361.  if($bibitcount==24) 
  362. { 
  363.  $img=imagecreatetruecolor($width,$height); 
  364.  $zbytek=$width%4; 
  365.  
  366.    for($y=$height-1;$y>=0;$y--) 
  367.     { 
  368.      for($x=0;$x<$width;$x++) 
  369.       { 
  370.        $b=freadbyte($f); 
  371.        $g=freadbyte($f); 
  372.        $r=freadbyte($f); 
  373.        $color=imagecolorexact($img,$r,$g,$b); 
  374.        if($color==-1) $color=imagecolorallocate($img,$r,$g,$b); 
  375.        imagesetpixel($img,$x,$y,$color); 
  376.       } 
  377.     for($z=0;$z<$zbytek;$z++) 
  378.      freadbyte($f); 
  379.    }; 
  380. }; 
  381. return $img; 
  382.  
  383. }; 
  384.  
  385.  
  386. fclose($f); 
  387.  
  388.  
  389. }; 
  390.  
  391.   
  392.  
  393.   
  394.  
  395. /* 
  396. * helping functions: 
  397. *------------------------- 
  398. * 
  399. * freadbyte($file) - reads 1 byte from $file 
  400. * freadword($file) - reads 2 bytes (1 word) from $file 
  401. * freaddword($file) - reads 4 bytes (1 dword) from $file 
  402. * freadlngint($file) - same as freaddword($file) 
  403. * decbin8($d) - returns binary string of d zero filled to 8 
  404. * retbits($byte,$start,$len) - returns bits $start->$start+$len from $byte 
  405. * freadbits($file,$count) - reads next $count bits from $file 
  406. * rgbtohex($r,$g,$b) - convert $r, $g, $b to hex 
  407. * int_to_dword($n) - returns 4 byte representation of $n 
  408. * int_to_word($n) - returns 2 byte representation of $n 
  409. */ 
  410.  
  411. function freadbyte($f) 
  412. { 
  413.  return ord(fread($f,1)); 
  414. }; 
  415.  
  416. function freadword($f) 
  417. { 
  418.  $b1=freadbyte($f); 
  419.  $b2=freadbyte($f); 
  420.  return $b2*256+$b1; 
  421. }; 
  422.  
  423.  
  424. function freadlngint($f) 
  425. { 
  426. return freaddword($f); 
  427. }; 
  428.  
  429. function freaddword($f) 
  430. { 
  431.  $b1=freadword($f); 
  432.  $b2=freadword($f); 
  433.  return $b2*65536+$b1; 
  434. }; 
  435.  
  436.   
  437.  
  438. function retbits($byte,$start,$len) 
  439. { 
  440. $bin=decbin8($byte); 
  441. $r=bindec(substr($bin,$start,$len)); 
  442. return $r; 
  443.  
  444. }; 
  445.  
  446.   
  447.  
  448. $currentbit=0; 
  449. function freadbits($f,$count) 
  450. { 
  451.  global $currentbit,$smode; 
  452.  $byte=freadbyte($f); 
  453.  $lastcbit=$currentbit; 
  454.  $currentbit+=$count; 
  455.  if($currentbit==8) 
  456.   { 
  457.    $currentbit=0; 
  458.   } 
  459.  else 
  460.   { 
  461.    fseek($f,ftell($f)-1); 
  462.   }; 
  463.  return retbits($byte,$lastcbit,$count); 
  464. }; 
  465.  
  466.   
  467.  
  468. function rgbtohex($red,$green,$blue) 
  469.   { 
  470.    $hred=dechex($red);if(strlen($hred)==1) $hred="0$hred"; 
  471.    $hgreen=dechex($green);if(strlen($hgreen)==1) $hgreen="0$hgreen"; 
  472.    $hblue=dechex($blue);if(strlen($hblue)==1) $hblue="0$hblue"; 
  473.    return($hred.$hgreen.$hblue); 
  474.   }; 
  475.  
  476.         function int_to_dword($n) 
  477.         { 
  478.                 return chr($n & 255).chr(($n >> 8) & 255).chr(($n >> 16) & 255).chr(($n >> 24) & 255); 
  479.         } 
  480.         function int_to_word($n) 
  481.         { 
  482.                 return chr($n & 255).chr(($n >> 8) & 255); 
  483.         } 
  484.  
  485.  
  486. function decbin8($d) 
  487. { 
  488. return decbinx($d,8); 
  489. }; 
  490.  
  491. function decbinx($d,$n) 
  492. { 
  493. $bin=decbin($d); 
  494. $sbin=strlen($bin); 
  495. for($j=0;$j<$n-$sbin;$j++) 
  496.  $bin="0$bin"; 
  497. return $bin; 
  498. }; 
  499.  
  500. function inttobyte($n) 
  501. {//开源代码phpfensi.com 
  502. return chr($n); 
  503. }; 
  504. //实例方法,代码如下: 
  505. include_once('bmp.php'); 
  506. $image=imagecreatefrombmp('a.bmp'); 
  507. imagejpeg($image,'a.jpeg'); 
  508. imagedestroy($image);  

Tags: bmp格式转换 jpg格式程序

分享到: