我创建了一个函数来解析“rawOutput”,因为此函数提供的的信息相当无用。
function parseIdentify($info) {
$lines = explode("\n", $info);
$outputs = [];
$output = [];
$keys = [];
$currSpaces = 0;
$raw = false;
foreach($lines as $line) {
$trimLine = trim($line);
if(empty($trimLine)) continue;
if($raw) {
preg_match('/^[0-9]+:\s/', $trimLine, $match);
if(!empty($match)) {
$regex = '/([\d]+):';
$regex .= '\s(\([\d|\s]{1,3},[\d|\s]{1,3},[\d|\s]{1,3},[\d|\s]{1,3}\))';
$regex .= '\s(#\w+)';
$regex .= '\s(srgba\([\d|\s]{1,3},[\d|\s]{1,3},[\d|\s]{1,3},[\d|\s|.]+\)|\w+)/';
preg_match($regex, $trimLine, $matches);
array_shift($matches);
$output['Image'][$raw][] = $matches;
continue;
}
else {
$raw = false;
array_pop($keys);
}
}
preg_match('/^\s+/', $line, $match);
$spaces = isset($match[0]) ? strlen($match[0]) : $spaces = 0;
$parts = preg_split('/:\s/', $trimLine, 2);
$_key = ucwords($parts[0]);
$_key = str_replace(' ', '', $_key);
$_val = isset($parts[1]) ? $parts[1] : [];
if($_key == 'Image') {
if(!empty($output)) {
$outputs[] = $output['Image'];
$output = [];
}
$_val = [];
}
if($spaces < $currSpaces) {
for($i = 0; $i < ($currSpaces - $spaces) / 2; $i++) {
array_pop($keys);
}
}
if(is_array($_val)) {
$_key = rtrim($_key, ':');
$keys[] = $_key;
if($_key == 'Histogram' || $_key == 'Colormap') {
$raw = $_key;
}
}
$currSpaces = $spaces;
$arr = &$output;
foreach($keys as $key) {
if(!isset($arr[$key])) {
$arr[$key] = $_val;
}
$arr = &$arr[$key];
}
if(!is_array($_val)) {
$arr[$_key] = $_val;
}
}
$outputs[] = $output['Image'];
return count($outputs) > 1 ? $outputs : $outputs[0];
}
用法示例
$img = new Imagick('example.png');
$identify = parseIdentify($img->identifyImage(true)['rawOutput']);
但此函数返回的“rawOutput”仅包含第一个帧(如果它是 GIF)。或者,您可以使用“identify”命令来获取所有帧的数据
$identify = parseIdentify(shell_exec('identify -verbose example.gif'));