Advertisement
Guest User

GifEncoder

a guest
Apr 10th, 2010
4,150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.80 KB | None | 0 0
  1. <?php
  2. /*
  3. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  4. ::
  5. ::    GIFEncoder Version 2.0 by László Zsidi, http://gifs.hu
  6. ::
  7. ::    This class is a rewritten 'GifMerge.class.php' version.
  8. ::
  9. ::  Modification:
  10. ::   - Simplified and easy code,
  11. ::   - Ultra fast encoding,
  12. ::   - Built-in errors,
  13. ::   - Stable working
  14. ::
  15. ::
  16. ::    Updated at 2007. 02. 13. '00.05.AM'
  17. ::
  18. ::
  19. ::
  20. ::  Try on-line GIFBuilder Form demo based on GIFEncoder.
  21. ::
  22. ::  http://gifs.hu/phpclasses/demos/GifBuilder/
  23. ::
  24. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  25. */
  26.  
  27. Class GIFEncoder {
  28.     var $GIF = "GIF89a";        /* GIF header 6 bytes    */
  29.     var $VER = "GIFEncoder V2.05";    /* Encoder version        */
  30.  
  31.     var $BUF = Array ( );
  32.     var $LOP =  0;
  33.     var $DIS =  2;
  34.     var $COL = -1;
  35.     var $IMG = -1;
  36.  
  37.     var $ERR = Array (
  38.         ERR00=>"Does not supported function for only one image!",
  39.         ERR01=>"Source is not a GIF image!",
  40.         ERR02=>"Unintelligible flag ",
  41.         ERR03=>"Does not make animation from animated GIF source",
  42.     );
  43.  
  44.     /*
  45.     :::::::::::::::::::::::::::::::::::::::::::::::::::
  46.     ::
  47.     ::    GIFEncoder...
  48.     ::
  49.     */
  50.     function GIFEncoder    (
  51.                             $GIF_src, $GIF_dly, $GIF_lop, $GIF_dis,
  52.                             $GIF_red, $GIF_grn, $GIF_blu, $GIF_mod
  53.                         ) {
  54.         if ( ! is_array ( $GIF_src ) && ! is_array ( $GIF_tim ) ) {
  55.             printf    ( "%s: %s", $this->VER, $this->ERR [ 'ERR00' ] );
  56.             exit    ( 0 );
  57.         }
  58.         $this->LOP = ( $GIF_lop > -1 ) ? $GIF_lop : 0;
  59.         $this->DIS = ( $GIF_dis > -1 ) ? ( ( $GIF_dis < 3 ) ? $GIF_dis : 3 ) : 2;
  60.         $this->COL = ( $GIF_red > -1 && $GIF_grn > -1 && $GIF_blu > -1 ) ?
  61.                         ( $GIF_red | ( $GIF_grn << 8 ) | ( $GIF_blu << 16 ) ) : -1;
  62.  
  63.         for ( $i = 0; $i < count ( $GIF_src ); $i++ ) {
  64.             if ( strToLower ( $GIF_mod ) == "url" ) {
  65.                 $this->BUF [ ] = fread ( fopen ( $GIF_src [ $i ], "rb" ), filesize ( $GIF_src [ $i ] ) );
  66.             }
  67.             else if ( strToLower ( $GIF_mod ) == "bin" ) {
  68.                 $this->BUF [ ] = $GIF_src [ $i ];
  69.             }
  70.             else {
  71.                 printf    ( "%s: %s ( %s )!", $this->VER, $this->ERR [ 'ERR02' ], $GIF_mod );
  72.                 exit    ( 0 );
  73.             }
  74.             if ( substr ( $this->BUF [ $i ], 0, 6 ) != "GIF87a" && substr ( $this->BUF [ $i ], 0, 6 ) != "GIF89a" ) {
  75.                 printf    ( "%s: %d %s", $this->VER, $i, $this->ERR [ 'ERR01' ] );
  76.                 exit    ( 0 );
  77.             }
  78.             for ( $j = ( 13 + 3 * ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) ) ), $k = TRUE; $k; $j++ ) {
  79.                 switch ( $this->BUF [ $i ] { $j } ) {
  80.                     case "!":
  81.                         if ( ( substr ( $this->BUF [ $i ], ( $j + 3 ), 8 ) ) == "NETSCAPE" ) {
  82.                             printf    ( "%s: %s ( %s source )!", $this->VER, $this->ERR [ 'ERR03' ], ( $i + 1 ) );
  83.                             exit    ( 0 );
  84.                         }
  85.                         break;
  86.                     case ";":
  87.                         $k = FALSE;
  88.                         break;
  89.                 }
  90.             }
  91.         }
  92.         GIFEncoder::GIFAddHeader ( );
  93.         for ( $i = 0; $i < count ( $this->BUF ); $i++ ) {
  94.             GIFEncoder::GIFAddFrames ( $i, $GIF_dly [ $i ] );
  95.         }
  96.         GIFEncoder::GIFAddFooter ( );
  97.     }
  98.     /*
  99.     :::::::::::::::::::::::::::::::::::::::::::::::::::
  100.     ::
  101.     ::    GIFAddHeader...
  102.     ::
  103.     */
  104.     function GIFAddHeader ( ) {
  105.         $cmap = 0;
  106.  
  107.         if ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x80 ) {
  108.             $cmap = 3 * ( 2 << ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x07 ) );
  109.  
  110.             $this->GIF .= substr ( $this->BUF [ 0 ], 6, 7        );
  111.             $this->GIF .= substr ( $this->BUF [ 0 ], 13, $cmap    );
  112.             $this->GIF .= "!\377\13NETSCAPE2.0\3\1" . GIFEncoder::GIFWord ( $this->LOP ) . "\0";
  113.         }
  114.     }
  115.     /*
  116.     :::::::::::::::::::::::::::::::::::::::::::::::::::
  117.     ::
  118.     ::    GIFAddFrames...
  119.     ::
  120.     */
  121.     function GIFAddFrames ( $i, $d ) {
  122.  
  123.         $Locals_str = 13 + 3 * ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) );
  124.  
  125.         $Locals_end = strlen ( $this->BUF [ $i ] ) - $Locals_str - 1;
  126.         $Locals_tmp = substr ( $this->BUF [ $i ], $Locals_str, $Locals_end );
  127.  
  128.         $Global_len = 2 << ( ord ( $this->BUF [ 0  ] { 10 } ) & 0x07 );
  129.         $Locals_len = 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 );
  130.  
  131.         $Global_rgb = substr ( $this->BUF [ 0  ], 13,
  132.                             3 * ( 2 << ( ord ( $this->BUF [ 0  ] { 10 } ) & 0x07 ) ) );
  133.         $Locals_rgb = substr ( $this->BUF [ $i ], 13,
  134.                             3 * ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) ) );
  135.  
  136.         $Locals_ext = "!\xF9\x04" . chr ( ( $this->DIS << 2 ) + 0 ) .
  137.                         chr ( ( $d >> 0 ) & 0xFF ) . chr ( ( $d >> 8 ) & 0xFF ) . "\x0\x0";
  138.  
  139.         if ( $this->COL > -1 && ord ( $this->BUF [ $i ] { 10 } ) & 0x80 ) {
  140.             for ( $j = 0; $j < ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) ); $j++ ) {
  141.                 if    (
  142.                         ord ( $Locals_rgb { 3 * $j + 0 } ) == ( ( $this->COL >> 16 ) & 0xFF ) &&
  143.                         ord ( $Locals_rgb { 3 * $j + 1 } ) == ( ( $this->COL >>  8 ) & 0xFF ) &&
  144.                         ord ( $Locals_rgb { 3 * $j + 2 } ) == ( ( $this->COL >>  0 ) & 0xFF )
  145.                     ) {
  146.                     $Locals_ext = "!\xF9\x04" . chr ( ( $this->DIS << 2 ) + 1 ) .
  147.                                     chr ( ( $d >> 0 ) & 0xFF ) . chr ( ( $d >> 8 ) & 0xFF ) . chr ( $j ) . "\x0";
  148.                     break;
  149.                 }
  150.             }
  151.         }
  152.         switch ( $Locals_tmp { 0 } ) {
  153.             case "!":
  154.                 $Locals_img = substr ( $Locals_tmp, 8, 10 );
  155.                 $Locals_tmp = substr ( $Locals_tmp, 18, strlen ( $Locals_tmp ) - 18 );
  156.                 break;
  157.             case ",":
  158.                 $Locals_img = substr ( $Locals_tmp, 0, 10 );
  159.                 $Locals_tmp = substr ( $Locals_tmp, 10, strlen ( $Locals_tmp ) - 10 );
  160.                 break;
  161.         }
  162.         if ( ord ( $this->BUF [ $i ] { 10 } ) & 0x80 && $this->IMG > -1 ) {
  163.             if ( $Global_len == $Locals_len ) {
  164.                 if ( GIFEncoder::GIFBlockCompare ( $Global_rgb, $Locals_rgb, $Global_len ) ) {
  165.                     $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_tmp );
  166.                 }
  167.                 else {
  168.                     $byte  = ord ( $Locals_img { 9 } );
  169.                     $byte |= 0x80;
  170.                     $byte &= 0xF8;
  171.                     $byte |= ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x07 );
  172.                     $Locals_img { 9 } = chr ( $byte );
  173.                     $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp );
  174.                 }
  175.             }
  176.             else {
  177.                 $byte  = ord ( $Locals_img { 9 } );
  178.                 $byte |= 0x80;
  179.                 $byte &= 0xF8;
  180.                 $byte |= ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 );
  181.                 $Locals_img { 9 } = chr ( $byte );
  182.                 $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp );
  183.             }
  184.         }
  185.         else {
  186.             $this->GIF .= ( $Locals_ext . $Locals_img . $Locals_tmp );
  187.         }
  188.         $this->IMG  = 1;
  189.     }
  190.     /*
  191.     :::::::::::::::::::::::::::::::::::::::::::::::::::
  192.     ::
  193.     ::    GIFAddFooter...
  194.     ::
  195.     */
  196.     function GIFAddFooter ( ) {
  197.         $this->GIF .= ";";
  198.     }
  199.     /*
  200.     :::::::::::::::::::::::::::::::::::::::::::::::::::
  201.     ::
  202.     ::    GIFBlockCompare...
  203.     ::
  204.     */
  205.     function GIFBlockCompare ( $GlobalBlock, $LocalBlock, $Len ) {
  206.  
  207.         for ( $i = 0; $i < $Len; $i++ ) {
  208.             if    (
  209.                     $GlobalBlock { 3 * $i + 0 } != $LocalBlock { 3 * $i + 0 } ||
  210.                     $GlobalBlock { 3 * $i + 1 } != $LocalBlock { 3 * $i + 1 } ||
  211.                     $GlobalBlock { 3 * $i + 2 } != $LocalBlock { 3 * $i + 2 }
  212.                 ) {
  213.                     return ( 0 );
  214.             }
  215.         }
  216.  
  217.         return ( 1 );
  218.     }
  219.     /*
  220.     :::::::::::::::::::::::::::::::::::::::::::::::::::
  221.     ::
  222.     ::    GIFWord...
  223.     ::
  224.     */
  225.     function GIFWord ( $int ) {
  226.  
  227.         return ( chr ( $int & 0xFF ) . chr ( ( $int >> 8 ) & 0xFF ) );
  228.     }
  229.     /*
  230.     :::::::::::::::::::::::::::::::::::::::::::::::::::
  231.     ::
  232.     ::    GetAnimation...
  233.     ::
  234.     */
  235.     function GetAnimation ( ) {
  236.         return ( $this->GIF );
  237.     }
  238. }
  239. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement