PHP Conference Japan 2024

imagesetinterpolation

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

imagesetinterpolation設定插值方法

說明

imagesetinterpolation(GdImage $image, int $method = IMG_BILINEAR_FIXED): bool

設定插值方法,設定插值方法會影響 GD 中各種函式的渲染,例如 imagerotate() 函式。

參數

image

一個 GdImage 物件,由影像建立函式之一返回,例如 imagecreatetruecolor()

method

插值方法,可以是以下其中之一

返回值

成功時返回 true,失敗時返回 false

更新日誌

版本 說明
8.0.0 image 現在需要一個 GdImage 實例;以前,需要一個有效的 gd 資源

範例

範例 #1 imagesetinterpolation() 範例

<?php
// 載入圖片
$im = imagecreate(500, 500);

// 預設插值法為 IMG_BILINEAR_FIXED,切換
// 使用 'Mitchell' 濾波器:
imagesetinterpolation($im, IMG_MITCHELL);

// 繼續使用 $im ...
?>

注意事項

更改插值方法會在渲染時影響以下函數

參見

新增筆記

使用者貢獻的筆記 1 則筆記

-1
shaun at slickdesign dot com dot au
7 年前
設定內插法並不會套用到 imageaffine() 或 imagerotate() 建立的任何影像。它預設為 IMG_BILINEAR_FIXED,需要根據需要在每個生成的影像上設定。

<?php
imagesetinterpolation
( $image, IMG_NEAREST_NEIGHBOUR );

// 使用 IMG_NEAREST_NEIGHBOUR 旋轉
$rotated = imagerotate( $image, 45, $transparent );

// 使用 IMG_BILINEAR_FIXED 旋轉
$rotated_again = imagerotate( $rotated, 45, $transparent );
?>

將內插法設定為 IMG_NEAREST_NEIGHBOUR 有助於在以 90 度增量旋轉影像時保留細節並防止取樣問題,包括順時針旋轉時。

<?php
// 旋轉後的影像可能看起來模糊且略微傾斜。
$rotated = imagerotate( $image, -360, $transparent );

// 與原始影像相似,但仍可能顯示背景或略微傾斜。
imagesetinterpolation( $image, IMG_NEAREST_NEIGHBOUR );
$rotated = imagerotate( $image, -360, $transparent );
?>
To Top