/** * Load and parse Exif data. * * This will populate the object with Exif data, contained as a * {@link PelTiff} object. This TIFF object can be accessed with * the {@link getTiff()} method. * * @param PelDataWindow $d */ public function load(PelDataWindow $d) { Pel::debug('Parsing %d bytes of Exif data...', $d->getSize()); /* There must be at least 6 bytes for the Exif header. */ if ($d->getSize() < 6) { throw new PelInvalidDataException('Expected at least 6 bytes of Exif ' . 'data, found just %d bytes.', $d->getSize()); } /* Verify the Exif header */ if ($d->strcmp(0, self::EXIF_HEADER)) { $d->setWindowStart(strlen(self::EXIF_HEADER)); } else { throw new PelInvalidDataException('Exif header not found.'); } /* The rest of the data is TIFF data. */ $this->tiff = new PelTiff(); $this->tiff->load($d); }
/** * 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 {@link 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 {@link PelJpegMarker::SOS} section at any given time. * * @param PelDataWindow the data that will be turned into JPEG * sections. */ function load(PelDataWindow $d) { Pel::debug('Parsing %d bytes...', $d->getSize()); /* JPEG data is stored in big-endian format. */ $d->setByteOrder(PelConvert::BIG_ENDIAN); /* Run through the data to read the sections in the image. After * each section is read, the start of the data window will be * moved forward, and after the last section we'll terminate with * no data left in the window. */ while ($d->getSize() > 0) { /* JPEG sections start with 0xFF. The first byte that is not * 0xFF is a marker (hopefully). */ for ($i = 0; $i < 7; $i++) { if ($d->getByte($i) != 0xff) { break; } } $marker = $d->getByte($i); if (!PelJpegMarker::isValid($marker)) { throw new PelJpegInvalidMarkerException($marker, $i); } /* Move window so first byte becomes first byte in this * section. */ $d->setWindowStart($i + 1); if ($marker == PelJpegMarker::SOI || $marker == PelJpegMarker::EOI) { $content = new PelJpegContent(new PelDataWindow()); $this->appendSection($marker, $content); } else { /* Read the length of the section. The length includes the * two bytes used to store the length. */ $len = $d->getShort(0) - 2; Pel::debug('Found %s section of length %d', PelJpegMarker::getName($marker), $len); /* Skip past the length. */ $d->setWindowStart(2); if ($marker == PelJpegMarker::APP1) { try { $content = new PelExif(); $content->load($d->getClone(0, $len)); } catch (PelInvalidDataException $e) { /* We store the data as normal JPEG content if it could * not be parsed as Exif data. */ $content = new PelJpegContent($d->getClone(0, $len)); } $this->appendSection($marker, $content); /* Skip past the data. */ $d->setWindowStart($len); } elseif ($marker == PelJpegMarker::COM) { $content = new PelJpegComment(); $content->load($d->getClone(0, $len)); $this->appendSection($marker, $content); $d->setWindowStart($len); } else { $content = new PelJpegContent($d->getClone(0, $len)); $this->appendSection($marker, $content); /* Skip past the data. */ $d->setWindowStart($len); /* In case of SOS, image data will follow. */ if ($marker == PelJpegMarker::SOS) { /* Some images have some trailing (garbage?) following the * EOI marker. To handle this we seek backwards until we * find the EOI marker. Any trailing content is stored as * a PelJpegContent object. */ $length = $d->getSize(); while ($d->getByte($length - 2) != 0xff || $d->getByte($length - 1) != PelJpegMarker::EOI) { $length--; } $this->jpeg_data = $d->getClone(0, $length - 2); Pel::debug('JPEG data: ' . $this->jpeg_data->__toString()); /* Append the EOI. */ $this->appendSection(PelJpegMarker::EOI, new PelJpegContent(new PelDataWindow())); /* Now check to see if there are any trailing data. */ if ($length != $d->getSize()) { Pel::maybeThrow(new PelException('Found trailing content ' . 'after EOI: %d bytes', $d->getSize() - $length)); $content = new PelJpegContent($d->getClone($length)); /* We don't have a proper JPEG marker for trailing * garbage, so we just use 0x00... */ $this->appendSection(0x0, $content); } /* Done with the loop. */ break; } } } } /* while ($d->getSize() > 0) */ }