PageRenderTime 60ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/ext_scripts/gd/ico.php

http://lansuite.googlecode.com/
PHP | 729 lines | 694 code | 17 blank | 18 comment | 15 complexity | 3097d4f31b14615948265eb53e03624d MD5 | raw file
Possible License(s): LGPL-3.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /*
  3. *------------------------------------------------------------
  4. * ICO Image functions
  5. *------------------------------------------------------------
  6. * By JPEXS
  7. */
  8. define("TRUE_COLOR", 16777216);
  9. define("XP_COLOR", 4294967296);
  10. define("MAX_COLOR", -2);
  11. define("MAX_SIZE", -2);
  12. /*
  13. *------------------------------------------------------------
  14. * ImageCreateFromIco
  15. *------------------------------------------------------------
  16. * - Reads image from a ICO file
  17. *
  18. * Parameters: $filename - Target ico file to load
  19. * $icoColorCount - Icon color count (For multiple icons ico file)
  20. * - 2,16,256, TRUE_COLOR or XP_COLOR
  21. * $icoSize - Icon width (For multiple icons ico file)
  22. * Returns: Image ID
  23. */
  24. function ImageCreateFromIco($filename,$icoColorCount=16,$icoSize=16)
  25. {
  26. $Ikona=GetIconsInfo($filename);
  27. $IconID=-1;
  28. $ColMax=-1;
  29. $SizeMax=-1;
  30. for($p=0;$p<count($Ikona);$p++)
  31. {
  32. $Ikona[$p]["NumberOfColors"]=pow(2,$Ikona[$p]["Info"]["BitsPerPixel"]);
  33. };
  34. for($p=0;$p<count($Ikona);$p++)
  35. {
  36. if(($ColMax==-1)or($Ikona[$p]["NumberOfColors"]>$Ikona[$ColMax]["NumberOfColors"]))
  37. if(($icoSize==$Ikona[$p]["Width"])or($icoSize==-2))
  38. {
  39. $ColMax=$p;
  40. };
  41. if(($SizeMax==-1)or($Ikona[$p]["Width"]>$Ikona[$SizeMax]["Width"]))
  42. if(($icoColorCount==$Ikona[$p]["NumberOfColors"])or($icoColorCount==-2))
  43. {
  44. $SizeMax=$p;
  45. };
  46. if($Ikona[$p]["NumberOfColors"]==$icoColorCount)
  47. if($Ikona[$p]["Width"]==$icoSize)
  48. {
  49. $IconID=$p;
  50. };
  51. };
  52. if($icoSize==-2) $IconID=$SizeMax;
  53. if($icoColorCount==-2) $IconID=$ColMax;
  54. $ColName=$icoColorCount;
  55. if($icoSize==-2) $icoSize="Max";
  56. if($ColName==16777216) $ColName="True";
  57. if($ColName==4294967296) $ColName="XP";
  58. if($ColName==-2) $ColName="Max";
  59. if($IconID==-1) die("Icon with $ColName colors and $icoSize x $icoSize size doesn't exist in this file!");
  60. ReadIcon($filename,$IconID,$Ikona);
  61. $biBitCount=$Ikona[$IconID]["Info"]["BitsPerPixel"];
  62. if($Ikona[$IconID]["Info"]["BitsPerPixel"]==0)
  63. {
  64. $Ikona[$IconID]["Info"]["BitsPerPixel"]=24;
  65. };
  66. $biBitCount=$Ikona[$IconID]["Info"]["BitsPerPixel"];
  67. if($biBitCount==0) $biBitCount=1;
  68. $Ikona[$IconID]["BitCount"]=$Ikona[$IconID]["Info"]["BitsPerPixel"];
  69. if($Ikona[$IconID]["BitCount"]>=24)
  70. {
  71. $img=imagecreatetruecolor($Ikona[$IconID]["Width"],$Ikona[$IconID]["Height"]);
  72. for($y=0;$y<$Ikona[$IconID]["Height"];$y++)
  73. for($x=0;$x<$Ikona[$IconID]["Width"];$x++)
  74. {
  75. $R=$Ikona[$IconID]["Data"][$x][$y]["r"];
  76. $G=$Ikona[$IconID]["Data"][$x][$y]["g"];
  77. $B=$Ikona[$IconID]["Data"][$x][$y]["b"];
  78. $Alpha=round($Ikona[$IconID]["Data"][$x][$y]["alpha"]/2);
  79. if($Ikona[$IconID]["BitCount"]==32)
  80. {
  81. $color=imagecolorexactalpha($img,$R,$G,$B,$Alpha);
  82. if($color==-1) $color=imagecolorallocatealpha($img,$R,$G,$B,$Alpha);
  83. }
  84. else
  85. {
  86. $color=imagecolorexact($img,$R,$G,$B);
  87. if($color==-1) $color=imagecolorallocate($img,$R,$G,$B);
  88. };
  89. imagesetpixel($img,$x,$y,$color);
  90. };
  91. }
  92. else
  93. {
  94. $img=imagecreate($Ikona[$IconID]["Width"],$Ikona[$IconID]["Height"]);
  95. for($p=0;$p<count($Ikona[$IconID]["Paleta"]);$p++)
  96. $Paleta[$p]=imagecolorallocate($img,$Ikona[$IconID]["Paleta"][$p]["r"],$Ikona[$IconID]["Paleta"][$p]["g"],$Ikona[$IconID]["Paleta"][$p]["b"]);
  97. for($y=0;$y<$Ikona[$IconID]["Height"];$y++)
  98. for($x=0;$x<$Ikona[$IconID]["Width"];$x++)
  99. {
  100. imagesetpixel($img,$x,$y,$Paleta[$Ikona[$IconID]["Data"][$x][$y]]);
  101. };
  102. };
  103. for($y=0;$y<$Ikona[$IconID]["Height"];$y++)
  104. for($x=0;$x<$Ikona[$IconID]["Width"];$x++)
  105. if($Ikona[$IconID]["Maska"][$x][$y]==1)
  106. {
  107. $IsTransparent=true;
  108. break;
  109. };
  110. if($Ikona[$IconID]["BitCount"]==32)
  111. {
  112. imagealphablending($img, FALSE);
  113. if(function_exists("imagesavealpha"))
  114. imagesavealpha($img,true);
  115. };
  116. if($IsTransparent)
  117. {
  118. if(($Ikona[$IconID]["BitCount"]>=24)or(imagecolorstotal($img)>=256))
  119. {
  120. $img2=imagecreatetruecolor(imagesx($img),imagesy($img));
  121. imagecopy($img2,$img,0,0,0,0,imagesx($img),imagesy($img));
  122. imagedestroy($img);
  123. $img=$img2;
  124. imagetruecolortopalette($img,true,255);
  125. };
  126. $Pruhledna=imagecolorallocate($img,0,0,0);
  127. for($y=0;$y<$Ikona[$IconID]["Height"];$y++)
  128. for($x=0;$x<$Ikona[$IconID]["Width"];$x++)
  129. if($Ikona[$IconID]["Maska"][$x][$y]==1)
  130. {
  131. imagesetpixel($img,$x,$y,$Pruhledna);
  132. };
  133. imagecolortransparent($img,$Pruhledna);
  134. };
  135. return $img;
  136. };
  137. function ReadIcon($filename,$id,&$Ikona)
  138. {
  139. global $CurrentBit;
  140. $f=fopen($filename,"rb");
  141. fseek($f,6+$id*16);
  142. $Width=freadbyte($f);
  143. $Height=freadbyte($f);
  144. fseek($f,6+$id*16+12);
  145. $OffSet=freaddword($f);
  146. fseek($f,$OffSet);
  147. $p=$id;
  148. $Ikona[$p]["Info"]["HeaderSize"]=freadlngint($f);
  149. $Ikona[$p]["Info"]["ImageWidth"]=freadlngint($f);
  150. $Ikona[$p]["Info"]["ImageHeight"]=freadlngint($f);
  151. $Ikona[$p]["Info"]["NumberOfImagePlanes"]=freadword($f);
  152. $Ikona[$p]["Info"]["BitsPerPixel"]=freadword($f);
  153. $Ikona[$p]["Info"]["CompressionMethod"]=freadlngint($f);
  154. $Ikona[$p]["Info"]["SizeOfBitmap"]=freadlngint($f);
  155. $Ikona[$p]["Info"]["HorzResolution"]=freadlngint($f);
  156. $Ikona[$p]["Info"]["VertResolution"]=freadlngint($f);
  157. $Ikona[$p]["Info"]["NumColorUsed"]=freadlngint($f);
  158. $Ikona[$p]["Info"]["NumSignificantColors"]=freadlngint($f);
  159. $biBitCount=$Ikona[$p]["Info"]["BitsPerPixel"];
  160. if($Ikona[$p]["Info"]["BitsPerPixel"]<=8)
  161. {
  162. $barev=pow(2,$biBitCount);
  163. for($b=0;$b<$barev;$b++)
  164. {
  165. $Ikona[$p]["Paleta"][$b]["b"]=freadbyte($f);
  166. $Ikona[$p]["Paleta"][$b]["g"]=freadbyte($f);
  167. $Ikona[$p]["Paleta"][$b]["r"]=freadbyte($f);
  168. freadbyte($f);
  169. };
  170. $Zbytek=(4-ceil(($Width/(8/$biBitCount)))%4)%4;
  171. for($y=$Height-1;$y>=0;$y--)
  172. {
  173. $CurrentBit=0;
  174. for($x=0;$x<$Width;$x++)
  175. {
  176. $C=freadbits($f,$biBitCount);
  177. $Ikona[$p]["Data"][$x][$y]=$C;
  178. };
  179. if($CurrentBit!=0) {freadbyte($f);};
  180. for($g=0;$g<$Zbytek;$g++)
  181. freadbyte($f);
  182. };
  183. }
  184. elseif($biBitCount==24)
  185. {
  186. $Zbytek=$Width%4;
  187. for($y=$Height-1;$y>=0;$y--)
  188. {
  189. for($x=0;$x<$Width;$x++)
  190. {
  191. $B=freadbyte($f);
  192. $G=freadbyte($f);
  193. $R=freadbyte($f);
  194. $Ikona[$p]["Data"][$x][$y]["r"]=$R;
  195. $Ikona[$p]["Data"][$x][$y]["g"]=$G;
  196. $Ikona[$p]["Data"][$x][$y]["b"]=$B;
  197. }
  198. for($z=0;$z<$Zbytek;$z++)
  199. freadbyte($f);
  200. };
  201. }
  202. elseif($biBitCount==32)
  203. {
  204. $Zbytek=$Width%4;
  205. for($y=$Height-1;$y>=0;$y--)
  206. {
  207. for($x=0;$x<$Width;$x++)
  208. {
  209. $B=freadbyte($f);
  210. $G=freadbyte($f);
  211. $R=freadbyte($f);
  212. $Alpha=freadbyte($f);
  213. $Ikona[$p]["Data"][$x][$y]["r"]=$R;
  214. $Ikona[$p]["Data"][$x][$y]["g"]=$G;
  215. $Ikona[$p]["Data"][$x][$y]["b"]=$B;
  216. $Ikona[$p]["Data"][$x][$y]["alpha"]=$Alpha;
  217. }
  218. for($z=0;$z<$Zbytek;$z++)
  219. freadbyte($f);
  220. };
  221. };
  222. //Maska
  223. $Zbytek=(4-ceil(($Width/(8)))%4)%4;
  224. for($y=$Height-1;$y>=0;$y--)
  225. {
  226. $CurrentBit=0;
  227. for($x=0;$x<$Width;$x++)
  228. {
  229. $C=freadbits($f,1);
  230. $Ikona[$p]["Maska"][$x][$y]=$C;
  231. };
  232. if($CurrentBit!=0) {freadbyte($f);};
  233. for($g=0;$g<$Zbytek;$g++)
  234. freadbyte($f);
  235. };
  236. //--------------
  237. fclose($f);
  238. };
  239. function GetIconsInfo($filename)
  240. {
  241. global $CurrentBit;
  242. $f=fopen($filename,"rb");
  243. $Reserved=freadword($f);
  244. $Type=freadword($f);
  245. $Count=freadword($f);
  246. for($p=0;$p<$Count;$p++)
  247. {
  248. $Ikona[$p]["Width"]=freadbyte($f);
  249. $Ikona[$p]["Height"]=freadbyte($f);
  250. $Ikona[$p]["ColorCount"]=freadword($f);
  251. if($Ikona[$p]["ColorCount"]==0) $Ikona[$p]["ColorCount"]=256;
  252. $Ikona[$p]["Planes"]=freadword($f);
  253. $Ikona[$p]["BitCount"]=freadword($f);
  254. $Ikona[$p]["BytesInRes"]=freaddword($f);
  255. $Ikona[$p]["ImageOffset"]=freaddword($f);
  256. };
  257. for($p=0;$p<$Count;$p++)
  258. {
  259. fseek($f,$Ikona[$p]["ImageOffset"]+14);
  260. $Ikona[$p]["Info"]["BitsPerPixel"]=freadword($f);
  261. };
  262. fclose($f);
  263. return $Ikona;
  264. };
  265. /*
  266. *------------------------------------------------------------
  267. * ImageIco
  268. *------------------------------------------------------------
  269. * - Returns ICO file
  270. *
  271. * Parameters: $img - Target Image (Can be array of images)
  272. * $filename - Target ico file to save
  273. *
  274. *
  275. * Note: For returning icons to Browser, you have to set header:
  276. *
  277. * header("Content-type: image/x-icon");
  278. *
  279. */
  280. function ImageIco($Images/*image or image array*/,$filename="")
  281. {
  282. if(is_array($Images))
  283. {
  284. $ImageCount=count($Images);
  285. $Image=$Images;
  286. }
  287. else
  288. {
  289. $Image[0]=$Images;
  290. $ImageCount=1;
  291. };
  292. $WriteToFile=false;
  293. if($filename!="")
  294. {
  295. $WriteToFile=true;
  296. };
  297. $ret="";
  298. $ret.=inttoword(0); //PASSWORD
  299. $ret.=inttoword(1); //SOURCE
  300. $ret.=inttoword($ImageCount); //ICONCOUNT
  301. for($q=0;$q<$ImageCount;$q++)
  302. {
  303. $img=$Image[$q];
  304. $Width=imagesx($img);
  305. $Height=imagesy($img);
  306. $ColorCount=imagecolorstotal($img);
  307. $Transparent=imagecolortransparent($img);
  308. $IsTransparent=$Transparent!=-1;
  309. if($IsTransparent) $ColorCount--;
  310. if($ColorCount==0) {$ColorCount=0; $BitCount=24;};
  311. if(($ColorCount>0)and($ColorCount<=2)) {$ColorCount=2; $BitCount=1;};
  312. if(($ColorCount>2)and($ColorCount<=16)) { $ColorCount=16; $BitCount=4;};
  313. if(($ColorCount>16)and($ColorCount<=256)) { $ColorCount=0; $BitCount=8;};
  314. //ICONINFO:
  315. $ret.=inttobyte($Width);//
  316. $ret.=inttobyte($Height);//
  317. $ret.=inttobyte($ColorCount);//
  318. $ret.=inttobyte(0);//RESERVED
  319. $Planes=0;
  320. if($BitCount>=8) $Planes=1;
  321. $ret.=inttoword($f,$Planes);//PLANES
  322. if($BitCount>=8) $WBitCount=$BitCount;
  323. if($BitCount==4) $WBitCount=0;
  324. if($BitCount==1) $WBitCount=0;
  325. $ret.=inttoword($WBitCount);//BITS
  326. $Zbytek=(4-($Width/(8/$BitCount))%4)%4;
  327. $ZbytekMask=(4-($Width/8)%4)%4;
  328. $PalSize=0;
  329. $Size=40+($Width/(8/$BitCount)+$Zbytek)*$Height+(($Width/8+$ZbytekMask) * $Height);
  330. if($BitCount<24)
  331. $Size+=pow(2,$BitCount)*4;
  332. $IconId=1;
  333. $ret.=inttodword($Size); //SIZE
  334. $OffSet=6+16*$ImageCount+$FullSize;
  335. $ret.=inttodword(6+16*$ImageCount+$FullSize);//OFFSET
  336. $FullSize+=$Size;
  337. //-------------
  338. };
  339. for($q=0;$q<$ImageCount;$q++)
  340. {
  341. $img=$Image[$q];
  342. $Width=imagesx($img);
  343. $Height=imagesy($img);
  344. $ColorCount=imagecolorstotal($img);
  345. $Transparent=imagecolortransparent($img);
  346. $IsTransparent=$Transparent!=-1;
  347. if($IsTransparent) $ColorCount--;
  348. if($ColorCount==0) {$ColorCount=0; $BitCount=24;};
  349. if(($ColorCount>0)and($ColorCount<=2)) {$ColorCount=2; $BitCount=1;};
  350. if(($ColorCount>2)and($ColorCount<=16)) { $ColorCount=16; $BitCount=4;};
  351. if(($ColorCount>16)and($ColorCount<=256)) { $ColorCount=0; $BitCount=8;};
  352. //ICONS
  353. $ret.=inttodword(40);//HEADSIZE
  354. $ret.=inttodword($Width);//
  355. $ret.=inttodword(2*$Height);//
  356. $ret.=inttoword(1); //PLANES
  357. $ret.=inttoword($BitCount); //
  358. $ret.=inttodword(0);//Compress method
  359. $ZbytekMask=($Width/8)%4;
  360. $Zbytek=($Width/(8/$BitCount))%4;
  361. $Size=($Width/(8/$BitCount)+$Zbytek)*$Height+(($Width/8+$ZbytekMask) * $Height);
  362. $ret.=inttodword($Size);//SIZE
  363. $ret.=inttodword(0);//HPIXEL_M
  364. $ret.=inttodword(0);//V_PIXEL_M
  365. $ret.=inttodword($ColorCount); //UCOLORS
  366. $ret.=inttodword(0); //DCOLORS
  367. //---------------
  368. $CC=$ColorCount;
  369. if($CC==0) $CC=256;
  370. if($BitCount<24)
  371. {
  372. $ColorTotal=imagecolorstotal($img);
  373. if($IsTransparent) $ColorTotal--;
  374. for($p=0;$p<$ColorTotal;$p++)
  375. {
  376. $color=imagecolorsforindex($img,$p);
  377. $ret.=inttobyte($color["blue"]);
  378. $ret.=inttobyte($color["green"]);
  379. $ret.=inttobyte($color["red"]);
  380. $ret.=inttobyte(0); //RESERVED
  381. };
  382. $CT=$ColorTotal;
  383. for($p=$ColorTotal;$p<$CC;$p++)
  384. {
  385. $ret.=inttobyte(0);
  386. $ret.=inttobyte(0);
  387. $ret.=inttobyte(0);
  388. $ret.=inttobyte(0); //RESERVED
  389. };
  390. };
  391. if($BitCount<=8)
  392. {
  393. for($y=$Height-1;$y>=0;$y--)
  394. {
  395. $bWrite="";
  396. for($x=0;$x<$Width;$x++)
  397. {
  398. $color=imagecolorat($img,$x,$y);
  399. if($color==$Transparent)
  400. $color=imagecolorexact($img,0,0,0);
  401. if($color==-1) $color=0;
  402. if($color>pow(2,$BitCount)-1) $color=0;
  403. $bWrite.=decbinx($color,$BitCount);
  404. if(strlen($bWrite)==8)
  405. {
  406. $ret.=inttobyte(bindec($bWrite));
  407. $bWrite="";
  408. };
  409. };
  410. if((strlen($bWrite)<8)and(strlen($bWrite)!=0))
  411. {
  412. $sl=strlen($bWrite);
  413. for($t=0;$t<8-$sl;$t++)
  414. $sl.="0";
  415. $ret.=inttobyte(bindec($bWrite));
  416. };
  417. for($z=0;$z<$Zbytek;$z++)
  418. $ret.=inttobyte(0);
  419. };
  420. };
  421. if($BitCount>=24)
  422. {
  423. for($y=$Height-1;$y>=0;$y--)
  424. {
  425. for($x=0;$x<$Width;$x++)
  426. {
  427. $color=imagecolorsforindex($img,imagecolorat($img,$x,$y));
  428. $ret.=inttobyte($color["blue"]);
  429. $ret.=inttobyte($color["green"]);
  430. $ret.=inttobyte($color["red"]);
  431. if($BitCount==32)
  432. $ret.=inttobyte(0);//Alpha for XP_COLORS
  433. };
  434. for($z=0;$z<$Zbytek;$z++)
  435. $ret.=inttobyte(0);
  436. };
  437. };
  438. //MASK
  439. for($y=$Height-1;$y>=0;$y--)
  440. {
  441. $byteCount=0;
  442. $bOut="";
  443. for($x=0;$x<$Width;$x++)
  444. {
  445. if(($Transparent!=-1)and(imagecolorat($img,$x,$y)==$Transparent))
  446. {
  447. $bOut.="1";
  448. }
  449. else
  450. {
  451. $bOut.="0";
  452. };
  453. };
  454. for($p=0;$p<strlen($bOut);$p+=8)
  455. {
  456. $byte=bindec(substr($bOut,$p,8));
  457. $byteCount++;
  458. $ret.=inttobyte($byte);
  459. // echo dechex($byte)." ";
  460. };
  461. $Zbytek=$byteCount%4;
  462. for($z=0;$z<$Zbytek;$z++)
  463. {
  464. $ret.=inttobyte(0xff);
  465. // echo "FF ";
  466. };
  467. };
  468. //------------------
  469. };//q
  470. if($WriteToFile)
  471. {
  472. $f=fopen($filename,"w");
  473. fwrite($f,$ret);
  474. fclose($f);
  475. }
  476. else
  477. {
  478. echo $ret;
  479. };
  480. };
  481. /*
  482. * Helping functions:
  483. *-------------------------
  484. *
  485. * inttobyte($n) - returns chr(n)
  486. * inttodword($n) - returns dword (n)
  487. * inttoword($n) - returns word(n)
  488. * freadbyte($file) - reads 1 byte from $file
  489. * freadword($file) - reads 2 bytes (1 word) from $file
  490. * freaddword($file) - reads 4 bytes (1 dword) from $file
  491. * freadlngint($file) - same as freaddword($file)
  492. * decbin8($d) - returns binary string of d zero filled to 8
  493. * RetBits($byte,$start,$len) - returns bits $start->$start+$len from $byte
  494. * freadbits($file,$count) - reads next $count bits from $file
  495. */
  496. function decbin8($d)
  497. {
  498. return decbinx($d,8);
  499. };
  500. function decbinx($d,$n)
  501. {
  502. $bin=decbin($d);
  503. $sbin=strlen($bin);
  504. for($j=0;$j<$n-$sbin;$j++)
  505. $bin="0$bin";
  506. return $bin;
  507. };
  508. function RetBits($byte,$start,$len)
  509. {
  510. $bin=decbin8($byte);
  511. $r=bindec(substr($bin,$start,$len));
  512. return $r;
  513. };
  514. $CurrentBit=0;
  515. function freadbits($f,$count)
  516. {
  517. global $CurrentBit,$SMode;
  518. $Byte=freadbyte($f);
  519. $LastCBit=$CurrentBit;
  520. $CurrentBit+=$count;
  521. if($CurrentBit==8)
  522. {
  523. $CurrentBit=0;
  524. }
  525. else
  526. {
  527. fseek($f,ftell($f)-1);
  528. };
  529. return RetBits($Byte,$LastCBit,$count);
  530. };
  531. function freadbyte($f)
  532. {
  533. return ord(fread($f,1));
  534. };
  535. function freadword($f)
  536. {
  537. $b1=freadbyte($f);
  538. $b2=freadbyte($f);
  539. return $b2*256+$b1;
  540. };
  541. function freadlngint($f)
  542. {
  543. return freaddword($f);
  544. };
  545. function freaddword($f)
  546. {
  547. $b1=freadword($f);
  548. $b2=freadword($f);
  549. return $b2*65536+$b1;
  550. };
  551. function inttobyte($n)
  552. {
  553. return chr($n);
  554. };
  555. function inttodword($n)
  556. {
  557. return chr($n & 255).chr(($n >> 8) & 255).chr(($n >> 16) & 255).chr(($n >> 24) & 255);
  558. };
  559. function inttoword($n)
  560. {
  561. return chr($n & 255).chr(($n >> 8) & 255);
  562. };
  563. ?>