You are here

protected function Exif::doSaveMetadataToFile in File metadata manager 8

Same name and namespace in other branches
  1. 8.2 file_mdm_exif/src/Plugin/FileMetadata/Exif.php \Drupal\file_mdm_exif\Plugin\FileMetadata\Exif::doSaveMetadataToFile()

Saves metadata to file at URI.

Return value

bool TRUE if metadata was saved successfully, FALSE otherwise.

Overrides FileMetadataPluginBase::doSaveMetadataToFile

File

file_mdm_exif/src/Plugin/FileMetadata/Exif.php, line 223

Class

Exif
FileMetadata plugin for EXIF.

Namespace

Drupal\file_mdm_exif\Plugin\FileMetadata

Code

protected function doSaveMetadataToFile() {

  // Get the file as a PelJpeg or PelTiff object.
  $file = $this
    ->getFile();
  if (!$file) {
    return FALSE;
  }

  // Get the TIFF section if existing, or create one if not.
  if ($file instanceof PelJpeg) {
    $exif = $file
      ->getExif();
    if ($exif === NULL) {

      // If EXIF section is missing we simply create a new APP1 section
      // (a PelExif object) and add it to the PelJpeg object.
      $exif = new PelExif();
      $file
        ->setExif($exif);
    }
    $tiff = $exif
      ->getTiff();
    if ($tiff === NULL) {

      // Same for TIFF section.
      $tiff = new PelTiff();
      $exif
        ->setTiff($tiff);
    }
  }
  elseif ($file instanceof PelTiff) {
    $tiff = $file;
  }

  // Get IFD0 if existing, or create it if not.
  $ifd0 = $tiff
    ->getIfd();
  if ($ifd0 === NULL) {

    // No IFD in the TIFF data, we just create and insert an empty PelIfd
    // object.
    $ifd0 = new PelIfd(PelIfd::IFD0);
    $tiff
      ->setIfd($ifd0);
  }

  // Loops through in-memory metadata and update tag entries accordingly.
  foreach ($this->metadata as $ifd_id => $entries) {
    switch ($ifd_id) {
      case PelIfd::IFD0:
        $this
          ->setIfdEntries($ifd0, $entries);
        break;
      case PelIfd::IFD1:
        $ifd1 = $ifd0
          ->getNextIfd();
        if ($ifd1 === NULL) {
          $ifd1 = new PelIfd(PelIfd::IFD1);
          $ifd0
            ->setNextIfd($ifd1);
        }
        $this
          ->setIfdEntries($ifd1, $entries);
        break;
      case PelIfd::EXIF:
        $exif = $ifd0
          ->getSubIfd(PelIfd::EXIF);
        if ($exif === NULL) {
          $exif = new PelIfd(PelIfd::EXIF);
          $ifd0
            ->addSubIfd($exif);
        }
        $this
          ->setIfdEntries($exif, $entries);
        break;
      case PelIfd::INTEROPERABILITY:
        $exif = $ifd0
          ->getSubIfd(PelIfd::EXIF);
        if ($exif === NULL) {
          $exif = new PelIfd(PelIfd::EXIF);
          $ifd0
            ->addSubIfd($exif);
        }
        $interop = $exif
          ->getSubIfd(PelIfd::INTEROPERABILITY);
        if ($interop === NULL) {
          $interop = new PelIfd(PelIfd::INTEROPERABILITY);
          $exif
            ->addSubIfd($interop);
        }
        $this
          ->setIfdEntries($interop, $entries);
        break;
      case PelIfd::GPS:
        $gps = $ifd0
          ->getSubIfd(PelIfd::GPS);
        if ($gps === NULL) {
          $gps = new PelIfd(PelIfd::GPS);
          $ifd0
            ->addSubIfd($gps);
        }
        $this
          ->setIfdEntries($gps, $entries);
        break;
    }
  }
  return $file
    ->saveFile($this
    ->getLocalTempPath()) === FALSE ? FALSE : TRUE;
}