PHP Classes

File: example05.php

Recommend this page to a friend!
  Classes of Alessandro Rosa   CFile   example05.php   Download  
File: example05.php
Role: Example script
Content type: text/plain
Description: reading some fields in the BITMAP (.BMP) header
Class: CFile
Read and write values to binary files
Author: By
Last change:
Date: 12 years ago
Size: 4,167 bytes
 

Contents

Class file image Download
<?php // example 01 : simple open and close
     
echo "<b>EXAMPLE 05</b>: we read some fields in the BITMAP (.BMP) header<br><br>" ;

      require_once(
"cfile.class.php" );
     
     
$CANDIDATEfile = "andromeda.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
          
$READ = $cfile->read( $nbytes, CFILE_BINARY_INT_MODE ) ;
           echo (
$READ === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" : "OFFSET BITS TO BITMAP DATA ($nbytes):<b>$READ</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
          
$READ = $cfile->read( $nbytes, CFILE_BINARY_INT_MODE ) ;
           echo (
$READ === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" : "IMAGE WIDTH ($nbytes):<b>$READ</b><br>" ;

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

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

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