duanliaoyin3171 2016-12-30 11:48
浏览 69
已采纳

无法从图像中检索关键字

I'm trying to retrieve JPEG file's keywords using PHP's native function:

exif_read_data

But it doesn't retrieve Keywords' data.

Tried bunch of ways, other libraries like PEL, etc. None of them worked.

Here is what I see on my Mac:

enter image description here

And here is the output of exif_read_data($image, 'ANY_TAG', true); function:

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)
  }
}

I'm using latest PHP7 installation on Ubuntu 16 and my PHP settings are set to:

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

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • dongweihuan8610 2016-12-30 12:05
    关注

    I think that data is in IPTC block.

    So try this one:

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

    more detailed examples You can find in comments
    in this official doc: http://php.net/manual/en/function.iptcparse.php

    IPTC headers are:

    <?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');
    

    and here is Your class to work with IPTC OOP way (:

    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');
      }
    }
    

    usage:

    $file = 'leaf.jpg';
    $iptcData = new IPTCData($file);
    
    $title = $iptcData->getTitle();
    $descriptions = $iptcData->getDescription();
    $keywords = $iptcData->getKeywords();
    

    or to use with DB ORMs (for example: Eloquent):

    $file = 'leaf.jpg';
    $iptcData = new IPTCData($file);
    
    $ImageInfo = new ImageInfo($iptcData->getAll());
    $ImageInfo->save();
    



    P.S. feel free to extend my class with necessary for You functions

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog