PHP code example of magicmonkey / metasya

1. Go to this page and download the library: Download magicmonkey/metasya library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

magicmonkey / metasya example snippets


    /* import the needed class */
    use MagicMonkey\Metasya\MetadataHelper; 

    /* ith a file path as parameter */
    $metadataHelper = new MetadataHelper("photo1.jpg");  

    /* Look all medatadata of photo1.jpg */
    var_dump($metadataHelper->read());
    

$metadataHelper = new MetadataHelper("data/images/photo1.jpg");

/* First way via the constructor : passing as SECOND parameter the boolean false which indicates to not use the provided exiftool */

$metadataHelper = new MetadataHelper("data/images/photo1.jpg", false);

/* Second way via the setter : set the attribute named "useProvidedExiftool" to false */

$metadataHelper = new MetadataHelper("data/images/photo1.jpg");
$metadataHelper->setUseProvidedExiftool(false);

/* First way via the constructor : passing as THIRD parameter the boolean false which indicates to not display errors */

$metadataHelper = new MetadataHelper("data/images/photo1.jpg", true, false);

/* Second way via the setter : set the attribute named "displayErros" to false */

$metadataHelper = new MetadataHelper("data/images/photo1.jpg");
$metadataHelper->setDisplayErrors(false);

/* Get the version of the exiftool installed on your computer else return null */
echo $metadataHelper->getLocalExiftoolVersion();

/* Get the version of the provided exiftool by Metasya */
echo $metadataHelper->getProvidedExiftoolVersion();

/* Return an array which indicates if Metasya uses the local or the provided exiftool and the version of the used exiftool */
var_dump($metadataHelper->getUsedExiftoolVersion());

/* example : 
array (size=1)
  'Provided' => string '10.67' (length=6)
*/

/* return an array which contains 3 information above */
var_dump($metadataHelper->getExiftoolVersionsInfo());

/* example : 

array (size=3)
  'Local' => null     ----> exiftool not installed ...
  'Provided' => string '10.67' (length=6)
  'Used' => 
  	array (size=1)
  	  'Provided' => string '10.67' (length=6)
  	  
*/

$metadataHelper->setFilePath("data/images/photo2.jpg");

/* Print all meta information in an image, including duplicate and unknown tags, sorted by group (for family 1). */
 
var_dump($metadataHelper->execute("-a -u -g1 image.jpg"));

$metadataHelper->generateXMPSideCar();

$metadataHelper->generateXMPSideCar("an/other/path");

        $metadata = $metadataHelper->reader()->read();
            
        /* or the short way */
            
        $metadata = $metadataHelper->read();
        

         $metadata = $metadataHelper->reader()->read(["XMP-dc:all"], ["XMP-dc:Subject"]);

         /* or the short way */

         $metadata = $metadataHelper->read(["XMP-dc:all"], ["XMP-dc:Subject"]);

         /* Result :
             
         array (size=5)
           'SourceFile' => string 'data/images/photo1.jpg' (length=22)
           'Rights' => string 'CC-by-sa' (length=8)
           'Description' => string 'Western part of the abandoned Packard Automotive Plant in 		Detroit, Michigan.' (length=76)
            'Creator' => string 'Albert Duce' (length=11)
            'Title' => string 'Abandoned Packard Automobile Factory, Detroit' (length=45)
             
         */
         

        $metadata = $metadataHelper->reader()->read(["all"], ["XMP-photoshop:all", "XMP-xmpRights:all"]);
            
        /* or the short way */
            
        $metadata = $metadataHelper->read(["all"], ["XMP-photoshop:all", "XMP-xmpRights:all"]);
        

            $metadata = $metadataHelper->reader()->readByGroup(["all"], 1);
                
            /* or the short way */
                
            $metadata = $metadataHelper->readByGroup(["all"], 1);
            

            $metadata = $metadataHelper->reader()->readByGroup(["XMP-dc:all"], 1, ["XMP-dc:Subject"]);

            /* or the short way */

            $metadata = $metadataHelper->readByGroup(["XMP-dc:all"], 1, ["XMP-dc:Subject"]);

            /* Result :

            array (size=2)
              'SourceFile' => string 'data/images/photo1.jpg' (length=22)
              'XMP-dc' => 
                array (size=4)
                  'Rights' => string 'CC-by-sa' (length=8)
                  'Description' => string 'Western part of the abandoned Packard Automotive Plant in 		Detroit, Michigan.' (length=76)
                  'Creator' => string 'Albert Duce' (length=11)
                  'Title' => string 'Abandoned Packard Automobile Factory, Detroit' (length=45)
                  
            */
            

            $metadata = $metadataHelper->reader()->readWithPrefix();
             
             /* or the short way */
             
             $metadata = $metadataHelper->readWithPrefix();
            

             $metadata = $metadataHelper->reader()->readWithPrefix(["XMP-dc:all"], 1, ["XMP-dc:Subject"]);

             /* or the short way */

             $metadata = $metadataHelper->readWithPrefix(["XMP-dc:all"], 1, ["XMP-dc:Subject"]);

             /* Result :
                
             array (size=5)
               'SourceFile' => string 'data/images/photo1.jpg' (length=22)
               'XMP-dc:Rights' => string 'CC-by-sa' (length=8)
               'XMP-dc:Description' => string 'Western part of the abandoned Packard Automotive Plant 	in Detroit, Michigan.' (length=76)
               'XMP-dc:Creator' => string 'Albert Duce' (length=11)
               'XMP-dc:Title' => string 'Abandoned Packard Automobile Factory, Detroit' (length=45)

             */
             

    $metadataHelper->writer()->write(["XMP-dc:Title" => "Blue Bird", "XMP-dc:Description" => "My song of the year"]);

    /* or the short way */

    $metadataHelper->write(["XMP-dc:Title" => "Blue Bird", "XMP-dc:Description" => "My song of the year"]);

    /* Result :

    	:string '1 image files updated' (length=21)

    */
    

     $metadataHelper->writer()->write(["XMP-dc:Title" => "First Title"], false);

     /* or the short way */

     $metadataHelper->write(["XMP-dc:Title" => "First Title"], false);
     

        $metadataHelper->writer()->writeFromJsonFile("../path/to/data.json");

        /* or the short way */

        $metadataHelper->writeFromJsonFile("../path/to/data.json");

        /* data.json :

          [{"SourceFile": "data/images/photo1.jpg",    <-- same value as $filePath
            "XMP-dc:Title": "Le titre de mon image",
            "XMP-dc:Rights": "CC-by-nc-sa",
            "XMP-dc:Description": "This is a test",
            "XMP-dc:Description-en-EN": "This is a test"
          }]

        */

        /* Result :

        	:string '1 image files updated' (length=21)

        */
        

        $metadataHelper->writer()->writeFromJson('
              [{"SourceFile": "data/images/photo1.jpg",
              "XMP-dc:Title": "Le titre de mon image",
              "XMP-dc:Rights": "CC-by-nc-sa",
              "XMP-dc:Description": "This is a test",
              "XMP-dc:Description-en-EN": "This is a test"
              }]
        ');

        /* or the short way */

        $metadataHelper->writeFromJson('
                              [{"SourceFile": "data/images/photo1.jpg",
                              "XMP-dc:Title": "Le titre de mon image",
                              "XMP-dc:Rights": "CC-by-nc-sa",
                              "XMP-dc:Description": "This is a test",
                              "XMP-dc:Description-en-EN": "This is a test"
                              }]
                        ');

        /* Result :

        	:string '1 image files updated' (length=21)
            
        */
        

        $metadataHelper->eraser()->remove(["all"]);

        /* or the short way */

        $metadataHelper->remove(["all"]);

        /* Result :

        	:string '1 image files updated' (length=21)
          
        */
        

        $metadataHelper->eraser()->remove(["XMP-dc:all"], ["XMP-dc:Title"]);

        /* or the short way */

        $metadataHelper->remove(["XMP-dc:all"], ["XMP-dc:Title"]);

        /* Result :

        	:string '1 image files updated' (length=21)

        */
        

// 1.  all metadata of the schema "cosmos" and the meadata Title with the namespace XMP-dc will be returned (if they exist) :

  // shortcut way
  $metadataHelper->read(["cosmos", "XMP-dc:title"]);

  // schema object way
  $metadataHelper->read([$cosmosSchemaObject, "XMP-dc:title"]);


// 2. Only the description metadata from the cosmos schema and the meadata Title with the namespace XMP-dc will be returned (if they exist) :

  // metadata shortcut way
  $metadataHelper->read(["dublinDesc", "XMP-dc:title"]);

  // metadata object way
  $metadataHelper->read([$descriptionMetadata, "XMP-dc:title"]);

// dublinDesc and dublinTitle are shortcut, rights is not a shortcut
$metadataHelper->write(["dublinDesc" => "new description", "dublinTitle" => "new title", "rights" => "new rights"]));

// 1. remove all metadata except metadata of the schema "cosmos" :
  
$metadataHelper->remove(["all"], ["cosmos"]);

// 2. remove metadata of the schema "cosmos" and the metadata rights except the description metadata targeted via its cosmos shortcut "dublinDesc" :

$metadataHelper->remove(["cosmos", "rights"], ["dublinDesc"]);

$schemataManager = SchemataManager::getInstance();

$metadataHelper->getSchemataManager();

$metadataHelper->getSchemataManager()->getSchemata();

/* Result :

array (size=2)
  0 => 
    object(MagicMonkey\Metasya\Schema\Schema)[4]
      private 'fileName' => string 'xmp-test-schema.json' (length=20)
      private 'shortcut' => string 'USER-xmp' (length=8)
      private 'isValid' => boolean false
      private 'errors' => 
        array (size=1)
          0 => string 'Schema's metadata list is missing or is not an array.' (length=53)
      private 'description' => string '' (length=0)
      private 'metadata' => 
        array (size=0)
          empty
      private 'schemaAsArray' => 
        array (size=4)
          'shortcut' => string 'USER-xmp' (length=8)
          'description' => string '' (length=0)
          'namespace' => string 'XMP-dc' (length=6)
          'properties' => 
            array (size=2)
              ...

*/

$metadataHelper->getSchemataManager()->getValidSchemata();

$metadataHelper->getSchemataManager()->setUserSchemataFolderPath("my/new/path");

$metadataHelper->getSchemataManager()->setUserSchemataFolderPath("my/new/path", true);

$metadataHelper->getSchemataManager()->getUsersSchemataFolder();

$metadataHelper->getSchemataManager()->isSchemaShortcut("a-shortcut");

$metadataHelper->getSchemataManager()->getSchemaFromShortcut("a-shortcut");

$metadataHelper->getSchemataManager()->isMetadataShortcut("a-shortcut");

$metadataHelper->getSchemataManager()->getMetadataFromShortcut("a-shortcut");

$metadataHelper->getSchemataManager()->checkSchemataState();

/* Result :

  array (size=2)
    'USER-xmp' => 
      array (size=1)
        0 => string 'Schema's metadata list is missing or is not an array.'
        (length=53)
    'cosmos' => string 'valid' (length=5)
    
*/

$title = new Metadata("Title", "XMP-dc", "ti");
$creator = new Metadata("Creator", "XMP-dc", "crea", "creator description");
$description = new Metadata("Description", "XMP-dc", "desc");
$sizeProperty = new Metadata("FileSize", "System", "fs", null, new MetaTypeString());

/* For this example, we will use the metadata created in the last one */
$mySchemaObject = new Schema("super-xmp", "Schema to get some metadata");
$mySchemaObject->addMetadata($title);
$mySchemaObject->addMetadata($creator);
$mySchemaObject->addMetadata($description);
$mySchemaObject->addMetadata($size);

$metadataHelper->read([$mySchemaObject]);
/*
thus Exiftool will search for the following metadata : 
	=> XMP-dc:Title, XMP-dc:Creator, XMP-dc:Description, System:FileSize 
*/

var_dump($mySchemaObject->getMetadata());

/* result :
array (size=4)
  0 => 
    object(MagicMonkey\Metasya\Schema\Metadata)[7]
      private 'tagName' => string 'Title' (length=5)
      private 'nameSpace' => null
      private 'value' => null
  1 => 
    object(MagicMonkey\Metasya\Schema\Metadata)[8]
      private 'tagName' => string 'Creator' (length=7)
      private 'nameSpace' => string '' (length=0)
      private 'value' => string 'Mr nobody' (length=9)
  2 => 
    object(MagicMonkey\Metasya\Schema\Metadata)[9]
      private 'tagName' => string 'Description' (length=11)
      private 'nameSpace' => null
      private 'value' => null
  3 => 
    object(MagicMonkey\Metasya\Schema\Metadata)[10]
      private 'tagName' => string 'FileSize' (length=8)
      private 'nameSpace' => string 'System' (length=6)
      private 'value' => null
*/

var_dump($mySchemaObject->buildTargetedMetadata());

/* result :
array (size=4)
  0 => string 'XMP-dc:Title' (length=12)
  1 => string 'XMP-dc:Creator' (length=17)
  2 => string 'XMP-dc:Description' (length=18)
  3 => string 'System:FileSize' (length=15)
*/

$mySchemaObject->addMetadata(new Metadata("Title", "XMP-dc", "t-shortcut"));
$mySchemaObject->removeMetadata($creator);
/* or with the index */
$mySchemaObject->removeMetadata(0);

$mySchemaObject->isMetadataShortcut("a-shortcut");

$mySchemaObject->getMetadataFromShortcut("a-shortcut");