2024 年日本 PHP 研討會

imagewbmp

(PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8)

imagewbmp將影像輸出到瀏覽器或檔案

說明

imagewbmp(GdImage $image, 資源|字串|null $file = null, ?int $foreground_color = null): bool

imagewbmp() 函式會輸出或儲存給定 imageWBMP 版本。

參數

image

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

file

儲存檔案的路徑或開啟的串流資源(此函式返回後會自動關閉)。如果未設定或為 null,則會直接輸出原始影像串流。

foreground_color

您可以透過設定從 imagecolorallocate() 取得的識別碼,使用此參數設定前景顏色。預設前景顏色為黑色。

返回值

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

注意

然而,如果 libgd 無法輸出影像,此函式會返回 true

更新日誌

版本 說明
8.0.0 image 現在需要 GdImage 實例;先前需要有效的 gd 資源
8.0.0 foreground_color 現在可以為 null。

範例

範例 #1 輸出 WBMP 影像

<?php
// 建立一張空白圖片並加入文字
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, '一段簡單的文字字串', $text_color);

// 設定內容類型標頭 - 在此範例中為 image/vnd.wap.wbmp
// 提示:請參考 image_type_to_mime_type() 以取得內容類型
header('Content-Type: image/vnd.wap.wbmp');

// 輸出圖片
imagewbmp($im);

// 釋放記憶體
imagedestroy($im);
?>

範例 #2 儲存 WBMP 圖片

<?php
// 建立一張空白圖片並加入文字
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, '一段簡單的文字字串', $text_color);

// 儲存圖片
imagewbmp($im, 'simpletext.wbmp');

// 釋放記憶體
imagedestroy($im);
?>

範例 #3 使用不同的前景顏色輸出圖片

<?php
// 建立空白影像並新增文字
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, '一段簡單的文字字串', $text_color);

// 設定內容類型標頭 - 此例為 image/vnd.wap.wbmp
// 提示:請參閱 image_type_to_mime_type() 以了解內容類型
header('Content-Type: image/vnd.wap.wbmp');

// 設定替代前景顏色
$foreground_color = imagecolorallocate($im, 255, 0, 0);

imagewbmp($im, NULL, $foreground_color);

// 釋放記憶體
imagedestroy($im);
?>

另請參閱

新增註釋

使用者貢獻的註釋 2 則註釋

2
andre at bynetworld dot com
16 年前
在先前的註釋中,-> lukeross at sys3175 dot co dot uk <- 的 for() 迴圈中有一個程式碼錯誤。更正錯誤後,我發現此函式在將彩色影像轉換為 2 色方面做得很好。
以下是修正後的函式程式碼

function ImageColorFloydSteinberg($dst_img, $src_img) {
ImageColorAllocate($dst_img, 0, 0, 0); // 配置黑色
ImageColorAllocate($dst_img, 255, 255, 255); // 配置白色
$isx = ImageSX($src_img); // 取得來源圖片寬度
$isy = ImageSY($src_img); // 取得來源圖片高度
$grey_img = ImageCreate($isx, $isy); // 建立灰階圖片
for ($a = 0; $a <= 255; $a++) ImageColorAllocate($grey_img, $a, $a, $a); // 配置灰階色盤
$isx -= 1; // 來源圖片寬度像素索引從 0 開始,因此需減 1 修正
$isy -= 1; // 來源圖片高度像素索引從 0 開始,因此需減 1 修正
for($x = 0; $x <= $isx; $x++) { // 迴圈處理每一列像素
for($y = 0; $y <= $isy; $y++) { // 迴圈處理每一行像素
$color = ImageColorsForIndex($src_img, ImageColorAt($src_img, $x, $y)); // 取得來源圖片像素的顏色資訊
$greyscale = .299 * $color["red"] + .587 * $color["green"] + .114 * $color["blue"]; // 計算灰階值
ImageSetPixel($grey_img, $x, $y, ImageColorClosest($grey_img, $greyscale, $greyscale, $greyscale)); // 設定灰階圖片像素
}
}
for($x = 0; $x <= $isx; $x++) { // 迴圈處理每一列像素
for($y = 0; $y <= $isy; $y++) { // 迴圈處理每一行像素
$color = ImageColorsForIndex($grey_img, ImageColorAt($grey_img, $x, $y)); // 取得灰階圖片像素的顏色資訊
if ($color["red"] > 128) { // 灰階值大於 128 則設為白色
ImageSetPixel($dst_img, $x, $y, ImageColorClosest($dst_img, 255, 255, 255)); // 設定目標圖片像素為白色
$err = $color["red"] - 255; // 計算誤差值
} else { // 灰階值小於等於 128 則設為黑色
ImageSetPixel($dst_img, $x, $y, ImageColorClosest($dst_img, 0, 0, 0)); // 設定目標圖片像素為黑色
$err = $color["red"]; // 計算誤差值
}
if ($x != $isx) { // 擴散誤差至右邊像素 (若非最右邊像素)
$color2 = ImageColorsForIndex($grey_img, ImageColorAt($grey_img, $x + 1, $y)); // 取得右邊像素顏色資訊
$newgrey = $color2["red"] + $err * 7 / 16; // 計算新的灰階值
ImageSetPixel($grey_img, $x + 1, $y, ImageColorClosest($grey_img, $newgrey, $newgrey, $newgrey)); // 設定右邊像素
}
if ($x != 0) { // 擴散誤差至左邊像素 (若非最左邊像素)
$color2 = ImageColorsForIndex($grey_img, ImageColorAt($grey_img, $x-1, $y)); // 取得左邊像素顏色資訊
$newgrey = $color2["red"] + $err * 3 / 16; // 計算新的灰階值
ImageSetPixel($grey_img, $x-1, $y, ImageColorClosest($grey_img,$newgrey, $newgrey, $newgrey)); // 設定左邊像素
}
if ($y != $isy) { // 擴散誤差至下方像素 (若非最下方像素)
$color2 = ImageColorsForIndex($grey_img, ImageColorAt($grey_img, $x, $y+1)); // 取得下方像素顏色資訊
$newgrey = $color2["red"] + $err * 5 / 16; // 計算新的灰階值
ImageSetPixel($grey_img, $x, $y+1, ImageColorClosest($grey_img,$newgrey, $newgrey, $newgrey)); // 設定下方像素
}
if ($x != $isx && $y != $isy) { // 擴散誤差至右下方像素 (若非最右下方像素)
$color2 = ImageColorsForIndex($grey_img, ImageColorAt($grey_img, $x + 1, $y + 1)); // 取得右下方像素顏色資訊
$newgrey = $color2["red"] + $err / 16; // 計算新的灰階值
ImageSetPixel($grey_img, $x + 1, $y + 1, ImageColorClosest($grey_img, $newgrey, $newgrey, $newgrey)); // 設定右下方像素
}

}
}
imagedestroy($grey_img); // 銷毀灰階圖片
}
1
lukeross at sys3175 dot co dot uk
22 年前
如同先前的評論所述,GD 在轉換成雙色圖像方面表現不佳,尤其是在處理照片時。以下程序將圖像轉換為兩種顏色,我認為是使用誤差擴散法(演算法取自新聞)。它很慢,但對於小圖像和低負載來說勉強夠用。我懷疑它可以變得更有效率 :-)

function ImageColorFloydSteinberg($dst_img, $src_img) {
ImageColorAllocate($dst_img, 0, 0, 0); // 配置黑色
ImageColorAllocate($dst_img, 255, 255, 255); // 配置白色
$grey_img = ImageCreate(ImageSX($src_img), ImageSY($src_img)); // 建立灰階圖片
for ($a = 0; $a <= 255; $a++) ImageColorAllocate($grey_img, $a, $a, $a); // 配置灰階色盤
for($x = 0; $x <= ImageSX($src_img); $x++) {
for($y = 0; $y <= ImageSY($src_img); $y++) {
$color = ImageColorsForIndex($src_img, ImageColorAt($src_img, $x, $y)); // 取得來源圖片像素的顏色資訊
$greyscale = .299 * $color["red"] + .587 * $color["green"] + .114 * $color["blue"]; // 計算灰階值
ImageSetPixel($grey_img, $x, $y, ImageColorClosest($grey_img, $greyscale, $greyscale, $greyscale)); // 設定灰階圖片像素
}
}
for($x = 0; $x <= ImageSX($src_img); $x++) {
for($y = 0; $y <= ImageSY($src_img); $y++) {
$color = ImageColorsForIndex($grey_img, ImageColorAt($grey_img, $x, $y)); // 取得灰階圖片像素的顏色資訊
if ($color["red"] > 128) { // 灰階值大於 128 則設為白色
ImageSetPixel($dst_img, $x, $y, ImageColorClosest($dst_img, 255, 255, 255)); // 設定目標圖片像素為白色
$err = $color["red"] - 255; // 計算誤差值
} else { // 灰階值小於等於 128 則設為黑色
ImageSetPixel($dst_img, $x, $y, ImageColorClosest($dst_img, 0, 0, 0)); // 設定目標圖片像素為黑色
$err = $color["red"]; // 計算誤差值
}
if ($x != ImageSx($src_img)) {
$color2 = ImageColorsForIndex($grey_img, ImageColorAt($grey_img, $x + 1, $y)); // 取得右邊像素顏色資訊
$newgrey = $color2["red"] + $err * 7 / 16; // 計算新的灰階值
ImageSetPixel($grey_img, $x + 1, $y, ImageColorClosest($grey_img, $newgrey, $newgrey, $newgrey)); // 設定右邊像素
}
if ($x != 0) { // 擴散誤差至左邊像素 (若非最左邊像素)
$color2 = ImageColorsForIndex($grey_img, ImageColorAt($grey_img, $x-1, $y)); // 取得左邊像素顏色資訊
$newgrey = $color2["red"] + $err * 3 / 16; // 計算新的灰階值
ImageSetPixel($grey_img, $x-1, $y, ImageColorClosest($grey_img,$newgrey, $newgrey, $newgrey)); // 設定左邊像素
}
if ($y != ImageSy($src_img)) {
$color2 = ImageColorsForIndex($grey_img, ImageColorAt($grey_img, $x, $y+1)); // 取得下方像素顏色資訊
$newgrey = $color2["red"] + $err * 5 / 16; // 計算新的灰階值
ImageSetPixel($grey_img, $x, $y+1, ImageColorClosest($grey_img,$newgrey, $newgrey, $newgrey)); // 設定下方像素
}
if ($x != ImageSx($src_img) && $y != ImageSy($src_img)) {
$color2 = ImageColorsForIndex($grey_img, ImageColorAt($grey_img, $x + 1, $y + 1)); // 取得右下方像素顏色資訊
$newgrey = $color2["red"] + $err / 16; // 計算新的灰階值
ImageSetPixel($grey_img, $x + 1, $y + 1, ImageColorClosest($grey_img, $newgrey, $newgrey, $newgrey)); // 設定右下方像素
}

}
}
imagedestroy($grey_img); // 銷毀灰階圖片
}

要輸出 WBMP 圖像,請使用

ImageWBMP($final_img, "", ImageColorClosest(255, 255, 255));
To Top