boxuk / describr
所以,告诉我关于你的文件... 给定任何文件,PHP库Describr将描述它
Requires
- php: >=5.3.0
- ext-fileinfo: *
Suggests
- e-butik/php-reader: Required for audio/video analysis
- zendframework/zendframework1: Allows use of advanced Audio and Video plugins
This package is auto-updated.
Last update: 2024-08-26 16:33:48 UTC
README
Describr是一个用于描述文件的面向对象库。它使用插件来确定尽可能多的关于文件的信息。
Describr可以通过提供的脚本来在命令行中运行,也可以集成到任何PHP项目中以收集有关各种文件类型的信息。您可以编写自己的插件,Describr支持的几种文件类型“开箱即用”的插件包括
- GIF
- JPEG
- PNG
- WMV
- MP4
- MP3
- AVI
- ... 以及更多!
如果您想调查的文件类型不受支持,您可以在{describr根目录}/lib/BoxUK/Describr/Plugins中添加插件,它将自动被识别并准备好使用!
需求
- PHP 5.3+
- Fileinfo扩展
插件可能有它们自己的需求。一些捆绑插件的依赖项包括
- GD扩展 - 由图像文件分析插件所需
- PHP-reader 1.8.1或更高版本 - 由音频/视频文件分析插件所需
- Zend Framework - 由PHP-Reader所需
安装
获取Describr非常简单!如果您只想使用它而不编写插件,您可以通过我们的PEAR频道安装,pear.boxuk.net(有关详细信息,请参阅我们的Box UK Labs页面),但我们假设您在这里的GitHub上,因此您想要开始并获取源代码。太好了!以下是您要这样做的方式
- 安装Zend Framework版本1.(最新)。最好通过PEAR安装,这样它就会自动添加到您的PHP路径中,否则您需要手动将其添加到路径中。
- 下载或检出PHP-reader 1.8.1或更高版本并将其添加到您的PHP包含路径中。
- 从Github获取源代码,通过克隆仓库(或下载zip文件)。
如果我不能将东西添加到我的php包含路径怎么办?
如果在步骤(1)和(2)中,您无法将Zend和PHP-Reader添加到您的PHP包含路径中,您可以执行以下操作
在Describr的根目录中,将{describr根目录}/lib/bootstrap.custom.php-sample复制到{describr根目录}/lib/bootstrap.custom.php。现在您需要编辑{describr根目录}/lib/bootstrap.custom.php并提供一个PHP-Reader的路径。您可能还需要在其中设置Zend Framework的路径。
如果您不包括Zend Framework和PHP-Reader,Describr仍然可以工作,但您将无法使用它来获取有关音频/视频文件的大量信息。
使用Describr
现在是时候测试Describr是否已安装并准备就绪了!我们提供了一个简单的命令行可执行脚本来帮助您完成此操作,因此如果您使用Linux/OSX/Unix,请使用chmod {describr根目录}/bin/describr使其可执行。在Windows上,您应该可以直接运行{describr根目录}/bin/describr.bat
gavd@gavd-desktop:/opt/BoxUK/describr$ bin/describr describr - tell me about your file... (c) 2011 Box UK Usage: describr [path to file] describr [path to file 1] [path to file 2] ... [path to file N]
如果您没有看到上面的内容,请返回到安装部分,看看是否有什么遗漏。如果没有,下面有故障排除部分
然后您应该能够做类似的事情
gavd@gavd-desktop:/opt/BoxUK/describr$ bin/describr tmp.txt Analysing tmp.txt... array(2) { ["BoxUK\General"]=> array(6) { ["errors"]=> array(0) { } ["extension"]=> string(3) "txt" ["type"]=> string(8) "document" ["mimeType"]=> string(10) "text/plain" ["fileSizeInBytes"]=> int(4) ["fileSize"]=> string(11) "Extra Small" } ["BoxUK\PlainText"]=> array(4) { ["errors"]=> array(0) { } ["lines"]=> int(1) ["characters"]=> int(4) ["words"]=> int(1) } }
所以,我们启动并运行了!除非我们没有,在这种情况下
故障排除
请检查以下内容
- 如果您使用Linux/Unix/OSX,请确保已将{describr}/bin/describr更改为可执行文件。
gavd@gavd-desktop:/opt/BoxUK/describr$ chmod u+x bin/describr
- Zend Framework 和 PHP-Reader 都在 PHP 包含路径中,或者您正在使用自定义的 bootstrap.php 文件。在后一种情况下,请检查 {describr 根目录}lib/bootstrap.custom.php 是否存在,并且 $describr_pathToPHPReaderLibrary 设置正确。它必须指向 "library" 或 "src" 目录(具体取决于您使用的 php-reader 版本)——包含 "Zend" 目录的那个目录。
- 应该安装 Zend Framework。通过 PEAR 安装可能是最干净的方法,但如果您无法使用 PEAR(例如,您在限制性共享主机上,您可以将以下内容添加到 {describr 根目录}/lib/bootstrap.custom.php
<?php // ... add the line below to the end of the file set_include_path('.:/home/you/yourProject/lib/ZendFramework-1.11.1/library');
当然,您需要调整路径以指向 Zend 的安装位置。这只是一种最后的手段,使用 PEAR 更好。
遇到任何其他问题,请在 Describr 的 Github 项目中提交一个问题,我们将尽力帮助您。请提供尽可能多的信息!
插件
Describr 是基于插件的。每个插件都有一个已知如何描述的文件类型列表。内置插件位于 {describr 根目录}/lib/BoxUK/Describr/Plugins/BoxUK。每个插件都有一个实现 Plugin 的类,主要通过扩展 AbstractPlugin 来实现。
插件依赖
您在插件的代码中指定每个插件的依赖关系。以下是一个来自 ImagePlugin.php 的示例
/** * Make sure that this plugin has everything that it needs - i.e. GD * * @throws UnmetDependencyException If a dependency is not met */ public function checkDependencies() { if (!extension_loaded('gd') || !function_exists('gd_info')) { throw new UnmetDependencyException('GD is not installed'); } }
如果插件的依赖关系未满足(即 checkDependencies() 失败),则该插件无法使用,并且当它找到可以匹配但不能报告的文件时,将静默失败并记录错误
gavd@gavd-desktop:/opt/BoxUK/describr$ bin/describr tests/resources/test.mov Analysing tests/resources/test.mov... array(2) { ["BoxUK\General"]=> *removed for brevity* ["BoxUK\AudioVideo\Iso14496Video"]=> array(1) { ["errors"]=> array(1) { [0]=> string(1236) "This plugin matched the file tests/resources/test.mov, but the dependencies could not be matched. Details: exception 'BoxUK\Describr\Plugins\UnmetDependencyException' with message 'Class Zend_Media_Iso14496 is not loaded - please ensure the php-reader library is on the include path if you wish to use this plugin' in /opt/BoxUK/describr/lib/BoxUK/Describr/Plugins/BoxUK/AudioVideo/Iso14496VideoPlugin.php:30 Stack trace: *removed for brevity* } } }
创建插件
让我们创建一个简单的插件。这个插件将仅通过计算 < 字符的数量并将其除以二来估计 XML 文件中的标签数量——当然,您可能不会在生产应用程序中这样做,但这应该有助于说明 Describr 插件!
我们创建一个文件 {describr 根目录}/lib/BoxUK/Describr/Plugins/Custom/XmlPlugin.php,内容如下
<?php namespace BoxUK\Describr\Plugins\Custom; /** * Plugin for automatically describing XML files * * @package BoxUK\Describr\Plugins\BoxUK * @author Box UK <opensource@boxuk.com> * @copyright Copyright (c) 2011, Box UK * @license https://open-source.org.cn/licenses/mit-license.php MIT License and https://gnu.ac.cn/licenses/gpl.html GPL license * @link http://github.com/boxuk/describr * @since 1.0.5 */ class XmlPlugin extends \BoxUK\Describr\Plugins\AbstractPlugin { /** * @return array Types of file this plugin can determine information about */ public function getMatchingMimeTypes() { return array( 'text/xml' ); } /** * @return array File extensions this plugin can determine information about. * The "." is not included, so "wmf" is OK, ".wmf" is not */ public function getMatchingFileExtensions() { return array( 'xml', 'xsl', ); } /** * Stub out configuration loading */ protected function loadConfiguration() {} /** * @return array With key 'tags' which is a count of tags in this document */ protected function setAttributes() { $fileContents = file_get_contents($this->fullPathToFileOnDisk); $tagOpenCount = substr_count($fileContents, '<'); $tagCloseCount = substr_count($fileContents, '</'); $tagCount = $tagOpenCount - $tagCloseCount; $this->attributes['tags'] = $tagCount; } }
请注意,我们没有在这里指定任何依赖关系——全是纯 PHP。
现在,如果我们运行测试脚本对 Describr 的 own build.xml(例如),XML 插件应该会自动使用!
gavd@gavd-desktop:/opt/BoxUK/describr$ bin/describr build.xml Analysing build.xml... array(3) { ["BoxUK\General"]=> array(5) { ["errors"]=> array(0) { } ["extension"]=> string(3) "xml" ["type"]=> NULL ["mimeType"]=> string(15) "application/xml" ["fileSizeInBytes"]=> int(1191) } ["BoxUK\PlainText"]=> array(4) { ["errors"]=> array(0) { } ["lines"]=> int(34) ["characters"]=> int(1191) ["words"]=> int(122) } ["Custom\Xml"]=> array(2) { ["errors"]=> array(0) { } ["tags"]=> int(15) } }
通过项目访问
在命令行上使用 Describr 进行入门和测试插件是很好的,但主要内容是在项目中使用它来了解文件。您可以这样做
<?php // ... $this->describr = new \BoxUK\Describr\Facade(); $responseFromDescribr = $this->describr->describeFile($pathToFile);
这将返回一个 BoxUK\Describr\MediaFileAttributes 对象,您可以通过它来调查哪个插件对文件说了什么!toArray 方法提供了我们在命令行脚本中使用的到目前为止的内容,或者您可以按插件逐个访问它以实现更精细和强大的控制。
参与其中!
如果您发现这个项目很有用,或者您发现了使用它的酷方法,请告诉我们!如果您编写了任何有用的插件,那么请提交一个 pull request,我们将查看!