用php编写exif数据

用php编写exif数据,php,metadata,jpeg,exif,Php,Metadata,Jpeg,Exif,我正在尝试创建一个网站,可以在其中添加和修改JPEG文件中的元数据 有没有一种方法可以让我以一种相当简单的方式编写exif数据 我看过一两个例子,但它们太复杂了,在我给出的时间框架内无法把握 我知道IPTC,我知道可以将元数据添加到JPEG文件中。但正确的做法是什么呢 如果有人能提供一些关于如何使用EXIF或IPTC或PHP的任何其他库或功能向JPEG添加元数据的帮助,我将不胜感激 更新: 首先,感谢您的回复 我已经看过代码了。我已经设法让它将默认标记添加到JPG中 对于代码的一小部分意味着什么

我正在尝试创建一个网站,可以在其中添加和修改JPEG文件中的元数据

有没有一种方法可以让我以一种相当简单的方式编写exif数据

我看过一两个例子,但它们太复杂了,在我给出的时间框架内无法把握

我知道IPTC,我知道可以将元数据添加到JPEG文件中。但正确的做法是什么呢

如果有人能提供一些关于如何使用EXIF或IPTC或PHP的任何其他库或功能向JPEG添加元数据的帮助,我将不胜感激

更新:

首先,感谢您的回复

我已经看过代码了。我已经设法让它将默认标记添加到JPG中

对于代码的一小部分意味着什么,我仍然有点困惑

例如,在php函数中写入exif数据:

function iptc_make_tag($rec, $data, $value) 
{ 
    $length = strlen($value); 
    $retval = chr(0x1C) . chr($rec) . chr($data);
    ...
}
我没有遇到函数变量,如果没有定义,
$rec
$data
$value
是如何被引用的。或者它们是从iptc制作标签中获取的

我回显了
$rec
$value
,但屏幕上没有返回值

if(isset($info['APP13']))
我不确定APP13是什么意思,当我尝试回显
$info
时,在表中回显
$info
时,我只得到以下信息

'2#120' => 'Test image', '2#116' => 'Copyright 2008-2009, The PHP Group' “2#120”=>“测试图像”, “2#116”=>“版权所有2008-2009,PHP集团”
我自己没有这方面的经验,但php的网站上有一些类似于您所寻找的东西:

如果你说的是这个意思 “我见过一两个例子,但它们太复杂了,在我给出的时间框架内无法把握。”

那么你可能会在你的头上

但是那页上的例子看起来一点也不难理解。

也许你可以试试:

  • 。一个库,用于使用PHP在JPEG和TIFF图像中读取和写入Exif头
  • 。允许读取、写入和显示以下JPEG元数据格式:EXIF 2.2、XMP/RDF、IPTC-NAA IIM 4.1等
  • 。ExifTool非常优秀。它基本上做到了这一切——EXIF、IPTC和XMP支持(读/写)以及对制造商扩展的支持

    • 我知道你找到了解决方案,但这可能会帮助其他正在寻找相同解决方案的人

      我修改了我发现的一个类(谢谢)

      所有对IPTC标签的引用都可以从这里读取

      现在是代码(PHP>=5.4):


      Imagick允许您设置EXIF数据,但仅设置内存中的对象,当将文件写入磁盘时,这些数据将被忽略。最流行的解决方案是向exiftools脱壳或使用PHP库。PEL的文档很少,API也不是真正的自解释

      我在尝试将正确的EXIF数据添加到将作为360图像上传到Facebook的图像时遇到了这个问题,这需要将特定的相机品牌和型号指定为EXIF。下面的代码将打开一个图像文件,设置其品牌和型号,并保存回磁盘。如果您希望设置其他EXIF数据,则有一个完整的所有受支持的PelTag常量列表


      setValue
      工作正常(使用图像编辑器测试),但
      getValue
      返回空值。知道吗?我想跟贝利的联系断了。尝试一下PEL-docs链接已断开,这里有一个可用的->@Andreas Bergström我来自一个库
      miljar/php-exif
      ,它允许您读取和设置exif数据。然而,我的问题是如何将更新的属性保存到原始文件或新文件中
      <?
      define("IPTC_OBJECT_NAME", "005");
      define("IPTC_EDIT_STATUS", "007");
      define("IPTC_PRIORITY", "010");
      define("IPTC_CATEGORY", "015");
      define("IPTC_SUPPLEMENTAL_CATEGORY", "020");
      define("IPTC_FIXTURE_IDENTIFIER", "022");
      define("IPTC_KEYWORDS", "025");
      define("IPTC_RELEASE_DATE", "030");
      define("IPTC_RELEASE_TIME", "035");
      define("IPTC_SPECIAL_INSTRUCTIONS", "040");
      define("IPTC_REFERENCE_SERVICE", "045");
      define("IPTC_REFERENCE_DATE", "047");
      define("IPTC_REFERENCE_NUMBER", "050");
      define("IPTC_CREATED_DATE", "055");
      define("IPTC_CREATED_TIME", "060");
      define("IPTC_ORIGINATING_PROGRAM", "065");
      define("IPTC_PROGRAM_VERSION", "070");
      define("IPTC_OBJECT_CYCLE", "075");
      define("IPTC_BYLINE", "080");
      define("IPTC_BYLINE_TITLE", "085");
      define("IPTC_CITY", "090");
      define("IPTC_PROVINCE_STATE", "095");
      define("IPTC_COUNTRY_CODE", "100");
      define("IPTC_COUNTRY", "101");
      define("IPTC_ORIGINAL_TRANSMISSION_REFERENCE", "103");
      define("IPTC_HEADLINE", "105");
      define("IPTC_CREDIT", "110");
      define("IPTC_SOURCE", "115");
      define("IPTC_COPYRIGHT_STRING", "116");
      define("IPTC_CAPTION", "120");
      define("IPTC_LOCAL_CAPTION", "121");
      
      class IPTC
      {
          var $meta = [];
          var $file = null;
      
          function __construct($filename)
          {
              $info = null;
      
              $size = getimagesize($filename, $info);
      
              if(isset($info["APP13"])) $this->meta = iptcparse($info["APP13"]);
      
              $this->file = $filename;
          }
      
          function getValue($tag)
          {
              return isset($this->meta["2#$tag"]) ? $this->meta["2#$tag"][0] : "";
          }
      
          function setValue($tag, $data)
          {
              $this->meta["2#$tag"] = [$data];
      
              $this->write();
          }
      
          private function write()
          {
              $mode = 0;
      
              $content = iptcembed($this->binary(), $this->file, $mode);   
      
              $filename = $this->file;
      
              if(file_exists($this->file)) unlink($this->file);
      
              $fp = fopen($this->file, "w");
              fwrite($fp, $content);
              fclose($fp);
          }         
      
          private function binary()
          {
              $data = "";
      
              foreach(array_keys($this->meta) as $key)
              {
                  $tag = str_replace("2#", "", $key);
                  $data .= $this->iptc_maketag(2, $tag, $this->meta[$key][0]);
              }       
      
              return $data;
          }
      
          function iptc_maketag($rec, $data, $value)
          {
              $length = strlen($value);
              $retval = chr(0x1C) . chr($rec) . chr($data);
      
              if($length < 0x8000)
              {
                  $retval .= chr($length >> 8) .  chr($length & 0xFF);
              }
              else
              {
                  $retval .= chr(0x80) . 
                             chr(0x04) . 
                             chr(($length >> 24) & 0xFF) . 
                             chr(($length >> 16) & 0xFF) . 
                             chr(($length >> 8) & 0xFF) . 
                             chr($length & 0xFF);
              }
      
              return $retval . $value;            
          }   
      
          function dump()
          {
              echo "<pre>";
              print_r($this->meta);
              echo "</pre>";
          }
      
          #requires GD library installed
          function removeAllTags()
          {
              $this->meta = [];
              $img = imagecreatefromstring(implode(file($this->file)));
              if(file_exists($this->file)) unlink($this->file);
              imagejpeg($img, $this->file, 100);
          }
      }
      
      $file = "photo.jpg";
      $objIPTC = new IPTC($file);
      
      //set title
      $objIPTC->setValue(IPTC_HEADLINE, "A title for this picture");
      
      //set description
      $objIPTC->setValue(IPTC_CAPTION, "Some words describing what can be seen in this picture.");
      
      echo $objIPTC->getValue(IPTC_HEADLINE);
      ?>
      
      $data = new PelDataWindow(file_get_contents('IMAGE PATH'));
      $tiff = null;
      $file = null;
      
      // If it is a JPEG-image, check if EXIF-headers exists
      if (PelJpeg::isValid($data)) {
          $jpeg = $file = new PelJpeg();
          $jpeg->load($data);
          $exif = $jpeg->getExif();
      
          // If no EXIF in image, create it
          if($exif == null) {
              $exif = new PelExif();
              $jpeg->setExif($exif);
      
              $tiff = new PelTiff();
              $exif->setTiff($tiff);
          }
          else {
              $tiff = $exif->getTiff();
          }
      }
      // If it is a TIFF EXIF-headers will always be set
      elseif (PelTiff::isValid($data)) {
          $tiff = $file = new PelTiff();
          $tiff->load($data);
      }
      else {
          throw new \Exception('Invalid image format');
      }
      
      // Get the first Ifd, where most common EXIF-tags reside
      $ifd0 = $tiff->getIfd();
      
      // If no Ifd info found, create it
      if($ifd0 == null) {
          $ifd0 = new PelIfd(PelIfd::IFD0);
          $tiff->setIfd($ifd0);
      }
      
      // See if the MAKE-tag already exists in Ifd
      $make = $ifd0->getEntry(PelTag::MAKE);
      
      // Create MAKE-tag if not found, otherwise just change the value
      if($make == null) {
          $make = new PelEntryAscii(PelTag::MAKE, 'RICOH');
          $ifd0->addEntry($make);
      }
      else {
          $make->setValue('RICOH');
      }
      
      // See if the MODEL-tag already exists in Ifd
      $model = $ifd0->getEntry(PelTag::MODEL);
      
      // Create MODEL-tag if not found, otherwise just change the value
      if($model == null) {
          $model = new PelEntryAscii(PelTag::MODEL, 'RICOH THETA S');
          $ifd0->addEntry($model);
      }
      else {
          $model->setValue('RICOH THETA S');
      }
      
      // Save to disk
      $file->saveFile('IMAGE.jpg');