<?php // example 01 : simple open and close
require_once( "cfile.class.php" );
echo "<b>EXAMPLE 08</b>: we read some fields in the PNG header<br><br>" ;
$CANDIDATEfile = "flowers.indexed.png" ;
$cfile = new cfile( $CANDIDATEfile );
$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 = 1 ; // SKIP
$READ = $cfile->read( $nbytes, CFILE_BINARY_INT_MODE ) ;
echo ( $READ === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" : "FIRST BYTE (SKIP):<b>$READ</b><br>" ;
$nbytes = 3 ; // PNG MARKER
$READ = $cfile->read( $nbytes, CFILE_TEXT_MODE ) ;
echo ( $READ === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" : "PNG MARKER:<b>$READ</b><br>" ;
// SKIP DOS CONFIGURATION BYTES
$cfile->move( $cfile->get_pointer_position(), 4 );
$nbytes = 4 ; // CHUNK LENGTH
$CHUNK_DATA_LENGTH = $cfile->read( $nbytes, CFILE_BINARY_INT_MODE, CFILE_BIG_ENDIAN, false ) ;
echo ( $CHUNK_DATA_LENGTH === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" : "CHUNK LENGTH:<b>$CHUNK_DATA_LENGTH</b> bytes<br>" ;
$nbytes = 4 ; // CHUNK MARKER
$READ = $cfile->read( $nbytes, CFILE_TEXT_MODE ) ;
echo ( $READ === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" : "CHUNK MARKER:<b>$READ</b><br>" ;
if ( $READ == "IHDR" )
{
// WIDTH
$IMAGE_W = $cfile->read( 4, CFILE_BINARY_INT_MODE, CFILE_BIG_ENDIAN, false ) ;
echo ( $IMAGE_W === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" : "WIDTH:<b>$IMAGE_W</b><br>" ;
// HEIGHT
$IMAGE_H = $cfile->read( 4, CFILE_BINARY_INT_MODE, CFILE_BIG_ENDIAN, false ) ;
echo ( $IMAGE_H === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" : "HEIGHT:<b>$IMAGE_H</b><br>" ;
// BIT DEPTH
$BIT_DEPTH = $cfile->read( 1, CFILE_BINARY_INT_MODE, CFILE_BIG_ENDIAN, false ) ;
echo ( $BIT_DEPTH === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" : "BIT DEPTH:<b>$BIT_DEPTH</b><br>" ;
// COLOR_TYPE
$COLOR_TYPE = $cfile->read( 1, CFILE_BINARY_INT_MODE, CFILE_BIG_ENDIAN, false ) ;
switch( $COLOR_TYPE )
{
case 0: $COLOR_TYPE = "Grayscale" ; break ;
case 2: $COLOR_TYPE = "RGB" ; break ;
case 3: $COLOR_TYPE = "Indexed" ; break ;
case 4: $COLOR_TYPE = "Grayscale" ; break ;
case 6: $COLOR_TYPE = "RGB" ; break ;
}
echo ( $COLOR_TYPE === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" : "COLOR_TYPE:<b>$COLOR_TYPE</b><br>" ;
// COMPRESSION
$COMPRESSION = $cfile->read( 1, CFILE_BINARY_INT_MODE, CFILE_BIG_ENDIAN, false ) ;
echo ( $COMPRESSION === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" : "COMPRESSION:<b>$COMPRESSION</b><br>" ;
// FILTER
$FILTER = $cfile->read( 1, CFILE_BINARY_INT_MODE, CFILE_BIG_ENDIAN, false ) ;
echo ( $FILTER === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" : "FILTER:<b>$FILTER</b><br>" ;
// INTERLACE
$INTERLACE = $cfile->read( 1, CFILE_BINARY_INT_MODE, CFILE_BIG_ENDIAN, false ) ;
echo ( $INTERLACE === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" : "INTERLACE:<b>$INTERLACE</b><br>" ;
}
$cfile->move( $cfile->get_pointer_position(), 4 ); // move to chunk end
$nbytes = 4 ; // CHUNK LENGTH
$CHUNK_DATA_LENGTH = $cfile->read( $nbytes, CFILE_BINARY_INT_MODE, CFILE_BIG_ENDIAN, false ) ;
echo ( $CHUNK_DATA_LENGTH === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" : "CHUNK LENGTH:<b>$CHUNK_DATA_LENGTH</b> bytes<br>" ;
$nbytes = 4 ; // CHUNK MARKER
$READ = $cfile->read( $nbytes, CFILE_TEXT_MODE ) ;
echo ( $READ === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" : "CHUNK MARKER:<b>$READ</b><br>" ;
if ( $READ == "PLTE" )
{
$CLRScount = 256 ; $COLONNE = 16 ; $nbytes = 1 ;
echo "<br>
<table cellpadding=0 cellspacing=0 valign=\"top\" ALIGN=\"left\">
<tr><td HEIGHT=\"1\"></td></tr>
<tr><td COLSPAN=\"".( $COLONNE * 2 )."\">PALETTE</td></tr>" ;
for( $i = 0 ; $i < $CLRScount ; $i++ )
{
$PALETTE_RED = $cfile->read( $nbytes, CFILE_BINARY_INT_MODE ) ;
$PALETTE_GREEN = $cfile->read( $nbytes, CFILE_BINARY_INT_MODE ) ;
$PALETTE_BLUE = $cfile->read( $nbytes, CFILE_BINARY_INT_MODE ) ;
$BK = "rgb( $PALETTE_RED, $PALETTE_GREEN, $PALETTE_BLUE )" ;
if ( $i % $COLONNE == 0 ) echo "<tr>" ;
echo "<td WIDTH=\"14\" HEIGHT=\"14\" STYLE=\"background-color:$BK\"></td>
<td WIDTH=\"1\"></td>" ;
if ( $i % $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() ;
?>
|