Login   Register  
PHP Classes
elePHPant
Icontem

File: example07.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  >  example07.php  >  Download  
File: example07.php
Role: Example script
Content type: text/plain
Description: read some fields in the BITMAP (.BMP) header and we raster an RGB bitmap picture in a HTML table, pixel by pixel
Class: CFile
Read and write values to binary files
Author: By
Last change:
Date: 2011-08-13 08:18
Size: 7,606 bytes
 

Contents

Class file image Download
<?php // example 01 : simple open and close
      
require_once( "cfile.class.php" );
      
      echo 
"<b>EXAMPLE 07</b>: we read some fields in the BITMAP (.BMP) header and we raster a palette-indexed bitmap picture in a HTML table, pixel by pixel<br><br>" ;
      
$CANDIDATEfile "waves.bmp" ;
      
      
$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 // BM MARKER
           
$READ $cfile->read$nbytesCFILE_TEXT_MODE ) ;
           echo ( 
$READ === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" "BITMAP MARKER:<b>$READ</b><br>" ;
           
           
$nbytes // HEADER SIZE
           
$BITMAPFILEHEADER_SIZE $cfile->read$nbytesCFILE_BINARY_INT_MODE ) ;

           
$nbytes // SKIP
           
$READ $cfile->read$nbytesCFILE_BINARY_INT_MODE ) ;

           
$nbytes // OFFSET BITS TO BITMAP DATA
           
$OFFSET_BITS $cfile->read$nbytesCFILE_BINARY_INT_MODE ) ;
           echo ( 
$READ === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" "OFFSET BITS TO BITMAP DATA:<b>$OFFSET_BITS</b><br>" ;

           
$nbytes // size of the BITMAPINFOHEADER structure, in bytes
           
$READ $cfile->read$nbytesCFILE_BINARY_INT_MODE ) ;
           echo ( 
$READ === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" "size of the BITMAPINFOHEADER structure, in bytes:<b>$READ</b><br>" ;

           
$nbytes // WIDTH
           
$IMAGE_W $cfile->read$nbytesCFILE_BINARY_INT_MODE ) ;
           echo ( 
$IMAGE_W === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" "IMAGE WIDTH :<b>$IMAGE_W</b><br>" ;

           
$nbytes // HEIGHT
           
$IMAGE_H $cfile->read$nbytesCFILE_BINARY_INT_MODE ) ;
           echo ( 
$IMAGE_H === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" "IMAGE HEIGHT :<b>$IMAGE_H</b><br>" ;

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

           
$nbytes // BITS per PIXEL
           
$BITS_PER_PIXEL $cfile->read$nbytesCFILE_BINARY_INT_MODE ) ;
           echo ( 
$BITS_PER_PIXEL === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" "BITS per PIXEL :<b>$BITS_PER_PIXEL</b><br>" ;

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

           
$nbytes // SKIP : SIZE IMAGE + XperMETER + YperMETER + CLRused + CLRimportant
           
$READ $cfile->read$nbytesCFILE_BINARY_INT_MODE ) ;
           
           
$PALETTEarray = array();
           
           if ( 
$BITS_PER_PIXEL 24 // read palette
           
{
                
$CLRScount pow2$BITS_PER_PIXEL );
                
$nbytes ;
                
$COLONNE 16 ;
                
                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_BLUE $cfile->read$nbytesCFILE_BINARY_INT_MODE ) ;
                    
$PALETTE_GREEN $cfile->read$nbytesCFILE_BINARY_INT_MODE ) ;
                    
$PALETTE_RED $cfile->read$nbytesCFILE_BINARY_INT_MODE ) ;
                    
$PALETTE_RESERVED $cfile->read$nbytesCFILE_BINARY_INT_MODE ) ;
                    
                    
$BK "rgb( $PALETTE_RED$PALETTE_GREEN$PALETTE_BLUE )" ;
                    
                    
$PALETTEarray[$i] = array( $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 
"<table cellpadding=0 cellspacing=0 valign=\"top\">
                 <tr><td HEIGHT=\"1\"></td></tr>
                 <tr><td COLSPAN=\""
.( $IMAGE_W )."\">PICTURE</td></tr>" ;
           
           
$BYTEStoREAD = ( $BITS_PER_PIXEL 24 ) ? ;

           
// padding the byte row in the image
           
$BYTES_PER_ROW $BYTEStoREAD $IMAGE_W $REMAINDER ;
           
$REMAINDER $BYTES_PER_ROW ;
           
           
$OFFSET $OFFSET_BITS ;
           
           for( 
$Hrunner $IMAGE_H $Hrunner >= $Hrunner-- )
           {
                 
$cfile->move$OFFSET $BYTES_PER_ROW $Hrunner) ;
           
                 echo 
"<tr>" ;
           
                 for( 
$Wrunner $Wrunner $IMAGE_W $Wrunner++ )
                 {
                      
$CLR "" ;
                 
                      if ( 
$BYTEStoREAD == )
                      {
                          
$PIXEL_B $cfile->read1CFILE_BINARY_INT_MODE ) ;
                          
$PIXEL_G $cfile->read1CFILE_BINARY_INT_MODE ) ;
                          
$PIXEL_R $cfile->read1CFILE_BINARY_INT_MODE ) ;
                      }
                      else
                      {
                          
$PIXEL_INDEX $cfile->read1CFILE_BINARY_INT_MODE ) ;
                          
$CLRtripleARRAY $PALETTEarray[$PIXEL_INDEX] ;

                          
$PIXEL_R $CLRtripleARRAY[0] ;
                          
$PIXEL_G $CLRtripleARRAY[1] ;
                          
$PIXEL_B $CLRtripleARRAY[2] ;
                      }
                      
                      
$BK "rgb( $PIXEL_R$PIXEL_G$PIXEL_B )" ;
                      
                      echo 
"<td WIDTH=\"1\" HEIGHT=\"1\" STYLE=\"background-color:$BK\"></td>" ;
                 }
                 
                 
// padding the byte row in the image
                 
for( $i $i $REMAINDER $i++ ) $cfile->read1CFILE_BINARY_INT_MODE ) ;

                 echo 
"</tr>" ;
                 
                 
ob_flush();      flush();
           }
           
           echo 
"</table>" ;

           echo 
"<br>" ;
           echo ( 
$cfile->close() ) ? "CLOSE FILE <b>$CANDIDATEfile</b> : SUCCESS" $cfile->get_error_string() ;
      }
      else echo 
$cfile->get_error_string() ;
?>