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: 3 years ago
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->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>" ;
          
           echo
"<br>READING <b>BITMAP FILE HEADER</b><br>" ;
          
          
$nbytes = 2 ; // BM MARKER
          
$READ = $cfile->read( $nbytes, CFILE_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 = 4 ; // SIZE OF BITMAP
          
$READ = $cfile->read( $nbytes, CFILE_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 = 2 ; // RESERVED (0)
          
$READ = $cfile->read( $nbytes, CFILE_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 = 2 ; // RESERVED (1)
          
$READ = $cfile->read( $nbytes, CFILE_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 = 4 ; // OFFSET BITS TO BITMAP DATA
          
$OFFSET_BITS = $cfile->read( $nbytes, CFILE_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 = 4 ; // size of the BITMAPINFOHEADER structure, in bytes
          
$READ = $cfile->read( $nbytes, CFILE_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 = 4 ; // WIDTH
          
$IMAGE_W = $cfile->read( $nbytes, CFILE_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 = 4 ; // HEIGHT
          
$IMAGE_H = $cfile->read( $nbytes, CFILE_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 = 2 ; // PLANES
          
$READ = $cfile->read( $nbytes, CFILE_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 = 2 ; // BITS per PIXEL
          
$BITS_PER_PIXEL = $cfile->read( $nbytes, CFILE_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 = 4 ; // COMPRESSION
          
$READ = $cfile->read( $nbytes, CFILE_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 = 4 * 5 ; // SKIP : SIZE IMAGE + XperMETER + YperMETER + CLRused + CLRimportant
          
$READ = $cfile->read( $nbytes, CFILE_BINARY_INT_MODE ) ;
          
          
$PALETTEarray = array();
          
           if (
$BITS_PER_PIXEL < 24 ) // read palette
          
{
               
$CLRScount = pow( 2, $BITS_PER_PIXEL );
               
$nbytes = 1 ;
               
$COLONNE = 16 ;
               
                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_BLUE = $cfile->read( $nbytes, CFILE_BINARY_INT_MODE ) ;
                   
$PALETTE_GREEN = $cfile->read( $nbytes, CFILE_BINARY_INT_MODE ) ;
                   
$PALETTE_RED = $cfile->read( $nbytes, CFILE_BINARY_INT_MODE ) ;
                   
$PALETTE_RESERVED = $cfile->read( $nbytes, CFILE_BINARY_INT_MODE ) ;
                   
                   
$BK = "rgb( $PALETTE_RED, $PALETTE_GREEN, $PALETTE_BLUE )" ;
                   
                   
$PALETTEarray[$i] = array( $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>
                 <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 ) ? 1 : 3 ;

          
// padding the byte row in the image
          
$BYTES_PER_ROW = $BYTEStoREAD * $IMAGE_W + $REMAINDER ;
          
$REMAINDER = $BYTES_PER_ROW % 4 ;
          
          
$OFFSET = $OFFSET_BITS ;
          
           for(
$Hrunner = $IMAGE_H - 1 ; $Hrunner >= 0 ; $Hrunner-- )
           {
                
$cfile->move( $OFFSET + $BYTES_PER_ROW * $Hrunner, 0 ) ;
          
                 echo
"<tr>" ;
          
                 for(
$Wrunner = 0 ; $Wrunner < $IMAGE_W ; $Wrunner++ )
                 {
                     
$CLR = "" ;
                
                      if (
$BYTEStoREAD == 3 )
                      {
                         
$PIXEL_B = $cfile->read( 1, CFILE_BINARY_INT_MODE ) ;
                         
$PIXEL_G = $cfile->read( 1, CFILE_BINARY_INT_MODE ) ;
                         
$PIXEL_R = $cfile->read( 1, CFILE_BINARY_INT_MODE ) ;
                      }
                      else
                      {
                         
$PIXEL_INDEX = $cfile->read( 1, CFILE_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 = 0 ; $i < $REMAINDER ; $i++ ) $cfile->read( 1, CFILE_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() ;
?>