magicmonkey / metasya
一个库,允许管理不同类型文件上嵌入的元数据,以管理信息系统中元数据的导入以及与exiftool同步信息系统和文件之间的数据。
Requires
- php: >=5.6.0
This package is not auto-updated.
Last update: 2024-09-29 05:14:45 UTC
README
Metasya是一个库,允许管理不同类型文件上嵌入的元数据,以管理信息系统中元数据的导入以及与exiftool同步信息系统和文件之间的数据。
什么是Exiftool?请查看这里: https://www.sno.phy.queensu.ca/~phil/exiftool/
[目录]
安装
- 您必须使用Composer,PHP的依赖管理工具
composer require magicmonkey/metasya
Metasya在Packagist上注册为包:https://packagist.org.cn/packages/magicmonkey/metasya
-
要激活自动加载器,您可能需要在命令行中输入以下命令
composer dumpautoload -o
-
基于UNIX的操作系统中,Metasya提供的Exiftool版本至少必须具有所有者的执行权限
chmod 500 vendor/magicmonkey/metasya/exiftool/unix/exiftool
-
您可以在index.php文件中编写代码,以测试您刚刚下载的Metasya是否真的工作
/* import the needed class */ use MagicMonkey\Metasya\MetadataHelper; /* include the composer autoloader */ include __DIR__ . "/vendor/autoload.php"; /* Create a MetadataHelper Object with a file path as parameter */ $metadataHelper = new MetadataHelper("photo1.jpg"); /* Look all medatadata of photo1.jpg */ var_dump($metadataHelper->read());
用法:这里是!
MetadataHelper
MetadataHelper是Metasya的主要类。
创建对象
要管理文件的元数据,您必须使用文件的路径创建一个新的MetadataHelper对象。
$metadataHelper = new MetadataHelper("data/images/photo1.jpg");
使用您计算机上安装的exiftool版本
默认情况下,Metasya使用提供的exiftool。但是,您可以使用两种不同的方式使用您计算机上安装的版本
/* 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);
显示错误
默认情况下,Metasya显示Exiftool工具返回的错误。但是,您可以选择不显示它
/* 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);
版本信息
有各种函数可用于获取有关exiftool版本的信息。
/* 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");
执行自己的exiftool命令行
下一部分是关于任务者,它允许您通过预定义的命令管理文件元数据。但是,您可以使用“execute”函数执行特定操作,而不管将文件路径作为参数传递给元数据Helper对象。
/* 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();
请注意,默认情况下生成的侧车文件输出路径是名为“metasya/Sidecar”的文件夹,它位于项目的根目录中。但是,您可以指定其他路径,如下所示
$metadataHelper->generateXMPSideCar("an/other/path");
任务者的概念
MetadataHelper对象具有多个任务者。每个任务者都通过使用exiftool提供功能。
ReaderTasker
ReaderTasker允许读取文件的元数据。您可以使用以下3个功能:
read ($selectedMetadata, $excludedMetadata)
- 描述:允许读取所有或某些文件的元数据,无需使用exiftool组选项。
- 参数
- $selectedMetadata:数组(默认:null):指示要读取的元数据。
- $excludedMetadata : 数组 (默认:null) :表示您不想读取的元数据。
- 返回:数组 | 字符串
- 示例
-
读取所有元数据
$metadata = $metadataHelper->reader()->read(); /* or the short way */ $metadata = $metadataHelper->read();
-
读取所有XMP Dublin Core元数据,除了XMP Dublin Core主题
$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) */
-
读取所有元数据,除了XMP Photoshop和XMP Rights元数据
$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"]);
-
readByGroup ($selectedMetadata, $num, $excludedMetadata)
- 描述:允许使用带有-g[$num...]选项的组选项读取所有或某些文件的元数据,该选项按标签组组织输出。
- 参数:* $selectedMetadata : 数组 (默认:null) :表示您想读取的元数据。* $num : 整数 (默认:0) :表示组级别。* $excludedMetadata : 数组 (默认:null) :表示您不想读取的元数据。
- 返回:数组 | 字符串
- 示例
-
以组级别1读取所有元数据
$metadata = $metadataHelper->reader()->readByGroup(["all"], 1); /* or the short way */ $metadata = $metadataHelper->readByGroup(["all"], 1);
-
以组级别1读取所有XMP Dublin Core元数据,除了XMP Dublin Core主题
$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) */
-
readWithPrefix ($selectedMetadata, $num, $excludedMetadata)
- 描述:允许使用带有-G[$num...]选项的组选项读取所有或某些文件的元数据,该选项在每组标签前打印组名。
- 参数:* $selectedMetadata : 数组 (默认:null) :表示您想读取的元数据。* $num : 整数 (默认:0) :表示组级别。* $excludedMetadata : 数组 (默认:null) :表示您不想读取的元数据。
-
返回:数组 | 字符串
-
示例
-
读取所有元数据
$metadata = $metadataHelper->reader()->readWithPrefix(); /* or the short way */ $metadata = $metadataHelper->readWithPrefix();
-
以组级别1读取所有XMP Dublin Core元数据,除了XMP Dublin Core主题
$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) */
-
-
WriterTasker
The WriterTasker允许向文件添加元数据或编辑文件的元数据。您可以使用以下3个功能:
write ($targetedMetadata, $replace, $overwrite)
-
描述:允许添加或编辑文件的某些元数据。
-
参数
- $targetedMetadata (默认:null) :表示您想添加或编辑的元数据。
- $replace (默认:true) :表示如果元数据已存在,是否必须替换元数据值。
- $overwrite (默认:true) :表示添加或修改是否应用于原始文件或副本。它对应于exiftool选项-overwrite_original的使用。
-
返回:字符串 | null
-
示例
-
写入一些XMP Dublin Core元数据
$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) */
-
仅当XMP Dublin Core标题不存在时写入XMP Dublin Core标题
$metadataHelper->writer()->write(["XMP-dc:Title" => "First Title"], false); /* or the short way */ $metadataHelper->write(["XMP-dc:Title" => "First Title"], false);
-
writeFromJsonFile ($jsonFilePath, $replace, $overwrite)
-
描述:与write功能相同,但来自json文件。
-
WARNING :请注意,json文件内的json必须包含元数据标签"SourceFile",其值为MetadataHelper对象使用的文件路径。
-
参数
- $jsonFilePath (默认:null) :表示包含要使用的元数据标签的json文件的路径。
- $replace (默认:true) :表示如果元数据已存在,是否必须替换元数据值。
- $overwrite (默认:true) :表示添加或修改是否应用于原始文件或副本。它对应于exiftool选项-overwrite_original的使用。
-
返回:字符串 | null
-
示例
-
从json文件写入元数据
$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) */
-
writeFromJson ($json, $replace, $overwrite)
-
描述:与write功能相同,但来自json字符串。
-
WARNING :请注意,json字符串必须包含元数据标签"SourceFile",其值为MetadataHelper对象使用的文件路径。
-
参数
- $json :表示包含要使用的元数据标签的json字符串。
- $replace (默认:true) :表示如果元数据已存在,是否必须替换元数据值。
- $overwrite (默认:true) :表示添加或修改是否应用于原始文件或副本。它对应于exiftool选项-overwrite_original的使用。
-
返回:字符串 | null
-
示例
-
从json文件写入元数据
$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) */
-
EraserTasker
The EraserTasker允许删除文件的元数据。目前只有一个功能可用
remove ($targetedMetadata, $excludedMetadata, $overwrite)
-
描述:允许删除所有或某些文件的元数据。
-
参数
- $targetedMetadata (默认:"all") :表示您想删除的元数据。可以是
或 。 - $excludedMetadata (默认:null) :表示您不想删除的元数据。可以是
或 。 - $overwrite (默认:true) :表示删除是否应用于原始文件或副本。它对应于exiftool选项-overwrite_original的使用。
- $targetedMetadata (默认:"all") :表示您想删除的元数据。可以是
-
返回:字符串 | null
-
示例
-
删除所有元数据
$metadataHelper->eraser()->remove(["all"]); /* or the short way */ $metadataHelper->remove(["all"]); /* Result : :string '1 image files updated' (length=21) */
-
删除所有XMP Dublin Core元数据,除了XMP Dublin Core标题
$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) */
-
方案体系
Metasya提供了一套方案体系,以便轻松管理文件的元数据。
-
什么是方案? 方案可以是一个JSON文件和/或一个包含如下信息(如方案的快捷方式、元数据属性、命名空间、描述等)的对象。方案有两种类型:Metasya指定的默认方案和用户创建的方案。
-
这个体系的用途是什么? 使用这个体系,你可以使用多个方案来读取、写入或删除大量元数据。这个体系可以节省时间:你只需写一个词(方案的快捷方式)而不是读取所有元数据的列表。
如何添加自定义的JSON文件方案
请注意,你有创建自己的方案并将其存储在所需文件夹中的可能性。因此,在一个项目中创建的方案也可以用在另一个项目中!
! 注意:个人方案的名字必须以 "-schema.json" 结尾。有效名称示例:cosmos-schema.json。
有效的JSON文件方案示例
{ "shortcut": "cosmos", "description": "Schema to get some metadata", "metadata": [ { "namespace": "XMP-dc", "list": { "Title": { "shortcut": "dublinTitle" }, "Creator": { "shortcut": "dublinAuthor", "description":"", "type":"String" // corresponds to MetaTypeString class }, "Description": { "shortcut": "dubinDesc" } } }, { "namespace": "System", "list": { "FileSize": { "shortcut": "fs" } } } ] }
结构与规则描述
为了创建有效的JSON文件方案,请遵守以下规则。请注意,如果JSON文件无效,则将创建方案对象,但它将不可用。
元数据类型
默认的元数据类型是MetaTypeAny类的一个实例。这个类接受任何类型的值。通过指定元数据类型,在添加新值之前会检查该值。如果该值不接受,则不会添加。
元数据类型列表
如何创建自己的元数据类型
如何使用方案与任务器
请注意,在以下所有示例中,将使用上面提到的名为“cosmos”的方案示例。
如何读取
你可以通过传递快捷方式或方案对象来读取所有方案元数据。你也可以用同样的方式读取方案的一些元数据。
让我们看看一个例子
// 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"]);
方案管理器
首先,你需要知道方案管理器是一个单例:这意味着只能创建一个此类实例。 它是如何工作的? 方案管理器会自动将用户和默认方案目录中的所有JSON文件转换为方案对象。然后,这些方案对象将被添加到方案管理器的方案列表中。
你可以如下获取这个管理器
$schemataManager = SchemataManager::getInstance();
或者直接通过MetadataHelper类
$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();
用户方案文件夹的路径
默认情况下,用户方案文件夹名为 "metasya/Schemata",并创建在项目的根目录中。然而,你可以按照以下方式更改它
$metadataHelper->getSchemataManager()->setUserSchemataFolderPath("my/new/path");
如果旧的文件夹 "metasyaSchemata" 包含作为方案的JSON文件,则所有这些文件都将复制到新文件夹中。接下来,如果你想删除文件夹 "metasyaSchemata" 及其内容,你可以手动(安全且安全)删除它,或者你可以让Metasya自动删除它。确实,你可以通过将布尔值设置为参数 "true" 来通知,在复制后删除旧文件夹及其内容
$metadataHelper->getSchemataManager()->setUserSchemataFolderPath("my/new/path", true);
你可以像这样获取用户方案文件夹的信息
$metadataHelper->getSchemataManager()->getUsersSchemataFolder();
测试一个字符串是否是方案快捷方式
您可以使用函数isSchemaShortcut()来测试字符串是否与模式相关联。该函数根据给定的快捷键值返回true或false。
$metadataHelper->getSchemataManager()->isSchemaShortcut("a-shortcut");
从快捷键获取模式对象
您可以使用函数getSchemaFromShortcut()获取模式对象。
$metadataHelper->getSchemataManager()->getSchemaFromShortcut("a-shortcut");
测试字符串是否是元数据的快捷键
您可以使用函数isMetadataShortcut()来测试字符串是否与元数据相关联。该函数根据给定的快捷键值返回true或false。注意,只测试来自有效模式的元数据。
$metadataHelper->getSchemataManager()->isMetadataShortcut("a-shortcut");
从快捷键获取元数据对象
您可以使用函数getMetadataFromShortcut()获取元数据对象。注意,只测试来自有效模式的元数据。
$metadataHelper->getSchemataManager()->getMetadataFromShortcut("a-shortcut");
检查模式的状态
检查模式的状态可能很有用,以便了解可能出现的错误。在下面的示例中,第一个由“USER-xmp”标识的模式无效,而第二个由“cosmos”标识的模式有效。
$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) */
Metadata类
Metadata对象对应于元数据标签。您可以根据标签名称、命名空间、快捷键、描述和类型(按此顺序)创建Metadata。让我们看一个例子。
$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());
请注意,默认情况下,Metadata的类型将是MetaTypeAny,而Metadata的描述为null。
Schema类
Schema对象主要由快捷键、描述和元数据列表组成。快捷键允许引用模式。
/* 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);
测试字符串是否是模式元数据的快捷键
您可以使用函数isMetadataShortcut()测试字符串是否与模式中的元数据相关联。该函数根据给定的快捷键值返回true或false。注意,模式必须是有效的。
$mySchemaObject->isMetadataShortcut("a-shortcut");
从快捷键获取元数据对象
您可以使用函数getMetadataFromShortcut()获取模式中的元数据对象。注意,模式必须是有效的。
$mySchemaObject->getMetadataFromShortcut("a-shortcut");
默认模式列表
即将推出...