You are here

class ImgData in Panopoly 8.2

Reads IPTC image data.

Hierarchy

  • class \Drupal\panopoly_media\ImgData

Expanded class hierarchy of ImgData

1 string reference to 'ImgData'
panopoly_media.services.yml in modules/panopoly/panopoly_media/panopoly_media.services.yml
modules/panopoly/panopoly_media/panopoly_media.services.yml
1 service uses ImgData
panopoly_media.img_data in modules/panopoly/panopoly_media/panopoly_media.services.yml
Drupal\panopoly_media\ImgData

File

modules/panopoly/panopoly_media/src/ImgData.php, line 8

Namespace

Drupal\panopoly_media
View source
class ImgData {

  /**
   * Form element mapping.
   *
   * @var array
   */
  protected $elementMap = [];

  /**
   * Mappings for IPTC attributes.
   *
   * @var array
   *
   * @todo use plugin system to manage these properties.
   */
  protected $iptcMapping = [
    'title' => '2#005',
    'keywords' => '2#025',
    'copyright' => '2#116',
    'caption' => '2#120',
    'headline' => '2#105',
    'credit' => '2#110',
    'source' => '2#115',
    'jobtitle' => '2#085',
  ];

  /**
   * Gets an image's IPTC data.
   *
   * @param string $uri
   *   The uri of the image.
   *
   * @return array
   *   The IPTC data, keyed by attribute.
   *
   * @see \PHPExif\Adapter\Native
   */
  public function getData($uri) {
    $data = [];

    /** @var \Drupal\Core\File\FileSystemInterface $f */
    $f = \Drupal::service('file_system');
    $size = @getimagesize($f
      ->realpath($uri), $info);

    // Ensure file and IPTC info is present.
    if (!$size || !$info || !isset($info['APP13'])) {
      return $data;
    }

    // Ensure IPTC could be parsed.
    if (!($iptc = iptcparse($info["APP13"]))) {
      return $data;
    }

    // Map attributes into sensible structure.
    foreach ($this->iptcMapping as $name => $field) {
      if (!isset($iptc[$field])) {
        $data[$name] = NULL;
        continue;
      }
      if (count($iptc[$field]) === 1) {
        $val = trim(reset($iptc[$field]));
        if (!empty($val)) {
          $data[$name] = reset($iptc[$field]);
        }
      }
      else {
        $data[$name] = $iptc[$field];
      }
    }
    return $data;
  }

  /**
   * Provides a mapping of form elements to IPTC data.
   *
   * @param string $entityType
   *   The entity type that the widget is placed on.
   *
   * @return array
   *   The form element map.
   *
   * @todo add persistent caching.
   */
  public function getElementMap($entityType) {
    if (!isset($this->elementMap[$entityType])) {
      $map = [
        'name' => [
          'iptc' => 'name',
          'formElement' => [
            'name',
            0,
            'value',
          ],
        ],
        'alt' => [
          'iptc' => 'headline',
          'element' => [
            'alt',
          ],
        ],
        'description' => [
          'iptc' => 'caption',
          'formElement' => [
            'field_panopoly_media_description',
            0,
            'value',
          ],
        ],
      ];
      \Drupal::moduleHandler()
        ->alter('panopoly_media_iptc_mapping', $map, $entityType);
      $this->elementMap[$entityType] = $map;
    }
    return $this->elementMap[$entityType];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ImgData::$elementMap protected property Form element mapping.
ImgData::$iptcMapping protected property Mappings for IPTC attributes.
ImgData::getData public function Gets an image's IPTC data.
ImgData::getElementMap public function Provides a mapping of form elements to IPTC data.