2011-03-21 7 views
12

JPEG 파일 내에서 메타 데이터를 추가하고 수정할 수있는 웹 사이트를 만들려고합니다.exif 데이터를 PHP로 작성

exif 데이터를 매우 쉽게 작성할 수있는 방법이 있습니까?

나는 하나 또는 두 가지 예를 보았지만, 나는 주어진 시간 안에 파악하기에는 너무 복잡하다.

IPTC를 알고 있고 메타 데이터를 JPEG 파일에 추가 할 수 있다는 것을 알고 있습니다. 그러나 이것을하는 올바른 방법은 무엇입니까?

누군가가 EXIF ​​나 IPTC 또는 다른 라이브러리 나 PHP의 기능을 사용하여 JPEG에 메타 데이터를 추가하는 방법에 대한 도움을 주시면 감사하겠습니다.

업데이트 : dbers에 의해 회신에 대한 모든 감사의

첫째.

코드를 검토했습니다. 나는 JPG에 기본 태그를 추가 할 수 있었다.

코드의 작은 부분이 무엇인지 아직도 혼란 스럽습니다.

PHP 함수에 EXIF ​​데이터를 쓰는 예를 들어

: 나는 함수 변수 건너하지 않은

function iptc_make_tag($rec, $data, $value) 
{ 
    $length = strlen($value); 
    $retval = chr(0x1C) . chr($rec) . chr($data); 
    ... 
} 

, 어떻게 그들이 정의 된 havent 한 경우 $rec, $data$value가 참조되고 있습니다. 아니면 iptc_make_tag에서 가져 왔습니까?

나는 $rec$value을 되풀이했지만 화면에 다시 표시되지 않습니다.

if(isset($info['APP13'])) 

나는 APP13이 무엇을 의미하는지 잘 모르겠어요 내가 $info를 에코하려고 할 때이 테이블에 $info을 에코 때, 난 그냥 다음을 얻는다.

 
'2#120' => 'Test image', 
'2#116' => 'Copyright 2008-2009, The PHP Group' 

답변

3

나는이 아무 경험이 자신이 없지만, PHP의 웹 사이트는 당신이 찾고있는 무엇처럼 보이는 무언가가 :

http://php.net/manual/en/function.iptcembed.php

만약 당신이 내가 " 을 말했을 때 당신이 무엇을 의미하는지 이잖아 하나 또는 두 가지 예를 보았지만, 주어진 시간 안에 파악하기에는 너무 복잡합니다. "

그러면 머리가 아플 수 있습니다.

그러나 그 페이지의 예제는 전혀 파악하기가 어려워 보입니다.

+0

긴 코드를 작성하는 것이 아니라 간단합니다. –

5

어쩌면 유 시도 할 수 있습니다 :

  • PEL(PHP Exif Library)을. PHP를 사용하여 JPEG 및 TIFF 이미지로 Exif 헤더를 읽고 쓰는 라이브러리.
  • The PHP JPEG Metadata Toolkit. EXIF 2.2, XMP/RDF, IPTC-NAA IIM 4 JPEG 메타 데이터 형식의 읽기, 쓰기 및 표시가 가능합니다.1 ect
  • ExifTool by perl. ExifTool은 우수합니다. 기본적으로 EXIF, IPTC 및 XMP 지원 (읽기/쓰기) 및 제조업체 확장 기능 지원이 모두 가능합니다.
+0

PEL에 대한 링크가 깨졌습니다. 시도해보십시오. artfulrobot

12

해결책을 찾았지만 다른 사람이 동일한 것을 찾는 데 도움이 될 수 있습니다.

here (감사 debers)이라는 클래스를 수정했습니다.

그리고 IPTC 태그에 대한 모든 참조

PDF

에서 읽혀질 수 있습니다 이제 코드 (PHP> = 5.4) :

<? 
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); 
?> 
+0

'setValue'가 잘 작동하고 (이미지 편집기로 테스트 한)'getValue'는 비어 있습니다. 어떤 아이디어? – azerafati

0

Imagick는 만에 EXIF ​​데이터를 설정할 수 않습니다 객체를 메모리에 저장하면 파일을 디스크에 쓸 때이 데이터는 무시됩니다. 가장 널리 사용되는 솔루션은 exoftools에 쉘 아웃하거나 PHP 라이브러리 PEL을 사용하는 것입니다. PEL의 문서는 드물고 API는 실제로 자체적으로 설명 할 수 없습니다.

360도 이미지로 업로드 될 이미지에 올바른 EXIF ​​데이터를 추가하려고 할 때이 문제가 발생했습니다. 특정 카메라 제조업체 및 모델이 EXIF로 지정되어야합니다. 아래의 코드는 이미지 파일을 열고, make와 모델을 설정하고, 디스크에 다시 저장합니다. 다른 EXIF ​​데이터를 설정하려는 경우 지원되는 모든 PelTag 상수의 전체 목록이 here in the PEL docs입니다.

$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'); 
관련 문제