[ Index ] |
PHP Cross Reference of phpwcms V1.4.3 _r380 (23.11.09) |
[Summary view] [Print] [Text view]
1 <?php 2 // ///////////////////////////////////////////////////////////////////////////////////////////////// 3 // GIF Util - (C) 2003 Yamasoft (S/C) 4 // http://www.yamasoft.com 5 // All Rights Reserved 6 // This file can be frelly copied, distributed, modified, updated by anyone under the only 7 // condition to leave the original address (Yamasoft, http://www.yamasoft.com) and this header. 8 // ///////////////////////////////////////////////////////////////////////////////////////////////// 9 // <gif> = gif_loadFile(filename, [index]) 10 // <bool> = gif_getSize(<gif> or filename, &width, &height) 11 // <bool> = gif_outputAsPng(<gif>, filename, [bgColor]) 12 // <bool> = gif_outputAsBmp(<gif>, filename, [bgcolor]) 13 // <bool> = gif_outputAsJpeg(<gif>, filename, [bgcolor]) - Requires cjpeg 14 // ///////////////////////////////////////////////////////////////////////////////////////////////// 15 // ///////////////////////////////////////////////////////////////////////////////////////////////// 16 function gif_loadFile( $lpszFileName, $iIndex = 0 ) 17 { 18 $gif = new CGIF(); 19 20 if ( !$gif->loadFile( $lpszFileName, $iIndex ) ) { 21 return false; 22 } 23 24 return $gif; 25 } 26 // ///////////////////////////////////////////////////////////////////////////////////////////////// 27 function gif_outputAsBmp( $gif, $lpszFileName, $bgColor = -1 ) 28 { 29 if ( !isSet( $gif ) || ( @get_class( $gif ) <> "cgif" ) || !$gif->loaded() || ( $lpszFileName == "" ) ) { 30 return false; 31 } 32 33 $fd = $gif->getBmp( $bgColor ); 34 if ( strlen( $fd ) <= 0 ) { 35 return false; 36 } 37 38 if ( !( $fh = @fOpen( $lpszFileName, "wb" ) ) ) { 39 return false; 40 } 41 @fWrite( $fh, $fd, strlen( $fd ) ); 42 @fFlush( $fh ); 43 @fClose( $fh ); 44 return true; 45 } 46 // ///////////////////////////////////////////////////////////////////////////////////////////////// 47 function gif_outputAsPng( $gif, $lpszFileName, $bgColor = -1 ) 48 { 49 if ( !isSet( $gif ) || ( @get_class( $gif ) <> "cgif" ) || !$gif->loaded() || ( $lpszFileName == "" ) ) { 50 return false; 51 } 52 53 $fd = $gif->getPng( $bgColor ); 54 if ( strlen( $fd ) <= 0 ) { 55 return false; 56 } 57 58 if ( !( $fh = @fopen( $lpszFileName, "wb" ) ) ) { 59 return false; 60 } 61 @fWrite( $fh, $fd, strlen( $fd ) ); 62 @fFlush( $fh ); 63 @fClose( $fh ); 64 return true; 65 } 66 // ///////////////////////////////////////////////////////////////////////////////////////////////// 67 function gif_outputAsJpeg( $gif, $lpszFileName, $bgColor = -1 ) 68 { 69 if ( gif_outputAsBmp( $gif, "$lpszFileName.bmp", $gbColor ) ) { 70 exec( "cjpeg $lpszFileName.bmp >$lpszFileName 2>/dev/null" ); 71 @unlink( "$lpszFileName.bmp" ); 72 73 if ( @file_exists( $lpszFileName ) ) { 74 if ( @filesize( $lpszFileName ) > 0 ) { 75 return true; 76 } 77 78 @unlink( $lpszFileName ); 79 } 80 } 81 82 return false; 83 } 84 // ///////////////////////////////////////////////////////////////////////////////////////////////// 85 function gif_getSize( $gif, &$width, &$height ) 86 { 87 if ( isset( $gif ) && ( @get_class( $gif ) == "cgif" ) && $gif->loaded() ) { 88 $width = $gif->width(); 89 $height = $gif->height(); 90 } else if ( @file_exists( $gif ) ) { 91 $myGIF = new CGIF(); 92 if ( !$myGIF->getSize( $gif, $width, $height ) ) { 93 return false; 94 } 95 } else { 96 return false; 97 } 98 99 return true; 100 } 101 // ///////////////////////////////////////////////////////////////////////////////////////////////// 102 class CGIFLZW { 103 var $MAX_LZW_BITS; 104 var $Fresh, $CodeSize, $SetCodeSize, $MaxCode, $MaxCodeSize, $FirstCode, $OldCode; 105 var $ClearCode, $EndCode, $Next, $Vals, $Stack, $sp, $Buf, $CurBit, $LastBit, $Done, $LastByte; 106 // ///////////////////////////////////////////////////////////////////////// 107 // CONSTRUCTOR 108 function CGIFLZW() 109 { 110 $this->MAX_LZW_BITS = 12; 111 unset( $this->Next ); 112 unset( $this->Vals ); 113 unset( $this->Stack ); 114 unset( $this->Buf ); 115 116 $this->Next = range( 0, ( 1 << $this->MAX_LZW_BITS ) - 1 ); 117 $this->Vals = range( 0, ( 1 << $this->MAX_LZW_BITS ) - 1 ); 118 $this->Stack = range( 0, ( 1 << ( $this->MAX_LZW_BITS + 1 ) ) - 1 ); 119 $this->Buf = range( 0, 279 ); 120 } 121 // ///////////////////////////////////////////////////////////////////////// 122 function deCompress( $data, &$datLen ) 123 { 124 $stLen = strlen( $data ); 125 $datLen = 0; 126 $ret = ""; 127 // INITIALIZATION 128 $this->LZWCommand( $data, true ); 129 130 while ( ( $iIndex = $this->LZWCommand( $data, false ) ) >= 0 ) { 131 $ret .= chr( $iIndex ); 132 } 133 134 $datLen = $stLen - strlen( $data ); 135 136 if ( $iIndex != -2 ) { 137 return false; 138 } 139 140 return $ret; 141 } 142 // ///////////////////////////////////////////////////////////////////////// 143 function LZWCommand( &$data, $bInit ) 144 { 145 if ( $bInit ) { 146 $this->SetCodeSize = ord( $data{0} ); 147 $data = substr( $data, 1 ); 148 149 $this->CodeSize = $this->SetCodeSize + 1; 150 $this->ClearCode = 1 << $this->SetCodeSize; 151 $this->EndCode = $this->ClearCode + 1; 152 $this->MaxCode = $this->ClearCode + 2; 153 $this->MaxCodeSize = $this->ClearCode << 1; 154 155 $this->GetCode( $data, $bInit ); 156 157 $this->Fresh = 1; 158 for( $i = 0; $i < $this->ClearCode; $i++ ) { 159 $this->Next[$i] = 0; 160 $this->Vals[$i] = $i; 161 } 162 163 for( ; $i < ( 1 << $this->MAX_LZW_BITS ); $i++ ) { 164 $this->Next[$i] = 0; 165 $this->Vals[$i] = 0; 166 } 167 168 $this->sp = 0; 169 return 1; 170 } 171 172 if ( $this->Fresh ) { 173 $this->Fresh = 0; 174 do { 175 $this->FirstCode = $this->GetCode( $data, $bInit ); 176 $this->OldCode = $this->FirstCode; 177 } while ( $this->FirstCode == $this->ClearCode ); 178 179 return $this->FirstCode; 180 } 181 182 if ( $this->sp > 0 ) { 183 $this->sp--; 184 return $this->Stack[$this->sp]; 185 } while ( ( $Code = $this->GetCode( $data, $bInit ) ) >= 0 ) { 186 if ( $Code == $this->ClearCode ) { 187 for( $i = 0; $i < $this->ClearCode; $i++ ) { 188 $this->Next[$i] = 0; 189 $this->Vals[$i] = $i; 190 } 191 192 for( ; $i < ( 1 << $this->MAX_LZW_BITS ); $i++ ) { 193 $this->Next[$i] = 0; 194 $this->Vals[$i] = 0; 195 } 196 197 $this->CodeSize = $this->SetCodeSize + 1; 198 $this->MaxCodeSize = $this->ClearCode << 1; 199 $this->MaxCode = $this->ClearCode + 2; 200 $this->sp = 0; 201 $this->FirstCode = $this->GetCode( $data, $bInit ); 202 $this->OldCode = $this->FirstCode; 203 204 return $this->FirstCode; 205 } 206 207 if ( $Code == $this->EndCode ) { 208 return -2; 209 } 210 211 $InCode = $Code; 212 if ( $Code >= $this->MaxCode ) { 213 $this->Stack[$this->sp] = $this->FirstCode; 214 $this->sp++; 215 $Code = $this->OldCode; 216 } while ( $Code >= $this->ClearCode ) { 217 $this->Stack[$this->sp] = $this->Vals[$Code]; 218 $this->sp++; 219 220 if ( $Code == $this->Next[$Code] ) // Circular table entry, big GIF Error! 221 return -1; 222 223 $Code = $this->Next[$Code]; 224 } 225 226 $this->FirstCode = $this->Vals[$Code]; 227 $this->Stack[$this->sp] = $this->FirstCode; 228 $this->sp++; 229 230 if ( ( $Code = $this->MaxCode ) < ( 1 << $this->MAX_LZW_BITS ) ) { 231 $this->Next[$Code] = $this->OldCode; 232 $this->Vals[$Code] = $this->FirstCode; 233 $this->MaxCode++; 234 235 if ( ( $this->MaxCode >= $this->MaxCodeSize ) && ( $this->MaxCodeSize < ( 1 << $this->MAX_LZW_BITS ) ) ) { 236 $this->MaxCodeSize *= 2; 237 $this->CodeSize++; 238 } 239 } 240 241 $this->OldCode = $InCode; 242 if ( $this->sp > 0 ) { 243 $this->sp--; 244 return $this->Stack[$this->sp]; 245 } 246 } 247 248 return $Code; 249 } 250 // ///////////////////////////////////////////////////////////////////////// 251 function GetCode( &$data, $bInit ) 252 { 253 if ( $bInit ) { 254 $this->CurBit = 0; 255 $this->LastBit = 0; 256 $this->Done = 0; 257 $this->LastByte = 2; 258 return 1; 259 } 260 261 if ( ( $this->CurBit + $this->CodeSize ) >= $this->LastBit ) { 262 if ( $this->Done ) { 263 if ( $this->CurBit >= $this->LastBit ) { 264 // Ran off the end of my bits 265 return 0; 266 } 267 return -1; 268 } 269 270 $this->Buf[0] = $this->Buf[$this->LastByte - 2]; 271 $this->Buf[1] = $this->Buf[$this->LastByte - 1]; 272 273 $Count = ord( $data{0} ); 274 $data = substr( $data, 1 ); 275 276 if ( $Count ) { 277 for( $i = 0; $i < $Count; $i++ ) { 278 $this->Buf[2 + $i] = ord( $data{$i} ); 279 } 280 $data = substr( $data, $Count ); 281 } else { 282 $this->Done = 1; 283 } 284 285 $this->LastByte = 2 + $Count; 286 $this->CurBit = ( $this->CurBit - $this->LastBit ) + 16; 287 $this->LastBit = ( 2 + $Count ) << 3; 288 } 289 290 $iRet = 0; 291 for( $i = $this->CurBit, $j = 0; $j < $this->CodeSize; $i++, $j++ ) { 292 $iRet |= ( ( $this->Buf[intval( $i / 8 )] &( 1 << ( $i % 8 ) ) ) != 0 ) << $j; 293 } 294 295 $this->CurBit += $this->CodeSize; 296 return $iRet; 297 } 298 } 299 // ///////////////////////////////////////////////////////////////////////////////////////////////// 300 class CGIFCOLORTABLE { 301 var $m_nColors; 302 var $m_arColors; 303 // ///////////////////////////////////////////////////////////////////////// 304 // CONSTRUCTOR 305 function CGIFCOLORTABLE() 306 { 307 unset( $this->m_nColors ); 308 unset( $this->m_arColors ); 309 } 310 // ///////////////////////////////////////////////////////////////////////// 311 function load( $lpData, $num ) 312 { 313 $this->m_nColors = 0; 314 $this->m_arColors = array(); 315 316 for( $i = 0; $i < $num; $i++ ) { 317 $rgb = substr( $lpData, $i * 3, 3 ); 318 if ( strlen( $rgb ) < 3 ) { 319 return false; 320 } 321 322 $this->m_arColors[] = ( ord( $rgb{2} ) << 16 ) + ( ord( $rgb{1} ) << 8 ) + ord( $rgb{0} ); 323 $this->m_nColors++; 324 } 325 326 return true; 327 } 328 // ///////////////////////////////////////////////////////////////////////// 329 function toString() 330 { 331 $ret = ""; 332 333 for( $i = 0; $i < $this->m_nColors; $i++ ) { 334 $ret .= 335 chr( ( $this->m_arColors[$i] &0x000000FF ) ) . // R 336 chr( ( $this->m_arColors[$i] &0x0000FF00 ) >> 8 ) . // G 337 chr( ( $this->m_arColors[$i] &0x00FF0000 ) >> 16 ); // B 338 } 339 340 return $ret; 341 } 342 // ///////////////////////////////////////////////////////////////////////// 343 function toRGBQuad() 344 { 345 $ret = ""; 346 347 for( $i = 0; $i < $this->m_nColors; $i++ ) { 348 $ret .= 349 chr( ( $this->m_arColors[$i] &0x00FF0000 ) >> 16 ) . // B 350 chr( ( $this->m_arColors[$i] &0x0000FF00 ) >> 8 ) . // G 351 chr( ( $this->m_arColors[$i] &0x000000FF ) ) . // R 352 "\x00"; 353 } 354 355 return $ret; 356 } 357 // ///////////////////////////////////////////////////////////////////////// 358 function colorIndex( $rgb ) 359 { 360 $rgb = intval( $rgb ) &0xFFFFFF; 361 $r1 = ( $rgb &0x0000FF ); 362 $g1 = ( $rgb &0x00FF00 ) >> 8; 363 $b1 = ( $rgb &0xFF0000 ) >> 16; 364 $idx = -1; 365 366 for( $i = 0; $i < $this->m_nColors; $i++ ) { 367 $r2 = ( $this->m_arColors[$i] &0x000000FF ); 368 $g2 = ( $this->m_arColors[$i] &0x0000FF00 ) >> 8; 369 $b2 = ( $this->m_arColors[$i] &0x00FF0000 ) >> 16; 370 $d = abs( $r2 - $r1 ) + abs( $g2 - $g1 ) + abs( $b2 - $b1 ); 371 372 if ( ( $idx == -1 ) || ( $d < $dif ) ) { 373 $idx = $i; 374 $dif = $d; 375 } 376 } 377 378 return $idx; 379 } 380 } 381 // ///////////////////////////////////////////////////////////////////////////////////////////////// 382 class CGIFFILEHEADER { 383 var $m_lpVer; 384 var $m_nWidth; 385 var $m_nHeight; 386 var $m_bGlobalClr; 387 var $m_nColorRes; 388 var $m_bSorted; 389 var $m_nTableSize; 390 var $m_nBgColor; 391 var $m_nPixelRatio; 392 var $m_colorTable; 393 // ///////////////////////////////////////////////////////////////////////// 394 // CONSTRUCTOR 395 function CGIFFILEHEADER() 396 { 397 unset( $this->m_lpVer ); 398 unset( $this->m_nWidth ); 399 unset( $this->m_nHeight ); 400 unset( $this->m_bGlobalClr ); 401 unset( $this->m_nColorRes ); 402 unset( $this->m_bSorted ); 403 unset( $this->m_nTableSize ); 404 unset( $this->m_nBgColor ); 405 unset( $this->m_nPixelRatio ); 406 unset( $this->m_colorTable ); 407 } 408 // ///////////////////////////////////////////////////////////////////////// 409 function load( $lpData, &$hdrLen ) 410 { 411 $hdrLen = 0; 412 413 $this->m_lpVer = substr( $lpData, 0, 6 ); 414 if ( ( $this->m_lpVer <> "GIF87a" ) && ( $this->m_lpVer <> "GIF89a" ) ) { 415 return false; 416 } 417 418 $this->m_nWidth = $this->w2i( substr( $lpData, 6, 2 ) ); 419 $this->m_nHeight = $this->w2i( substr( $lpData, 8, 2 ) ); 420 if ( !$this->m_nWidth || !$this->m_nHeight ) { 421 return false; 422 } 423 424 $b = ord( substr( $lpData, 10, 1 ) ); 425 $this->m_bGlobalClr = ( $b &0x80 ) ? true : false; 426 $this->m_nColorRes = ( $b &0x70 ) >> 4; 427 $this->m_bSorted = ( $b &0x08 ) ? true : false; 428 $this->m_nTableSize = 2 << ( $b &0x07 ); 429 $this->m_nBgColor = ord( substr( $lpData, 11, 1 ) ); 430 $this->m_nPixelRatio = ord( substr( $lpData, 12, 1 ) ); 431 $hdrLen = 13; 432 433 if ( $this->m_bGlobalClr ) { 434 $this->m_colorTable = new CGIFCOLORTABLE(); 435 if ( !$this->m_colorTable->load( substr( $lpData, $hdrLen ), $this->m_nTableSize ) ) { 436 return false; 437 } 438 $hdrLen += 3 * $this->m_nTableSize; 439 } 440 441 return true; 442 } 443 // ///////////////////////////////////////////////////////////////////////// 444 function w2i( $str ) 445 { 446 return ord( substr( $str, 0, 1 ) ) + ( ord( substr( $str, 1, 1 ) ) << 8 ); 447 } 448 } 449 // ///////////////////////////////////////////////////////////////////////////////////////////////// 450 class CGIFIMAGEHEADER { 451 var $m_nLeft; 452 var $m_nTop; 453 var $m_nWidth; 454 var $m_nHeight; 455 var $m_bLocalClr; 456 var $m_bInterlace; 457 var $m_bSorted; 458 var $m_nTableSize; 459 var $m_colorTable; 460 // ///////////////////////////////////////////////////////////////////////// 461 // CONSTRUCTOR 462 function CGIFIMAGEHEADER() 463 { 464 unset( $this->m_nLeft ); 465 unset( $this->m_nTop ); 466 unset( $this->m_nWidth ); 467 unset( $this->m_nHeight ); 468 unset( $this->m_bLocalClr ); 469 unset( $this->m_bInterlace ); 470 unset( $this->m_bSorted ); 471 unset( $this->m_nTableSize ); 472 unset( $this->m_colorTable ); 473 } 474 // ///////////////////////////////////////////////////////////////////////// 475 function load( $lpData, &$hdrLen ) 476 { 477 $hdrLen = 0; 478 479 $this->m_nLeft = $this->w2i( substr( $lpData, 0, 2 ) ); 480 $this->m_nTop = $this->w2i( substr( $lpData, 2, 2 ) ); 481 $this->m_nWidth = $this->w2i( substr( $lpData, 4, 2 ) ); 482 $this->m_nHeight = $this->w2i( substr( $lpData, 6, 2 ) ); 483 484 if ( !$this->m_nWidth || !$this->m_nHeight ) { 485 return false; 486 } 487 488 $b = ord( $lpData{8} ); 489 $this->m_bLocalClr = ( $b &0x80 ) ? true : false; 490 $this->m_bInterlace = ( $b &0x40 ) ? true : false; 491 $this->m_bSorted = ( $b &0x20 ) ? true : false; 492 $this->m_nTableSize = 2 << ( $b &0x07 ); 493 $hdrLen = 9; 494 495 if ( $this->m_bLocalClr ) { 496 $this->m_colorTable = new CGIFCOLORTABLE(); 497 if ( !$this->m_colorTable->load( substr( $lpData, $hdrLen ), $this->m_nTableSize ) ) { 498 return false; 499 } 500 $hdrLen += 3 * $this->m_nTableSize; 501 } 502 503 return true; 504 } 505 // ///////////////////////////////////////////////////////////////////////// 506 function w2i( $str ) 507 { 508 return ord( substr( $str, 0, 1 ) ) + ( ord( substr( $str, 1, 1 ) ) << 8 ); 509 } 510 } 511 // ///////////////////////////////////////////////////////////////////////////////////////////////// 512 class CGIFIMAGE { 513 var $m_disp; 514 var $m_bUser; 515 var $m_bTrans; 516 var $m_nDelay; 517 var $m_nTrans; 518 var $m_lpComm; 519 var $m_gih; 520 var $m_data; 521 var $m_lzw; 522 // ///////////////////////////////////////////////////////////////////////// 523 function CGIFIMAGE() 524 { 525 unSet( $this->m_disp ); 526 unSet( $this->m_bUser ); 527 unSet( $this->m_bTrans ); 528 unSet( $this->m_nDelay ); 529 unSet( $this->m_nTrans ); 530 unSet( $this->m_lpComm ); 531 unSet( $this->m_data ); 532 $this->m_gih = new CGIFIMAGEHEADER(); 533 $this->m_lzw = new CGIFLZW(); 534 } 535 // ///////////////////////////////////////////////////////////////////////// 536 function load( $data, &$datLen ) 537 { 538 $datLen = 0; 539 540 while ( true ) { 541 $b = ord( $data{0} ); 542 $data = substr( $data, 1 ); 543 $datLen++; 544 545 switch ( $b ) { 546 case 0x21: // Extension 547 if ( !$this->skipExt( $data, $len = 0 ) ) { 548 return false; 549 } 550 $datLen += $len; 551 break; 552 553 case 0x2C: // Image 554 // LOAD HEADER & COLOR TABLE 555 if ( !$this->m_gih->load( $data, $len = 0 ) ) { 556 return false; 557 } 558 $data = substr( $data, $len ); 559 $datLen += $len; 560 // ALLOC BUFFER 561 if ( !( $this->m_data = $this->m_lzw->deCompress( $data, $len = 0 ) ) ) { 562 return false; 563 } 564 $data = substr( $data, $len ); 565 $datLen += $len; 566 567 if ( $this->m_gih->m_bInterlace ) { 568 $this->deInterlace(); 569 } 570 return true; 571 572 case 0x3B: // EOF 573 default: 574 return false; 575 } 576 } 577 return false; 578 } 579 // ///////////////////////////////////////////////////////////////////////// 580 function skipExt( &$data, &$extLen ) 581 { 582 $extLen = 0; 583 584 $b = ord( $data{0} ); 585 $data = substr( $data, 1 ); 586 $extLen++; 587 588 switch ( $b ) { 589 case 0xF9: // Graphic Control 590 $b = ord( $data{1} ); 591 $this->m_disp = ( $b &0x1C ) >> 2; 592 $this->m_bUser = ( $b &0x02 ) ? true : false; 593 $this->m_bTrans = ( $b &0x01 ) ? true : false; 594 $this->m_nDelay = $this->w2i( substr( $data, 2, 2 ) ); 595 $this->m_nTrans = ord( $data{4} ); 596 break; 597 598 case 0xFE: // Comment 599 $this->m_lpComm = substr( $data, 1, ord( $data{0} ) ); 600 break; 601 602 case 0x01: // Plain text 603 break; 604 605 case 0xFF: // Application 606 break; 607 } 608 // SKIP DEFAULT AS DEFS MAY CHANGE 609 $b = ord( $data{0} ); 610 $data = substr( $data, 1 ); 611 $extLen++; 612 while ( $b > 0 ) { 613 $data = substr( $data, $b ); 614 $extLen += $b; 615 $b = ord( $data{0} ); 616 $data = substr( $data, 1 ); 617 $extLen++; 618 } 619 return true; 620 } 621 // ///////////////////////////////////////////////////////////////////////// 622 function w2i( $str ) 623 { 624 return ord( substr( $str, 0, 1 ) ) + ( ord( substr( $str, 1, 1 ) ) << 8 ); 625 } 626 // ///////////////////////////////////////////////////////////////////////// 627 function deInterlace() 628 { 629 $data = $this->m_data; 630 631 for( $i = 0; $i < 4; $i++ ) { 632 switch ( $i ) { 633 case 0: 634 $s = 8; 635 $y = 0; 636 break; 637 638 case 1: 639 $s = 8; 640 $y = 4; 641 break; 642 643 case 2: 644 $s = 4; 645 $y = 2; 646 break; 647 648 case 3: 649 $s = 2; 650 $y = 1; 651 break; 652 } 653 654 for( ; $y < $this->m_gih->m_nHeight; $y += $s ) { 655 $lne = substr( $this->m_data, 0, $this->m_gih->m_nWidth ); 656 $this->m_data = substr( $this->m_data, $this->m_gih->m_nWidth ); 657 658 $data = 659 substr( $data, 0, $y * $this->m_gih->m_nWidth ) . $lne . 660 substr( $data, ( $y + 1 ) * $this->m_gih->m_nWidth ); 661 } 662 } 663 664 $this->m_data = $data; 665 } 666 } 667 // ///////////////////////////////////////////////////////////////////////////////////////////////// 668 class CGIF { 669 var $m_gfh; 670 var $m_lpData; 671 var $m_img; 672 var $m_bLoaded; 673 // ///////////////////////////////////////////////////////////////////////// 674 // CONSTRUCTOR 675 function CGIF() 676 { 677 $this->m_gfh = new CGIFFILEHEADER(); 678 $this->m_img = new CGIFIMAGE(); 679 $this->m_lpData = ""; 680 $this->m_bLoaded = false; 681 } 682 // ///////////////////////////////////////////////////////////////////////// 683 function loadFile( $lpszFileName, $iIndex ) 684 { 685 if ( $iIndex < 0 ) { 686 return false; 687 } 688 // READ FILE 689 if ( !( $fh = @fOpen( $lpszFileName, "rb" ) ) ) { 690 return false; 691 } 692 // EDITEI - in order to read remote files (HTTP(s) and FTP protocols) 693 if ( strpos( $lpszFileName, "http" ) !== false or strpos( $lpszFileName, "ftp" ) !== false ) { 694 $contents = ''; 695 while ( !feof( $fh ) ) $contents .= @fread( $fh, 8192 ); 696 } else { 697 $contents = @fread( $fh, @filesize( $lpszFileName ) ); 698 } 699 $this->m_lpData = $contents; 700 // $this->m_lpData = @fRead ($fh, @fileSize ($lpszFileName)); 701 fClose( $fh ); 702 // GET FILE HEADER 703 if ( !$this->m_gfh->load( $this->m_lpData, $len = 0 ) ) { 704 return false; 705 } 706 $this->m_lpData = substr( $this->m_lpData, $len ); 707 708 do { 709 if ( !$this->m_img->load( $this->m_lpData, $imgLen = 0 ) ) { 710 return false; 711 } 712 $this->m_lpData = substr( $this->m_lpData, $imgLen ); 713 } while ( $iIndex-- > 0 ); 714 715 $this->m_bLoaded = true; 716 return true; 717 } 718 // ///////////////////////////////////////////////////////////////////////// 719 function getSize( $lpszFileName, &$width, &$height ) 720 { 721 if ( !( $fh = @fOpen( $lpszFileName, "rb" ) ) ) { 722 return false; 723 } 724 $data = @fRead( $fh, @fileSize( $lpszFileName ) ); 725 @fClose( $fh ); 726 727 $gfh = new CGIFFILEHEADER(); 728 if ( !$gfh->load( $data, $len = 0 ) ) { 729 return false; 730 } 731 732 $width = $gfh->m_nWidth; 733 $height = $gfh->m_nHeight; 734 return true; 735 } 736 // ///////////////////////////////////////////////////////////////////////// 737 function getBmp( $bgColor ) 738 { 739 $out = ""; 740 741 if ( !$this->m_bLoaded ) { 742 return false; 743 } 744 // PREPARE COLOR TABLE (RGBQUADs) 745 if ( $this->m_img->m_gih->m_bLocalClr ) { 746 $nColors = $this->m_img->m_gih->m_nTableSize; 747 $rgbq = $this->m_img->m_gih->m_colorTable->toRGBQuad(); 748 if ( $bgColor != -1 ) { 749 $bgColor = $this->m_img->m_gih->m_colorTable->colorIndex( $bgColor ); 750 } 751 } else if ( $this->m_gfh->m_bGlobalClr ) { 752 $nColors = $this->m_gfh->m_nTableSize; 753 $rgbq = $this->m_gfh->m_colorTable->toRGBQuad(); 754 if ( $bgColor != -1 ) { 755 $bgColor = $this->m_gfh->m_colorTable->colorIndex( $bgColor ); 756 } 757 } else { 758 $nColors = 0; 759 $bgColor = -1; 760 } 761 // PREPARE BITMAP BITS 762 $data = $this->m_img->m_data; 763 $nPxl = ( $this->m_gfh->m_nHeight - 1 ) * $this->m_gfh->m_nWidth; 764 $bmp = ""; 765 $nPad = ( $this->m_gfh->m_nWidth % 4 ) ? 4 - ( $this->m_gfh->m_nWidth % 4 ) : 0; 766 for( $y = 0; $y < $this->m_gfh->m_nHeight; $y++ ) { 767 for( $x = 0; $x < $this->m_gfh->m_nWidth; $x++, $nPxl++ ) { 768 if ( 769 ( $x >= $this->m_img->m_gih->m_nLeft ) && 770 ( $y >= $this->m_img->m_gih->m_nTop ) && 771 ( $x < ( $this->m_img->m_gih->m_nLeft + $this->m_img->m_gih->m_nWidth ) ) && 772 ( $y < ( $this->m_img->m_gih->m_nTop + $this->m_img->m_gih->m_nHeight ) ) ) { 773 // PART OF IMAGE 774 if ( $this->m_img->m_bTrans && ( ord( $data{$nPxl} ) == $this->m_img->m_nTrans ) ) { 775 // TRANSPARENT -> BACKGROUND 776 if ( $bgColor == -1 ) { 777 $bmp .= chr( $this->m_gfh->m_nBgColor ); 778 } else { 779 $bmp .= chr( $bgColor ); 780 } 781 } else { 782 $bmp .= $data{$nPxl}; 783 } 784 } else { 785 // BACKGROUND 786 if ( $bgColor == -1 ) { 787 $bmp .= chr( $this->m_gfh->m_nBgColor ); 788 } else { 789 $bmp .= chr( $bgColor ); 790 } 791 } 792 } 793 $nPxl -= $this->m_gfh->m_nWidth << 1; 794 // ADD PADDING 795 for( $x = 0; $x < $nPad; $x++ ) { 796 $bmp .= "\x00"; 797 } 798 } 799 // BITMAPFILEHEADER 800 $out .= "BM"; 801 $out .= $this->dword( 14 + 40 + ( $nColors << 2 ) + strlen( $bmp ) ); 802 $out .= "\x00\x00"; 803 $out .= "\x00\x00"; 804 $out .= $this->dword( 14 + 40 + ( $nColors << 2 ) ); 805 // BITMAPINFOHEADER 806 $out .= $this->dword( 40 ); 807 $out .= $this->dword( $this->m_gfh->m_nWidth ); 808 $out .= $this->dword( $this->m_gfh->m_nHeight ); 809 $out .= "\x01\x00"; 810 $out .= "\x08\x00"; 811 $out .= "\x00\x00\x00\x00"; 812 $out .= "\x00\x00\x00\x00"; 813 $out .= "\x12\x0B\x00\x00"; 814 $out .= "\x12\x0B\x00\x00"; 815 $out .= $this->dword( $nColors % 256 ); 816 $out .= "\x00\x00\x00\x00"; 817 // COLOR TABLE 818 if ( $nColors > 0 ) { 819 $out .= $rgbq; 820 } 821 // DATA 822 $out .= $bmp; 823 824 return $out; 825 } 826 // ///////////////////////////////////////////////////////////////////////// 827 function getPng( $bgColor ) 828 { 829 $out = ""; 830 831 if ( !$this->m_bLoaded ) { 832 return false; 833 } 834 // PREPARE COLOR TABLE (RGBQUADs) 835 if ( $this->m_img->m_gih->m_bLocalClr ) { 836 $nColors = $this->m_img->m_gih->m_nTableSize; 837 $pal = $this->m_img->m_gih->m_colorTable->toString(); 838 if ( $bgColor != -1 ) { 839 $bgColor = $this->m_img->m_gih->m_colorTable->colorIndex( $bgColor ); 840 } 841 } else if ( $this->m_gfh->m_bGlobalClr ) { 842 $nColors = $this->m_gfh->m_nTableSize; 843 $pal = $this->m_gfh->m_colorTable->toString(); 844 if ( $bgColor != -1 ) { 845 $bgColor = $this->m_gfh->m_colorTable->colorIndex( $bgColor ); 846 } 847 } else { 848 $nColors = 0; 849 $bgColor = -1; 850 } 851 // PREPARE BITMAP BITS 852 $data = $this->m_img->m_data; 853 $nPxl = 0; 854 $bmp = ""; 855 for( $y = 0; $y < $this->m_gfh->m_nHeight; $y++ ) { 856 $bmp .= "\x00"; 857 for( $x = 0; $x < $this->m_gfh->m_nWidth; $x++, $nPxl++ ) { 858 if ( 859 ( $x >= $this->m_img->m_gih->m_nLeft ) && 860 ( $y >= $this->m_img->m_gih->m_nTop ) && 861 ( $x < ( $this->m_img->m_gih->m_nLeft + $this->m_img->m_gih->m_nWidth ) ) && 862 ( $y < ( $this->m_img->m_gih->m_nTop + $this->m_img->m_gih->m_nHeight ) ) ) { 863 // PART OF IMAGE 864 $bmp .= $data{$nPxl}; 865 } else { 866 // BACKGROUND 867 if ( $bgColor == -1 ) { 868 $bmp .= chr( $this->m_gfh->m_nBgColor ); 869 } else { 870 $bmp .= chr( $bgColor ); 871 } 872 } 873 } 874 } 875 $bmp = gzcompress( $bmp, 9 ); 876 // ///////////////////////////////////////////////////////////////////// 877 // SIGNATURE 878 $out .= "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"; 879 // ///////////////////////////////////////////////////////////////////// 880 // HEADER 881 $out .= "\x00\x00\x00\x0D"; 882 $tmp = "IHDR"; 883 $tmp .= $this->ndword( $this->m_gfh->m_nWidth ); 884 $tmp .= $this->ndword( $this->m_gfh->m_nHeight ); 885 $tmp .= "\x08\x03\x00\x00\x00"; 886 $out .= $tmp; 887 $out .= $this->ndword( crc32( $tmp ) ); 888 // ///////////////////////////////////////////////////////////////////// 889 // PALETTE 890 if ( $nColors > 0 ) { 891 $out .= $this->ndword( $nColors * 3 ); 892 $tmp = "PLTE"; 893 $tmp .= $pal; 894 $out .= $tmp; 895 $out .= $this->ndword( crc32( $tmp ) ); 896 } 897 // ///////////////////////////////////////////////////////////////////// 898 // TRANSPARENCY 899 if ( $this->m_img->m_bTrans && ( $nColors > 0 ) ) { 900 $out .= $this->ndword( $nColors ); 901 $tmp = "tRNS"; 902 for( $i = 0; $i < $nColors; $i++ ) { 903 $tmp .= ( $i == $this->m_img->m_nTrans ) ? "\x00" : "\xFF"; 904 } 905 $out .= $tmp; 906 $out .= $this->ndword( crc32( $tmp ) ); 907 } 908 // ///////////////////////////////////////////////////////////////////// 909 // DATA BITS 910 $out .= $this->ndword( strlen( $bmp ) ); 911 $tmp = "IDAT"; 912 $tmp .= $bmp; 913 $out .= $tmp; 914 $out .= $this->ndword( crc32( $tmp ) ); 915 // ///////////////////////////////////////////////////////////////////// 916 // END OF FILE 917 $out .= "\x00\x00\x00\x00IEND\xAE\x42\x60\x82"; 918 919 return $out; 920 } 921 // ///////////////////////////////////////////////////////////////////////// 922 function dword( $val ) 923 { 924 $val = intval( $val ); 925 return chr( $val &0xFF ) . chr( ( $val &0xFF00 ) >> 8 ) . chr( ( $val &0xFF0000 ) >> 16 ) . chr( ( $val &0xFF000000 ) >> 24 ); 926 } 927 // ///////////////////////////////////////////////////////////////////////// 928 function ndword( $val ) 929 { 930 $val = intval( $val ); 931 return chr( ( $val &0xFF000000 ) >> 24 ) . chr( ( $val &0xFF0000 ) >> 16 ) . chr( ( $val &0xFF00 ) >> 8 ) . chr( $val &0xFF ); 932 } 933 // ///////////////////////////////////////////////////////////////////////// 934 function width() 935 { 936 return $this->m_gfh->m_nWidth; 937 } 938 // ///////////////////////////////////////////////////////////////////////// 939 function height() 940 { 941 return $this->m_gfh->m_nHeight; 942 } 943 // ///////////////////////////////////////////////////////////////////////// 944 function comment() 945 { 946 return $this->m_img->m_lpComm; 947 } 948 // ///////////////////////////////////////////////////////////////////////// 949 function loaded() 950 { 951 return $this->m_bLoaded; 952 } 953 } 954 // ///////////////////////////////////////////////////////////////////////////////////////////////// 955 956 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Dec 30 05:55:15 2009 | Cross-referenced by PHPXref 0.7 |