You are here

protected function FileSearch::extractContentExif in Search File Attachments 8

Extract IPTC metadata from image.

Parameters

\Drupal\Core\Entity\EntityInterface $file: The file object.

string $file_path: The path to the file.

Return value

string The extracted text.

1 call to FileSearch::extractContentExif()
FileSearch::getFileContent in src/Plugin/Search/FileSearch.php
Extract the content of the given file.

File

src/Plugin/Search/FileSearch.php, line 375

Class

FileSearch
Executes a keyword search for files against {file_managed} database table.

Namespace

Drupal\search_file_attachments\Plugin\Search

Code

protected function extractContentExif(EntityInterface $file, $file_path) {
  $content = '';
  $size = getimagesize($file_path, $info);
  if (isset($info['APP13'])) {
    $iptc_raw = iptcparse($info['APP13']);
    if (empty($iptc_raw)) {
      return $content;
    }
    $tagmarker = $this
      ->getExifTagmarker();
    $iptc = array();
    foreach ($iptc_raw as $key => $value) {

      // Add only values from the defined iptc fields.
      if (array_key_exists($key, $tagmarker)) {
        $iptc_field_value = array();
        foreach ($value as $innerkey => $innervalue) {
          $innervalue = trim($innervalue);
          if (!empty($innervalue)) {
            $iptc_field_value[] = $innervalue;
          }
        }
        if (!empty($iptc_field_value)) {
          $iptc[$tagmarker[$key]] = implode(', ', $iptc_field_value);
        }
      }
    }
    foreach ($iptc as $key => $value) {
      $content .= " <strong>{$key}:</strong> {$value}";
    }
    $content = trim($content);
  }
  return $content;
}