Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tagging images with php

I would like to write a script that can tag images and save the tag on the FILE, not in an external database. I would also need to read the tags from the file via php as well.

Why do I want to do this?

Right clicking on an image and selecting properties and then clicking on details, THEN clicking on tags and THEN adding your tags is tedious to say the least. I can't find any shortcut for adding tags quickly in windows so I want to write my own script that can do that.

Is this possible?

I don't know if it is, whenever I search for images and tags in the same line I get image tags for html tutorials or people saving tags in an external database. I can't really find any good resources, if anyone can suggest some I can look at I would be grateful.

What do I have so far?

I can list all the images on my hdd and click on an image and a popup will appear for me to enter a tag. I then send this tag to a php file waiting for me to do something with it...

Why don't I want to save the tags in an external database?

I won't be running my localhost all the time. If I copy the images the tags should go with.

Any information about this would be great.

like image 798
Chris Avatar asked Oct 28 '15 11:10

Chris


People also ask

How do I echo an image in PHP?

You cant echo an image using PHP. echo is for strings only. However, you can echo the image source - img src="" Just make sure you put the picture extension at the end of the file you are grabbing.


2 Answers

You can use

  • iptcembed to embed binary IPTC data into a JPEG image
  • exif-read-data to read the EXIF headers from JPEG or TIFF
  • How to read a Title tag from a JPEG file

    getimagesize('./phplogo.jpg', $info);
    $title = '';
    if (isset($info["APP13"])) {
       $iptc  = iptcparse ($info["APP13"]);
       $title = (isset($iptc["2#085"][0])) ? $iptc["2#085"][0] : NULL;
    }
    print $title;
    

    How to add Title tag to a JPEG
    Notes: You have only to manipulate $iptc array and to specify to file path
    this is a working example from iptcembed

    // iptc_make_tag() function by Thies C. Arntzen
    function iptc_make_tag($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;
    }
    
    // Path to jpeg file
    $path = './phplogo.jpg';
    
    // Set the IPTC tags
    $iptc = array(
        '2#085' => 'Anis TITLE'
    );
    
    // Convert the IPTC tags into binary code
    $data = '';
    
    foreach($iptc as $tag => $string)
    {
        $tag = substr($tag, 2);
        $data .= iptc_make_tag(2, $tag, $string);
    }
    
    // Embed the IPTC data
    $content = iptcembed($data, $path);
    
    // Write the new image data out to the file.
    $fp = fopen($path, "wb");
    fwrite($fp, $content);
    fclose($fp);
    

    Here the complet list of IPTC indexes

    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');
    
    like image 66
    Halayem Anis Avatar answered Oct 01 '22 05:10

    Halayem Anis


    You could use PHPExiftool to write metadata in a file as follows (quote from the github page):

    <?php
    
    require __DIR__ . '/vendor/autoload.php';
    
    use Monolog\Logger;
    use PHPExiftool\Writer;
    use PHPExiftool\Driver\Metadata\Metadata;
    use PHPExiftool\Driver\Metadata\MetadataBag;
    use PHPExiftool\Driver\Tag\IPTC\ObjectName;
    use PHPExiftool\Driver\Value\Mono;
    
    $logger = new Logger('exiftool');
    $Writer = Writer::create($logger);
    
    $bag = new MetadataBag();
    $bag->add(new Metadata(new ObjectName(), new Mono('Pretty cool subject')));
    
    $Writer->write('image.jpg', $bag);
    

    And in case you would like to keep track of the metadatas you've written you could easily use php's md5_file() function to get an identifier of the file whose metadatas you've modified and then write a line in a text file with the resulted hash followed by the metadatas you've written separated by a delimitor such as "," (comma). Each line in that text file would represent a file modified by your script.

    like image 25
    Rubén Marrero Avatar answered Oct 01 '22 06:10

    Rubén Marrero