PHP Classes

File: example04.php

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

Contents

Class file image Download
<?php
     
echo "<b>EXAMPLE 04</b>: we read some fields in the JPEG header<br><br>" ;
?>
We refer to this JFIF header structure in C.
<pre>
typedef struct _JFIFHeader
{
  BYTE SOI[2]; /* 00h Start of Image Marker */
  BYTE APP0[2]; /* 02h Application Use Marker */
  BYTE Length[2]; /* 04h Length of APP0 Field */
  BYTE Identifier[5]; /* 06h "JFIF" (zero terminated) Id String */
  BYTE Version[2]; /* 07h JFIF Format Revision */
  BYTE Units; /* 09h Units used for Resolution */
  BYTE Xdensity[2]; /* 0Ah Horizontal Resolution */
  BYTE Ydensity[2]; /* 0Ch Vertical Resolution */
  BYTE XThumbnail; /* 0Eh Horizontal Pixel Count */
  BYTE YThumbnail; /* 0Fh Vertical Pixel Count */

} JFIFHEAD;
</pre>
<?php // example 01 : simple open and close
     
require_once( "cfile.class.php" );
     
     
$CANDIDATEfile = "andromeda.jpg" ;
     
     
$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 ) )."<br>" ;
          
          
$bBEGIN = $cfile->move_to_beginning();
           echo (
$bBEGIN ) ? "OK MOVE TO BEGINNING ...<br>" : "CAN'T MOVE TO THE BEGINNING ...<br>" ;
          
          
$nbytes = 2 ; // Start of Image Marker
          
$READ = $cfile->read( $nbytes, CFILE_BINARY_INT_MODE ) ;
          
          
$nbytes = 2 ; // Application Use Marker
          
$READ = $cfile->read( $nbytes, CFILE_BINARY_INT_MODE ) ;

          
$nbytes = 2 ; // Length of APP0 Field
          
$READ = $cfile->read( $nbytes, CFILE_BINARY_INT_MODE ) ;

          
$nbytes = 5 ; // IDENTIFIER JFIF
          
$READ = $cfile->read( $nbytes, CFILE_TEXT_MODE ) ;
           echo (
$READ === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" : "JFIF IDENTIFIER:<b>$READ</b><br>" ;

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

          
$nbytes = 1 ; // MINOR VERSION JFIF
          
$READ = $cfile->read( $nbytes, CFILE_BINARY_INT_MODE ) ;
           echo (
$READ === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" : "MINOR VERSION:<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() ;
?>