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)
*/
/* 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"));
$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"]);
$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
*/
$mySchemaObject->addMetadata(new Metadata("Title", "XMP-dc", "t-shortcut"));
$mySchemaObject->removeMetadata($creator);
/* or with the index */
$mySchemaObject->removeMetadata(0);