Login   Register  
PHP Classes
elePHPant
Icontem

File: example08.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of zandor  >  CFile  >  example08.php  >  Download  
File: example08.php
Role: Example script
Content type: text/plain
Description: reading some fields in the PNG header
Class: CFile
Read and write values to binary files
Author: By
Last change:
Date: 2011-08-17 06:31
Size: 6,729 bytes
 

Contents

Class file image Download
<?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->openCFILE_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 // SKIP
           
$READ $cfile->read$nbytesCFILE_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 // PNG MARKER
           
$READ $cfile->read$nbytesCFILE_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(), );

           
$nbytes // CHUNK LENGTH
           
$CHUNK_DATA_LENGTH $cfile->read$nbytesCFILE_BINARY_INT_MODECFILE_BIG_ENDIANfalse ) ;
           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 // CHUNK MARKER
           
$READ $cfile->read$nbytesCFILE_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->read4CFILE_BINARY_INT_MODECFILE_BIG_ENDIANfalse ) ;
               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->read4CFILE_BINARY_INT_MODECFILE_BIG_ENDIANfalse ) ;
               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->read1CFILE_BINARY_INT_MODECFILE_BIG_ENDIANfalse ) ;
               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->read1CFILE_BINARY_INT_MODECFILE_BIG_ENDIANfalse ) ;

               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->read1CFILE_BINARY_INT_MODECFILE_BIG_ENDIANfalse ) ;
               echo ( 
$COMPRESSION === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" "COMPRESSION:<b>$COMPRESSION</b><br>" ;

               
// FILTER
               
$FILTER $cfile->read1CFILE_BINARY_INT_MODECFILE_BIG_ENDIANfalse ) ;
               echo ( 
$FILTER === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" "FILTER:<b>$FILTER</b><br>" ;

               
// INTERLACE
               
$INTERLACE $cfile->read1CFILE_BINARY_INT_MODECFILE_BIG_ENDIANfalse ) ;
               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(), ); // move to chunk end
           
           
$nbytes // CHUNK LENGTH
           
$CHUNK_DATA_LENGTH $cfile->read$nbytesCFILE_BINARY_INT_MODECFILE_BIG_ENDIANfalse ) ;
           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 // CHUNK MARKER
           
$READ $cfile->read$nbytesCFILE_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 ;
                
                echo 
"<br>
                      <table cellpadding=0 cellspacing=0 valign=\"top\" ALIGN=\"left\">
                      <tr><td HEIGHT=\"1\"></td></tr>
                      <tr><td COLSPAN=\""
.( $COLONNE )."\">PALETTE</td></tr>" ;
                      
                for( 
$i $i $CLRScount $i++ )
                {
                    
$PALETTE_RED $cfile->read$nbytesCFILE_BINARY_INT_MODE ) ;
                    
$PALETTE_GREEN $cfile->read$nbytesCFILE_BINARY_INT_MODE ) ;
                    
$PALETTE_BLUE $cfile->read$nbytesCFILE_BINARY_INT_MODE ) ;
                    
                    
$BK "rgb( $PALETTE_RED$PALETTE_GREEN$PALETTE_BLUE )" ;
                    
                    if ( 
$i $COLONNE == ) echo "<tr>" ;
                    
                    echo 
"<td WIDTH=\"14\" HEIGHT=\"14\" STYLE=\"background-color:$BK\"></td>
                          <td WIDTH=\"1\"></td>" 
;
                              
                    if ( 
$i $COLONNE == ( $COLONNE ) )
                    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() ;
?>