5

We create thumb images on our server and I'm looking for a way to save metadata (text) in that image. Is that possible?

At this moment we use PHP and we create JPG images.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • What do you think people should answer on this question? Yes\No? Telling us about file format would be a good start. – aku Sep 19 '08 at 14:54
  • you're right! updated my question –  Sep 19 '08 at 15:01
  • Yes. It's no problem. but your question is the same as this http://stackoverflow.com/questions/5384962/writing-exif-data-in-php/8688869#8688869. – Riceball LEE Dec 31 '11 at 15:20

5 Answers5

3

You question is the same as writing exif data in php.

My answer is:

  • PEL(PHP Exif Library). A library for reading and writing Exif headers in JPEG and TIFF images using PHP.
  • The PHP JPEG Metadata Toolkit. Allows reading, writing and display of the following JPEG metadata formats: EXIF 2.2, XMP / RDF, IPTC-NAA IIM 4.1 ect
  • ExifTool by perl. The ExifTool is excellent. It’s basically got it all – EXIF, IPTC and XMP support (read/write) and support for manufacturer extensions.
Riceball LEE
  • 1,541
  • 18
  • 18
2

I hope this helps you!

I modified a class that I found here (thanks debers).

And all the references to IPTC tags can be readed from this PDF

Code (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);
?>
Community
  • 1
  • 1
Matías Cánepa
  • 5,770
  • 4
  • 57
  • 97
  • 1
    You should ought to give credence to your source and not claim this to be your own script: http://www.php.net/manual/en/function.iptcembed.php#85887 – Brian Fegter Aug 06 '13 at 05:22
  • I have a question, how do I update EXIF data, for example I have this EXIF tag :0x9286 and how do I implement it? Can you help me? – SenTisso Aug 21 '18 at 18:01
  • @SenTisso the sample code is very complete. What is exactly your problem? – Matías Cánepa Aug 21 '18 at 18:08
  • For example here http://www.php.net/manual/en/function.iptcembed.php#example-3862 you update IPTC data using tags, for example `2#120` which is title and same in yours answer, but I need to update comments in image, which has EXIF tag (https://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/EXIF.html) and the tag is `0x9286` and my question is how do I add it there or how do I update comments. – SenTisso Aug 21 '18 at 18:13
  • Because you cant update comments via IPTC (https://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/IPTC.html) – SenTisso Aug 21 '18 at 18:14
  • I tried this to define new tag like this `define("COMMENTS", "0x9286")` but that gave me an error of bad formating so I tried this `define("COMMENTS", "9286")` and it didn't return any error, but it didn't update anything – SenTisso Aug 21 '18 at 18:16
  • @SenTisso Notice that EXIF data cannot be changed, that's data set by the device. You have to stick with the IPTC format, and the closest to comments will be IPTC_CAPTION – Matías Cánepa Aug 21 '18 at 18:17
  • Oh, I didn't know that, but with exiftool you can or even in windows explorer – SenTisso Aug 21 '18 at 18:19
  • Is not possible at least with PHP, that was my experience. That's why I ended up with IPTC – Matías Cánepa Aug 21 '18 at 19:58
1

EXIF or reuse an old "data-hiding" concept, Stenography

Aaron Arbery
  • 124
  • 1
1

Yes, it's possible.

You can use the almighty Exiftool perl utility, which handles nearly every known set of tags, both standard(EXIF, IPTC, Adobe's XMP, etc) and proprietary ones.

Camilo Díaz Repka
  • 4,805
  • 5
  • 43
  • 68
0

Embedding XMP Metadata in Application Files (PDF)