PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/ext_scripts/gd/ani.php

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