<?php // example 01 : simple open and close
require_once( "cfile.class.php" );
echo "<b>EXAMPLE 09</b>: we read some fields in the GIF header<br><br>" ;
$CANDIDATEfile = "flowers.gif" ;
$cfile = new cfile( $CANDIDATEfile, CFILE_LITTLE_ENDIAN );
$bOPEN = $cfile->open( CFILE_READ_MODE );
$bERR = $cfile->is_error() ;
if ( $bOPEN && !$bERR ) // you can check open return value or internal error for safe operation
{
echo "OPEN FILE <b>$CANDIDATEfile</b> : SUCCESS<br>" ;
echo "FILE SIZE : ".( filesize( $CANDIDATEfile ) )." bytes <br>" ;
$bBEGIN = $cfile->move_to_beginning();
echo ( $bBEGIN ) ? "OK MOVE TO BEGINNING ...<br>" : "CAN'T MOVE TO THE BEGINNING ...<br>" ;
$nbytes = 6 ; // GIF MARKER
$READ = $cfile->read( $nbytes, CFILE_TEXT_MODE ) ;
echo ( $READ === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" : "GIF MARKER:<b>$READ</b><br>" ;
$nbytes = 2 ; // IMAGE WIDTH
$READ = $cfile->read( $nbytes, CFILE_BINARY_INT_MODE ) ;
echo ( $READ === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" : "WIDTH:<b>$READ</b><br>" ;
$nbytes = 2 ; // IMAGE HEIGHT
$READ = $cfile->read( $nbytes, CFILE_BINARY_INT_MODE ) ;
echo ( $READ === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" : "HEIGHT:<b>$READ</b><br>" ;
$nbytes = 1 ; // BIT MAPPED
$READ = $cfile->read( $nbytes, CFILE_BINARY_INT_MODE ) ;
$COLOR_INDEX_BK = $cfile->read( $nbytes, CFILE_BINARY_INT_MODE ) ;
$RESERVED = $cfile->read( $nbytes, CFILE_BINARY_INT_MODE ) ;
$BITS_PER_PIXEL = $READ & 7 ;
$COLORS = pow( 2, $BITS_PER_PIXEL + 1 );
$nbytes = 1 ;
$COLONNE = 16 ;
echo "<br>
<table cellpadding=0 cellspacing=0 valign=\"top\">
<tr><td HEIGHT=\"1\"></td></tr>
<tr><td COLSPAN=\"".( $COLONNE * 2 )."\">PALETTE - $COLORS colors</td></tr>" ;
$nbytes = 1 ; // READ PALETTE
for( $clr_count = 0 ; $clr_count < $COLORS ; $clr_count++ )
{
$PALETTE_RED = $cfile->read( $nbytes, CFILE_BINARY_INT_MODE, CFILE_BIG_ENDIAN ) ;
$PALETTE_GREEN = $cfile->read( $nbytes, CFILE_BINARY_INT_MODE, CFILE_BIG_ENDIAN ) ;
$PALETTE_BLUE = $cfile->read( $nbytes, CFILE_BINARY_INT_MODE, CFILE_BIG_ENDIAN ) ;
$BK = "rgb( $PALETTE_RED, $PALETTE_GREEN, $PALETTE_BLUE )" ;
$PALETTEarray[$i] = array( $PALETTE_RED, $PALETTE_GREEN, $PALETTE_BLUE );
if ( $clr_count % $COLONNE == 0 ) echo "<tr>" ;
echo "<td WIDTH=\"14\" HEIGHT=\"14\" STYLE=\"background-color:$BK\"></td>
<td WIDTH=\"1\"></td>" ;
if ( $clr_count % $COLONNE == ( $COLONNE - 1 ) )
echo "</tr>
<tr><td HEIGHT=\"1\"></td></tr>" ;
}
echo "</table>" ;
echo "<br>" ;
echo ( $cfile->close() ) ? "CLOSE FILE <b>$CANDIDATEfile</b> : SUCCESS" : $cfile->get_error_string() ;
}
else echo $cfile->get_error_string() ;
?>
|