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

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