Login   Register  
PHP Classes
elePHPant
Icontem

File: example06.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  >  example06.php  >  Download  
File: example06.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-18 06:58
Size: 8,461 bytes
 

Contents

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

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

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

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

           echo 
"<br>READING <b>BITMAP INFO HEADER</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 ($nbytes):<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 ($nbytes):<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 ($nbytes):<b>$IMAGE_H</b><br>" ;

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