无法从图像中检索关键字

时间:2016-12-30 11:48:39

标签: php image jpeg exif

我正在尝试使用PHP的本机函数检索JPEG文件的关键字:

exif_read_data

但它不会检索关键字的数据。

尝试了许多方法,其他图书馆如PEL等。它们都没有奏效。

以下是我在Mac上看到的内容:

enter image description here

这是exif_read_data($image, 'ANY_TAG', true);函数的输出:

array(4) {
  ["FILE"]=>
  array(6) {
    ["FileName"]=>
    string(17) "casino-st1-01.jpg"
    ["FileDateTime"]=>
    int(1483098243)
    ["FileSize"]=>
    int(454913)
    ["FileType"]=>
    int(2)
    ["MimeType"]=>
    string(10) "image/jpeg"
    ["SectionsFound"]=>
    string(19) "ANY_TAG, IFD0, EXIF"
  }
  ["COMPUTED"]=>
  array(5) {
    ["html"]=>
    string(26) "width="4167" height="4167""
    ["Height"]=>
    int(4167)
    ["Width"]=>
    int(4167)
    ["IsColor"]=>
    int(1)
    ["ByteOrderMotorola"]=>
    int(1)
  }
  ["IFD0"]=>
  array(4) {
    ["ImageDescription"]=>
    string(58) "playing card icon illustration isolated vector sign symbol"
    ["Orientation"]=>
    int(1)
    ["Software"]=>
    string(35) "Adobe Illustrator CC 2015 (Windows)"
    ["DateTime"]=>
    string(19) "2016:12:15 08:30:58"
  }
  ["EXIF"]=>
  array(2) {
    ["ExifVersion"]=>
    string(4) "0221"
    ["ColorSpace"]=>
    int(65535)
  }
}

我在Ubuntu 16上使用最新的PHP7安装,我的PHP设置设置为:

[exif]
exif.encode_unicode = UTF-8
exif.decode_unicode_motorola = UCS-2LE

我做错了什么?

1 个答案:

答案 0 :(得分:3)

我认为数据在IPTC块中。

所以试试这个:

<?php

$size = getimagesize('leaf.jpg', $info );
//var_dump($info);die;
var_dump(iptcparse($info['APP13']));

更详细的示例您可以在此官方文档的评论中找到:http://php.net/manual/en/function.iptcparse.php

IPTC标题是:

<?php 

DEFINE('IPTC_OBJECT_NAME', '2#005');
DEFINE('IPTC_EDIT_STATUS', '2#007');
DEFINE('IPTC_PRIORITY', '2#010');
DEFINE('IPTC_CATEGORY', '2#015');
DEFINE('IPTC_SUPPLEMENTAL_CATEGORY', '2#020');
DEFINE('IPTC_FIXTURE_IDENTIFIER', '2#022');
DEFINE('IPTC_KEYWORDS', '2#025');
DEFINE('IPTC_RELEASE_DATE', '2#030');
DEFINE('IPTC_RELEASE_TIME', '2#035');
DEFINE('IPTC_SPECIAL_INSTRUCTIONS', '2#040');
DEFINE('IPTC_REFERENCE_SERVICE', '2#045');
DEFINE('IPTC_REFERENCE_DATE', '2#047');
DEFINE('IPTC_REFERENCE_NUMBER', '2#050');
DEFINE('IPTC_CREATED_DATE', '2#055');
DEFINE('IPTC_CREATED_TIME', '2#060');
DEFINE('IPTC_ORIGINATING_PROGRAM', '2#065');
DEFINE('IPTC_PROGRAM_VERSION', '2#070');
DEFINE('IPTC_OBJECT_CYCLE', '2#075');
DEFINE('IPTC_BYLINE', '2#080');
DEFINE('IPTC_BYLINE_TITLE', '2#085');
DEFINE('IPTC_CITY', '2#090');
DEFINE('IPTC_PROVINCE_STATE', '2#095');
DEFINE('IPTC_COUNTRY_CODE', '2#100');
DEFINE('IPTC_COUNTRY', '2#101');
DEFINE('IPTC_ORIGINAL_TRANSMISSION_REFERENCE', '2#103');
DEFINE('IPTC_HEADLINE', '2#105');
DEFINE('IPTC_CREDIT', '2#110');
DEFINE('IPTC_SOURCE', '2#115');
DEFINE('IPTC_COPYRIGHT_STRING', '2#116');
DEFINE('IPTC_CAPTION', '2#120');
DEFINE('IPTC_LOCAL_CAPTION', '2#121');

这是你的班级使用IPTC OOP方式(:

class IPTCData {
  const KEYWORDS_HEADER = '2#025';
  const TITLE_HEADER = '2#005';
  const DESCRIPTION_HEADER = '2#120';

  private $file;
  private $info;
  private $data = [];

  public function __construct($file) {
    $tgis->file = $file;
    getimagesize($file, $this->info);
    if(isset($info['APP13'])) {
      $this->data = $info['APP13'];
    }
  }

  public function getFile() {
    return $this->file;
  }

  public function getInfo() {
    return $this->info;
  }

  public function getIPTCData($key = null) {
    if($key) {
      return isset($this->data[$key])
             ? $this->data[$key] : null;
    }
    return $this->data;
  }

  private function pickOneFromData($key) {
    $data = $this->getIPTCData($key);
    return (is_array($data) && !empty($data))
           ? $data[0] : null;
  }

  public function getKeywords() {
    return $this->getIPTCData(self::KEYWORDS_HEADER);
  }

  public function getTitle() {
    return $this->pickOneFromData(self::TITLE_HEADER);
  }

  public function getDescription() {
    return $this->pickOneFromData(self::DESCRIPTION_HEADER);
  }

  public function getAll() {
    $title = $this->getTitle();
    $descriptions = $this->getDescription();
    $keywords = $this->getKeywords();
    return compact('title', 'descriptions', 'keywords');
  }
}

用法:

$file = 'leaf.jpg';
$iptcData = new IPTCData($file);

$title = $iptcData->getTitle();
$descriptions = $iptcData->getDescription();
$keywords = $iptcData->getKeywords();

或与DB ORM一起使用(例如:Eloquent):

$file = 'leaf.jpg';
$iptcData = new IPTCData($file);

$ImageInfo = new ImageInfo($iptcData->getAll());
$ImageInfo->save();



附:随意为你的功能扩展我的课程