PHP Conference Japan 2024

Imagick::identifyImage

(PECL imagick 2, PECL imagick 3)

Imagick::identifyImage識別影像並獲取屬性

描述

public Imagick::identifyImage(bool $appendRawOutput = false): array

識別影像並傳回屬性。屬性包括影像寬度、高度、大小和其他屬性。

參數

appendRawOutput

如果為 true,則原始輸出將附加到陣列。

傳回值

識別影像並傳回屬性。屬性包括影像寬度、高度、大小和其他屬性。

錯誤/例外

發生錯誤時,擲回 ImagickException。

範例

範例 #1 範例結果格式

Array
(
    [imageName] => /some/path/image.jpg
    [format] => JPEG (Joint Photographic Experts Group JFIF format)
    [geometry] => Array
        (
            [width] => 90
            [height] => 90
        )

    [type] => TrueColor
    [colorSpace] => RGB
    [resolution] => Array
        (
            [x] => 300
            [y] => 300
        )

    [units] => PixelsPerInch
    [fileSize] => 1.88672kb
    [compression] => JPEG
    [signature] => 9a6dc8f604f97d0d691c0286176ddf992e188f0bebba98494b2146ee2d7118da
)

新增註解

使用者貢獻的註解 3 個註解

0
rhuanpachecok at gmail dot com
4 年前
我建立了一個函式來解析「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']);

但是,如果它是 GIF,此函式傳回的「rawOutput」只包含第一個影格。或者,您可以使用命令「identify」來取得所有影格的資料

$identify = parseIdentify(shell_exec('identify -verbose example.gif'));
0
php at ontheroad dot net dot nz
14 年前
如果您使用附加原始輸出的選項,您可以從那裡擷取 mime 型別。我不確定這裡幕後發生了什麼事,但它似乎比命令列識別工具還沒用。
0
rob at OhReally dot nl
16 年前
Imagick::identifyImage() 傳回的陣列

Array
(
[imageName] => /some/path/image.jpg
[format] => JPEG (聯合影像專家組 JFIF 格式)
[geometry] => Array
(
[width] => 90
[height] => 90
)

[type] => TrueColor
[colorSpace] => RGB
[resolution] => Array
(
[x] => 300
[y] => 300
)

[units] => PixelsPerInch
[fileSize] => 1.88672kb
[compression] => JPEG
[signature] => 9a6dc8f604f97d0d691c0286176ddf992e188f0bebba98494b2146ee2d7118da
)

看起來唯一取得 mimetype 的方法是 getimagesize()…
To Top