Class PelJpeg

Description

Class for handling JPEG data.

The PelJpeg class defined here provides an abstraction for dealing with a JPEG file. The file will be contain a number of sections containing some content identified by a marker.

The getExif() method is used get hold of the APP1 section which stores Exif data. So if the name of the JPEG file is stored in $filename, then one would get hold of the Exif data by saying:

  1.  $jpeg new PelJpeg($filename);
  2.  $exif $jpeg->getExif();
  3.  $tiff $exif->getTiff();
  4.  $ifd0 $tiff->getIfd();
  5.  $exif $ifd0->getSubIfd(PelIfd::EXIF);
  6.  $ifd1 $ifd0->getNextIfd();

The $idf0 and $ifd1 variables will then be two TIFF Image File Directories, in which the data is stored under the keys found in PelTag.

Should one have some image data (in the form of a PelDataWindow) of an unknown type, then the PelJpeg::isValid() function is handy: it will quickly test if the data could be valid JPEG data. The PelTiff::isValid() function does the same for TIFF images.

Located in /src/PelJpeg.php (line 111)


	
			
Method Summary
static boolean isValid (PelDataWindow $d)
PelJpeg __construct ([mixed $data = false])
void appendSection (PelJpegMarker $marker, PelJpegContent $content)
void clearExif ()
string getBytes ()
PelJpegContent getSection (PelJpegMarker $marker, [int $skip = 0])
array getSections ()
void insertSection (PelJpegMarker $marker, PelJpegContent $content, int $offset)
void load (PelDataWindow $d)
void loadFile (string $filename)
void saveFile (string $filename)
void setExif (PelExif $exif)
string __toString ()
Methods
static method isValid (line 596)

Test data to see if it could be a valid JPEG image.

The function will only look at the first few bytes of the data, and try to determine if it could be a valid JPEG image based on those bytes. This means that the check is more like a heuristic than a rigorous check.

  • return: true if the bytes look like the beginning of a JPEG image, false otherwise.
  • see: PelTiff::isValid()
static boolean isValid (PelDataWindow $d)
Constructor __construct (line 161)

Construct a new JPEG object.

The new object will be empty unless an argument is given from which it can initialize itself. This can either be the filename of a JPEG image, a PelDataWindow object or a PHP image resource handle.

New Exif data (in the form of a PelExif object) can be inserted with the setExif() method:

  1.  $jpeg new PelJpeg($data);
  2.  // Create container for the Exif information:
  3.  $exif new PelExif();
  4.  // Now Add a PelTiff object with a PelIfd object with one or more
  5.  // PelEntry objects to $exif... Finally add $exif to $jpeg:
  6.  $jpeg->setExif($exif);

PelJpeg __construct ([mixed $data = false])
  • mixed $data: the data that this JPEG. This can either be a filename, a PelDataWindow object, or a PHP image resource handle.
appendSection (line 397)

Append a new section.

Used only when loading an image. If it used again later, then the section will end up after the @{link PelJpegMarker::EOI EOI marker} and will probably not be useful.

Please use @{link setExif()} instead if you intend to add Exif information to an image as that function will know the right place to insert the data.

void appendSection (PelJpegMarker $marker, PelJpegContent $content)
clearExif (line 372)

Clear any Exif data.

This method will only clear the first @{link PelJpegMarker::APP1} section found (there should normally be just one).

void clearExif ()
getBytes (line 505)

Turn this JPEG object into bytes.

The bytes returned by this method is ready to be stored in a file as a valid JPEG image. Use the saveFile() convenience method to do this.

  • return: bytes representing this JPEG object, including all its sections and their associated data.
string getBytes ()
getExif (line 357)

Get Exif data.

Use this to get the @{link PelExif Exif data} stored.

  • return: the Exif data found or null if the image has no Exif data.
PelExif getExif ()
getSection (line 446)

Get a section corresponding to a particular marker.

Please use the getExif() if you just need the Exif data.

This will search through the sections of this JPEG object, looking for a section identified with the specified marker. The content will then be returned. The optional argument can be used to skip over some of the sections. So if one is looking for the, say, third DHT section one would do:

  1.  $dht3 $jpeg->getSection(PelJpegMarker::DHT2);

  • return: the content found, or null if there is no content available.
PelJpegContent getSection (PelJpegMarker $marker, [int $skip = 0])
  • PelJpegMarker $marker: the marker identifying the section.
  • int $skip: the number of sections to be skipped. This must be a non-negative integer.
getSections (line 490)

Get all sections.

  • return:

    an array of (PelJpegMarker, PelJpegContent) pairs. Each pair is an array with the PelJpegMarker as the first element and the PelJpegContent as the second element, so the return type is an array of arrays.

    So to loop through all the sections in a given JPEG image do this:

    1.  foreach ($jpeg->getSections(as $section{
    2.    $marker $section[0];
    3.    $content $section[1];
    4.    // Use $marker and $content here.
    5.  }

    instead of this:

    1.  foreach ($jpeg->getSections(as $marker => $content{
    2.    // Does not work the way you would think...
    3.  }

    The problem is that there could be several sections with the same marker, and thus a simple associative array does not suffice.

array getSections ()
insertSection (line 417)

Insert a new section.

Please use @{link setExif()} instead if you intend to add Exif information to an image as that function will know the right place to insert the data.

void insertSection (PelJpegMarker $marker, PelJpegContent $content, int $offset)
  • PelJpegMarker $marker: the marker for the new section.
  • PelJpegContent $content: the content of the new section.
  • int $offset: the offset where the new section will be inserted --- use 0 to insert it at the very beginning, use 1 to insert it between sections 1 and 2, etc.
load (line 196)

Load data into a JPEG object.

The data supplied will be parsed and turned into an object structure representing the image. This structure can then be manipulated and later turned back into an string of bytes.

This methods can be called at any time after a JPEG object has been constructed, also after the appendSection() has been called to append custom sections. Loading several JPEG images into one object will accumulate the sections, but there will only be one PelJpegMarker::SOS section at any given time.

void load (PelDataWindow $d)
  • PelDataWindow $d: the data that will be turned into JPEG sections.
loadFile (line 312)

Load data from a file into a JPEG object.

void loadFile (string $filename)
  • string $filename: the filename. This must be a readable file.
saveFile (line 540)

Save the JPEG object as a JPEG image in a file.

void saveFile (string $filename)
  • string $filename: the filename to save in. An existing file with the same name will be overwritten!
setExif (line 325)

Set Exif data.

Use this to set the Exif data in the image. This will overwrite any old Exif information in the image.

void setExif (PelExif $exif)
__toString (line 553)

Make a string representation of this JPEG object.

This is mainly usefull for debugging. It will show the structure of the image, and its sections.

  • return: debugging information about this JPEG object.
string __toString ()

Documentation generated on Thu, 05 May 2011 07:19:18 +0200 by phpDocumentor 1.4.3