這也會擷取多頁文件(即 PDF、PS)檔案的 Imagick 實例目前所在的頁碼。對於 PDF,預設似乎設定為最後一頁。
(PECL imagick 2, PECL imagick 3)
Imagick::getIteratorIndex — 取得目前活動影像的索引
傳回 Imagick 物件中目前活動影像的索引。如果 Imagick 是針對 ImageMagick 6.2.9 或更新版本編譯的,則此方法可用。
此函式沒有參數。
傳回一個整數,其中包含堆疊中影像的索引。
發生錯誤時,拋出 ImagickException。
範例 1 使用 Imagick::getIteratorIndex()
建立影像、設定和取得迭代器索引
<?php
$im = new Imagick();
$im->newImage(100, 100, new ImagickPixel("red"));
$im->newImage(100, 100, new ImagickPixel("green"));
$im->newImage(100, 100, new ImagickPixel("blue"));
$im->setIteratorIndex(1);
echo $im->getIteratorIndex();
?>
ImageMagick 函式的 getIteratorIndex 函式適用於任何類型的影像,但它是為了測量動畫 .gif 檔案中的影格數而建置的。對於非動畫影像檔案(如一般 .bmp 或 .jpg 檔案),此函式將永遠傳回數字「0」,表示只有一個影格。此函式的計數從零開始,因此具有五個不同影格的 .gif 檔案將傳回值「4」。高度重複但迷幻的動畫 GIF 通常在 10 到 30 個影格之間,但只不過是轉換為資料檔案的影片片段的 .gif 檔案可能有數百個影格。
根據維基百科(在「Graphics Interchange Format」的文章中),動畫 GIF 影像使用的是 GIF 標準,「該標準允許在檔案中繪製具有時間延遲的各種影像(影格)。」此函式不會為您取得每個影格之間延遲的時間量,但它會給您 .gif 檔案中唯一影格的數量。這將告訴您動畫的複雜程度或簡單程度。
「getIteratorIndex」函式對您不起作用嗎?請嘗試使用「getImageIndex」函式,它會產生完全相同的結果。
一些範例程式碼
<?php
// 作者:holdoffhunger@gmail.com
// Imagick 型別
// ---------------------------------------------
$imagick_type = new Imagick();
// 開啟檔案
// ---------------------------------------------
$file_to_grab = "image_workshop_directory/test.gif";
$file_handle_for_viewing_image_file = fopen($file_to_grab, 'a+');
// 抓取檔案
// ---------------------------------------------
$imagick_type->readImageFile($file_handle_for_viewing_image_file);
// 取得影像型別值
// ---------------------------------------------
$image_iterator_index = $imagick_type->getIteratorIndex();
// 列印影像型別值
// ---------------------------------------------
print(" .GIF 檔案中唯一影格的數量: $image_iterator_index");
?>