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