迭代器非常适合这种类型的事情。这是 SVG 文件的基本文件
/**
* @brief SvgIterator for finding svg files
*/
class SvgIterator extends FilterIterator {
public function accept() {
$isSvg = $this->current()->getExtension() == 'svg';
if(!$isSvg) {
return false;
}
return $this->_getData();
}
/**
* @brief method for getting data from the SVG files
*
* @return boolean
*/
protected function _getData() {
$this->current()->_aspectRatio = SvgConvert::aspectRatio($this->current()->getPathname());
return true;
}
/**
* @brief calculate the aspect ratio of the curret file
*
* @return float
*/
public function aspectRatio() {
return $this->current()->_aspectRatio;
}
}
和用法:
$it = new SvgIterator(
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path)));
$return = array();
for ($it->rewind(); $it->valid(); $it->next()) {
$file = array(
'file' => $it->current()->getFilename(),
'aspect_ratio' => $it->aspectRatio()
);
$return[] = $file;
}