PHP Conference Japan 2024

imagecolortransparent

(PHP 4, PHP 5, PHP 7, PHP 8)

imagecolortransparent定義透明色彩

說明

imagecolortransparent(GdImage $image, ?int $color = null): int

取得或設定指定 image 中的透明色彩。

參數

image

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

color

使用 imagecolorallocate() 建立的色彩識別碼。

傳回值

傳回新的(或目前的,如果未指定)透明色彩的識別碼。如果 colornull,且影像沒有透明色彩,則傳回的識別碼將為 -1

更新日誌

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

範例

範例 #1 imagecolortransparent() 範例

<?php
// 建立一個 55x30 的影像
$im = imagecreatetruecolor(55, 30);
$red = imagecolorallocate($im, 255, 0, 0);
$black = imagecolorallocate($im, 0, 0, 0);

// 將背景設為透明
imagecolortransparent($im, $black);

// 繪製一個紅色矩形
imagefilledrectangle($im, 4, 4, 50, 25, $red);

// 儲存影像
imagepng($im, './imagecolortransparent.png');
imagedestroy($im);
?>

上述範例將輸出類似以下的內容

Output of example : imagecolortransparent()

注意事項

注意:

透明度僅會透過 imagecopymerge() 和真彩色影像複製,而不會透過 imagecopy() 或調色盤影像複製。

注意:

透明色彩是影像的屬性,透明度不是色彩的屬性。一旦您將一種色彩設定為透明色彩,先前繪製的該色彩的任何影像區域都將變為透明。

新增註釋

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

erik at vectorsector dot net
17 年前
這段程式碼可以讓已建立的 PNG 圖片中指定的 RGB 顏色變成透明。它也包含了在處理過程中不會被破壞的文字疊加程式碼。

<?php

header
("Content-type: image/png");
$image = imagecreatetruecolor(250, 250);
$string = $_GET['text'];
$im = imagecreatefrompng("dynIcon.png");
$img = imagecreatetruecolor(16,16);
$orange = imagecolorallocate($im, 220, 210, 60);
$bg_color = imagecolorat($im,1,1);
$px = (imagesx($im) - 3 * strlen($string)) / 2;
imagecolortransparent($im, $bg_color);
imagestring($im, 3, $px, 5, $string, $orange);
imagepng($im);
imagedestroy($im);

?>

使用 `$bg_color = imagecolorat($im,1,1);` 例如,如果您在 Photoshop 中透過清除所有顏色,留下顯示為空的棋盤格背景來製作透明顏色。
markglibres at yahoo dot com
15 年前
我寫了一個非常簡單的程式碼,可以在調整圖片大小時,特別是保留圖片的透明度。

注意:只有 GIF 和 PNG 檔案支援透明度。

參數

`$new_image`:圖片資源識別碼,例如 `imagecreatetruecolor()` 返回的識別碼。必須以傳址方式傳遞。
`$image_source`:圖片資源識別碼,由 `imagecreatefromjpeg`、`imagecreatefromgif` 和 `imagecreatefrompng` 返回。必須以傳址方式傳遞。

<?php
function setTransparency($new_image,$image_source)
{

$transparencyIndex = imagecolortransparent($image_source);
$transparencyColor = array('red' => 255, 'green' => 255, 'blue' => 255);

if (
$transparencyIndex >= 0) {
$transparencyColor = imagecolorsforindex($image_source, $transparencyIndex);
}

$transparencyIndex = imagecolorallocate($new_image, $transparencyColor['red'], $transparencyColor['green'], $transparencyColor['blue']);
imagefill($new_image, 0, 0, $transparencyIndex);
imagecolortransparent($new_image, $transparencyIndex);

}
?>


範例用法:(調整大小)

<?php
$image_source
= imagecreatefrompng('test.png');
$new_image = imagecreatetruecolor($width, $height);
setTransparency($new_image,$image_source);
imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
?>
email at vladislav dot net
13 年前
請注意,某些 GIF 影像可能不包含透明色彩。markglibres at yahoo dot com 於 2009 年 3 月 29 日 02:48 提供了一個在調整大小的 GIF 影像中強制透明化的良好範例。但有時 GIF 影像中的透明色彩可能未設定。問題在於,您強制設定為透明的顏色可能在原始 GIF 中被用作不透明顏色,而您將在調整大小的影像中失去該顏色。解決方案是不使用某些預設的透明色彩,並讓調整大小後的影像沒有透明色彩(與原始 GIF 相同)。我使用了(幾乎)以下程式碼,讓調整大小的 GIF 影像僅在需要透明化時才透明。

<?php
/* ... */

$img = ImageCreateFromGIF($f); /* 從現有的 GIF 建立圖像:原始圖像檔名 $f 可以從您需要的地方取得 */
$transparent_index = ImageColorTransparent($img); /* 傳回目前透明色的索引或 -1 */
if($transparent_index!=(-1)) $transparent_color = ImageColorsForIndex($img,$transparent_index);

/* ... */

$img_resized = ImageCreateTrueColor( $nw, $nh ); /* (新寬度 $nw 和高度 $nh 必須事先定義) */
if(!empty($transparent_color)) /* 簡單檢查以確認是否設定了透明色 */
{
$transparent_new = ImageColorAllocate( $img_resized, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue'] );
$transparent_new_index = ImageColorTransparent( $img_resized, $transparent_new );
ImageFill( $img_resized, 0,0, $transparent_new_index ); /* 別忘了用透明色填充新圖像 */
}
list(
$w,$h) = GetImageSize($f); /* 定義原始寬度 $w 和高度 $h */
if( ImageCopyResized( $img_resized, $img, 0,0, 0,0, $nw,$nh, $w,$h ) ) /* 縮放複製並取代原始圖像 */
{
ImageDestroy($img);
$img = $img_resized;
}

/* ... */

header('Content-Type: image/gif');
ImageGIF($img);
ImageDestroy($img);
?>

備註:
如果有一些錯誤,我希望你能理解其想法。
romanenko.alex.v at gmail.com
10 年前
這個函數在 GD 版本 > 2 時有個很奇怪的行為。如果找不到透明色,它會回傳顏色數量而不是 -1(如文件中所述)。請小心!
steved at HaHaHa dot com dot au
16 年前
如何在 GD 圖像中垂直和水平置中文本

<?php
//取得文字方塊大小
$box = imagettfbbox($titlesize, 0, $titlefont, $title);

//計算文字方塊的寬度和高度
$textW = $box[2] - $box[0];
$textH = $box[5] - $box[3];

// 計算位置
$positionLeft = ($width - $textW)/2;
$positionTop = (($height - $textH)/2);

// 加入文字
if($align=="center"){
imagettftext($pic, size, angle, $positionLeft, $positionTop, colour, font, message);
?>
Mikasto
17 年前
調整透明 PNG 的大小(如果圖像是透明的且 ImageColorTransparent() 返回 -1)
1) 使用新的尺寸建立一個新的圖像
2) 將新圖像設定為完全透明
3) 關閉新圖像的 Alpha 混合(在複製數據時保留 Alpha 通道)
4) 使用 copyresampled 或 copyresized 複製到新圖像

PHP 程式碼
// 1
$im2 = ImageCreateTrueColor($w, $h);
// 2
ImageColorTransparent($im2, ImageColorAllocate($im2, 0, 0, 0));
// 3
ImageAlphaBlending($im2, false);
// 4
ImageCopyResampled($im2, $im, 0, 0, 0, 0, $w, $h, ImageSX($im), ImageSY($im));
talkback at codesponge dot com
19 年前
一張圖片只能設定一個顏色為透明色。最後一次呼叫 imagecolortransparent 設定的顏色才會生效。

我正在處理按鈕圖像,它的填充顏色與按鈕邊框外的背景顏色略有不同。我原本希望可以將這兩種顏色都設為透明來解決問題。

希望這點資訊能幫你省點時間。
cca220v
16 年前
我吃盡苦頭才發現,要正確堆疊兩個具有 Alpha 透明度的 PNG,需要明確地重新啟用 Alpha 混合。

$img=imagecreatetruecolor($dx,$dy);
$img1 = imagecreatefrompng($png1); //第一層
$img2 = imagecreatefrompng($png2); //第二層 (較小的圖片)

imagealphablending($img, false);
$transparent = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagefill($img, 0, 0, $transparent);
imagesavealpha($img, true);
imagealphablending($img, true); //移除這行會導致第二層的透明度穿透第一層並將其清除 (圖片在該處「的確」是透明的...第二層也是...但第一層不是,所以它不應該透明)

imagecopyresampled($img, $img1,
imagecopyresampled($img, $img2,
webnetwizard.co.uk 的 webmaster
19 年前
經過一番絞盡腦汁後,我找到了一個方法來測試任何 GIF 是否存在背景透明度。這個功能對我的應用程式至關重要,它會上傳 GIF、JPEG 或 PNG 的任何一種,並同時創建相同圖像類型和相同檔案名的縮圖(完整大小和縮圖版本儲存在不同的資料夾中)。
以上傳並移動圖像後,一個 switch($image_type) 陳述式可確保使用最佳程式碼來產生縮圖;無論圖像類型為何。
GIF 類型的問題在於,具有透明背景的 GIF 需要與沒有透明背景的 GIF 進行不同的處理。當我沒有偵測到 GIF 透明度時,要麼所有透明 GIF 的背景都會變成黑色,要麼所有 GIF 都會轉換為透明背景 - 即使它們在原始圖像中並非透明的。
但是如何偵測原始圖像中的透明度呢?我最終想到,我可以透過程式設計方式測試透明度,方法是將原始圖像的副本疊加在全黑圖像上,記錄特定像素位置的顏色值,然後重複此過程,將原始圖像的副本疊加在全白圖像上,記錄相同像素位置的顏色值,並將這些值與第一組值進行比較。
如果兩組值完全相關,並且使用了合理的取樣點,則可以將圖像視為不透明。如果兩組值顯示差異,則應將圖像視為具有透明背景。
optonline.net 的 fmkaiba
16 年前
這是我的「完美」(我輕描淡寫地使用這個詞)縮圖生成腳本,將 '$transparency' 切換為 true 即可盡可能處理 gif 和 png 中的透明度。這段程式碼是根據這裡其他人的評論和建議建構的,我不應該居功。到目前為止,它可以處理我遇到的所有錯誤。
<?php
function createthumb($name, $newname, $new_w, $new_h, $border=false, $transparency=true, $base64=false) {
if(
file_exists($newname))
@
unlink($newname);
if(!
file_exists($name))
return
false;
$arr = split("\.",$name);
$ext = $arr[count($arr)-1];

if(
$ext=="jpeg" || $ext=="jpg"){
$img = @imagecreatefromjpeg($name);
} elseif(
$ext=="png"){
$img = @imagecreatefrompng($name);
} elseif(
$ext=="gif") {
$img = @imagecreatefromgif($name);
}
if(!
$img)
return
false;
$old_x = imageSX($img);
$old_y = imageSY($img);
if(
$old_x < $new_w && $old_y < $new_h) {
$thumb_w = $old_x;
$thumb_h = $old_y;
} elseif (
$old_x > $old_y) {
$thumb_w = $new_w;
$thumb_h = floor(($old_y*($new_h/$old_x)));
} elseif (
$old_x < $old_y) {
$thumb_w = floor($old_x*($new_w/$old_y));
$thumb_h = $new_h;
} elseif (
$old_x == $old_y) {
$thumb_w = $new_w;
$thumb_h = $new_h;
}
$thumb_w = ($thumb_w<1) ? 1 : $thumb_w;
$thumb_h = ($thumb_h<1) ? 1 : $thumb_h;
$new_img = ImageCreateTrueColor($thumb_w, $thumb_h);

if(
$transparency) {
if(
$ext=="png") {
imagealphablending($new_img, false);
$colorTransparent = imagecolorallocatealpha($new_img, 0, 0, 0, 127);
imagefill($new_img, 0, 0, $colorTransparent);
imagesavealpha($new_img, true);
} elseif(
$ext=="gif") {
$trnprt_indx = imagecolortransparent($img);
if (
$trnprt_indx >= 0) {
//its transparent
$trnprt_color = imagecolorsforindex($img, $trnprt_indx);
$trnprt_indx = imagecolorallocate($new_img, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
imagefill($new_img, 0, 0, $trnprt_indx);
imagecolortransparent($new_img, $trnprt_indx);
}
}
} else {
Imagefill($new_img, 0, 0, imagecolorallocate($new_img, 255, 255, 255));
}

imagecopyresampled($new_img, $img, 0,0,0,0, $thumb_w, $thumb_h, $old_x, $old_y);
if(
$border) {
$black = imagecolorallocate($new_img, 0, 0, 0);
imagerectangle($new_img,0,0, $thumb_w, $thumb_h, $black);
}
if(
$base64) {
ob_start();
imagepng($new_img);
$img = ob_get_contents();
ob_end_clean();
$return = base64_encode($img);
} else {
if(
$ext=="jpeg" || $ext=="jpg"){
imagejpeg($new_img, $newname);
$return = true;
} elseif(
$ext=="png"){
imagepng($new_img, $newname);
$return = true;
} elseif(
$ext=="gif") {
imagegif($new_img, $newname);
$return = true;
}
}
imagedestroy($new_img);
imagedestroy($img);
return
$return;
}
//example useage
createthumb("img.gif", "tn_img.gif", 64,64,true, true, false);
?>
wesley.gunn@email.it
17 年前
一種檢查具有 Alpha 通道的 png 的簡單方法:讀取 IHDR PNG 標頭

$readPng = fopen($argSourceImagePath, "rb");
$readAlp = fread($readPng, 52);
fclose($readPng);

if (substr(bin2hex($readAlp), 50, 2) == "04" || substr(bin2hex($readAlp), 50, 2) == "06")
echo("Png 具有 Alpha 通道");
bengeweb.co.nz 的 glenn
18 年前
關於 webnetwizard.co.uk 的 webmaster 使用相當複雜的方法來判斷 GIF 是否設定了透明度 ...

如果沒有找到透明度,imagecolortransparent 將返回 -1。

例如

$transColorIndexMain = imageColorTransparent($mainImgObj);
if ($transColorIndexMain >= 0) {
# GIF 具有透明度 ... ;
}

目前 PHP 版本:4.4.4
[GD 版本] => bundled (2.0.28 相容)
galeria-m.art.pl 的 emilus
20 年前
調整大小或複製具有透明度的圖像(gif [使用 gd>=2.0.28] 或 png)。
- 取得目前的透明顏色
- 創建一個與舊圖像調色盤相同的新圖像,並用透明顏色填充新圖像
- 設定透明顏色
- 複製調整大小後的圖像

$colorTransparent = imagecolortransparent($im);
$im2 = imagecreate($new_w, $new_h);
imagepalettecopy($im2,$im); // 複製影像調色盤
imagefill($im2,0,0,$colorTransparent); // 以透明色填充影像
imagecolortransparent($im2, $colorTransparent); // 設定影像透明色
imagecopyresized($im2,$im,0,0,0,0,$new_w,$new_h,$imsx,$imsy); // 調整影像大小並複製
creator79>>web>>de
21 年前
嗨!
據我所知,我目前使用的是 GDLib 1.6.3。
當您使用 imagecolorclosest($im, $R, $G, $B) 函式而不是 imagecolorallocate() 或 imagecolorexact() 時,您可以在任何影像類型的任何已定義顏色上實現透明效果。
Sebastian Sejzer
8 年前
如果您需要一個空白的 PNG 圖檔,您不需要每次都生成它。只需定義以下常數:

define("BLANK_PNG", "iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29m".
"dHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAADqSURBVHjaYvz//z/DYAYAAcTEMMgBQAANegcCBNCg".
"dyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAAN".
"egcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQ".
"oHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAA".
"DXoHAgTQoHcgQAANegcCBNCgdyBAgAEAMpcDTTQWJVEAAAAASUVORK5CYII=");
john at jbwalker dot com
12 年前
由於反鋸齒的關係,帶有文字的透明背景似乎效果不太好。但我嘗試了以下的權宜之計,結果非常好:

<?php
$im
= imagecreatetruecolor(100,20);
$almostblack = imagecolorallocate($im,254,254,254);
imagefill($im,0,0,$almostblack);
$black = imagecolorallocate($im,0,0,0);
imagecolortransparent($im,$almostblack);
//... 設定 x 和 y 座標..
imagettftext($im,8,0,$x,$y,$black,"calibri.ttf",$txt);
?>
admin at fastproxyservers dot org
14 年前
我想分享一下如何讓 PNG 圖片透明化。我幾乎嘗試了該頁面上描述的所有範例,但沒有一個對我有用。最後我找到了解決方案,但有點取巧 :) 但如果有效,誰在乎呢?

因此,當我嘗試將白色(例如)分配為透明色時,它的效果是隨機的(假設 10 個範例中只有 1 個有效)。這反過來讓我想到,問題在於白色已經存在於調色盤中。因此,如果我嘗試添加另一個具有不同索引的白色作為透明色,則會導致錯誤。

所以我找到了解決方案,我不是添加新的索引,而是在調色盤中搜尋白色的索引,並將其定義為透明 :) 最好的方法是不要使用白色,而是使用左下角的顏色作為透明色(我猜這是一些其他軟體語言的標準)。

<?php
$index
= imagecolorexact($this->image, 255, 255, 255);
imagecolortransparent($this->image, $index);
?>

您只需要在輸出結果之前加上這兩行。
ajamal
15 年前
這對新手來說可能很重要...如果您嘗試創建具有透明度的 GIF,請堅持對圖像上的所有顏色使用 216 色的調色板... 否則透明度將無法正常工作...至少您會得到抖動...
yousuf at philipz dot com
16 年前
嗯,8 位元 gif 和 png 檔案可以有多種透明顏色,所以透過手動查看檔案或使用 imagecolortransparent 來檢測透明顏色,並在 imagecopy(或其他圖像複製函式)到真彩色圖像時使用檢測到的 rgb 顏色作為背景顏色將無法正常工作。讓透明像素出現的唯一方法是將像素顏色逐個複製到新的真彩色圖像。以下是我編寫的一些程式碼來執行此操作。

$original_image = @imagecreatefromgif( $original_image_file );

$original_image_width = imagesx( $original_image );
$original_image_height = imagesy( $original_image );

if ( !imageistruecolor($original_image) ) {
# 檢測它是否為非真彩色圖像
if ( imagecolortransparent($original_image) >= 0 ) {
# 檢測是否有任何調色板已設定為透明
$truecolor = imagecreatetruecolor( $original_image_width, $original_image_height );
for ($x = 0; $x < $original_image_width; $x++) {
for ($y = 0; $y < $original_image_height; $y++) {
$color_index = ImageColorAt($original_image, $x, $y);
if ( !$color_palette[$color_index] ) {
$rgb = imagecolorsforindex($original_image, $color_index);
$color_to_set = imagecolorallocate($truecolor, $rgb['red'], $rgb['green'], $rgb['blue']);
$color_palette[$color_index] = $color_to_set;
} else {
$color_to_set = $color_palette[$color_index];
}
imagesetpixel($truecolor, $x, $y, $color_to_set);
}
}
imagedestroy($original_image);
$original_image = $truecolor;
}
}
kroccamen, gmail
16 年前
抱歉,以下應該是

<?php
$is_alpha
= ord (file_get_contents ($file_path, false, null, 25, 1)) & 4;
?>

好多了 :)
類型 4(灰階透明 png)和 6(彩色透明 png)都設定了第 4 位元,因此無需進行兩次位元運算,也無需使用 ==,因為返回值將是 4 或 0,這很容易在程式碼的其他地方解釋為 true 或 false。

注意:在這種情況下,file_get_contents 並未讀取整個檔案,而只讀取了 1 個位元組,因此您可以放心,這是快速且安全的。
kroccamen, gmail
16 年前
回覆 @wesley gunn
非常感謝您提供用於判斷 PNG 影像是 24 位元還是 32 位元的程式碼 :)
這是改良的單行程式碼,無需使用檔案控制代碼

<?php
$is_alpha
= ((ord (
file_get_contents ($filename, false, null, 25, 1)
) &
6) & 4) == 4;
?>

如果 PNG 影像有 Alpha 透明通道,則回傳 true,否則回傳 false。
這個方法無法偵測 8 位元且具有透明度的 PNG 影像,但您可以透過搜尋檔案內容中是否存在 "PLTE" *以及* "tRNS" 來判斷。
tomas at cimax dot com
16 年前
在某些版本中,您必須設定調色盤中該顏色的索引編號,而不是顏色本身!因此,如果您使用 imagecolorallocate 分配顏色,則該顏色在調色盤上的索引為 0,所以您必須呼叫:imagecolortransparent ( resource $image , 0 ) 才能使影像透明!
cinymini
17 年前
當您使用調色盤影像(使用 imagecreate() 建立)時,第一個分配的顏色是背景顏色。此顏色不能用於透明度。因此,如果您想要使背景透明,請先分配一個虛擬的背景顏色,然後再分配真正的背景顏色,並將其宣告為透明。
ingrid
19 年前
這個腳本應該可以處理具有白色背景透明的 GIF 動畫,在上面加上一些白色透明文字,並且可以使用檔名和一般的 image 標籤在任何網頁中使用。

一切都運作良好,除了動畫之外。GIF 動畫是靜止的。不知道如何在 PHP 中保持動畫的動態效果。請問這在 PHP 中可行嗎?

<?php

$TextFile
= "teller.txt";
$Count = trim(file_get_contents($TextFile));
$FP = fopen($TextFile, "r");
$Count=fgets($FP, 4096);
fclose ($FP);
settype($Count, "integer");
$Count++;
if (
$FP = fopen ($TextFile, "w")){
fwrite ($FP, $Count);
fclose ($FP);
}

$image = "blad.gif";
$im = imagecreatefromgif($image);
$white = imageColorAllocate ($im, 255, 255, 255);
$trans = imagecolortransparent($im,$white);
$hit = "$Count";
$ip = $_SERVER["REMOTE_ADDR"];

ImageString($im, 2, 30, 60, " je bent bezoeker ", $white);
ImageString($im, 3, 20, 80, " $ip.$hit", $trans);

header("Content-Type: image/gif");
Imagegif($im,'',100);
ImageDestroy ($im);

?>
php_comment et 5mm de
20 年前
實際上,這個函式可以用於兩個目的

A.) 在 PHP 中處理影像時,處理透明度(參見浮水印範例)

B.) 建立部分透明的圖片

對於 A.),可以使用一些規定的真彩色影像,但 B.) 只能用於基於調色盤的影像(=索引=使用 imagecreate 建立,而不是 imagecreatetruecolor 建立)以及支援透明度的格式(png、gif)。

例如,如果您想從給定的真彩色圖片中裁剪出一種顏色,請使用以下方法先將其轉換為基於調色盤的影像,指定透明度,然後以 png 格式傳送給瀏覽器

<?
$img = imagecreatefromjpeg('test.jpg'); // 從 test.jpg 建立圖像
imagetruecolortopalette($img, false, 256); // 轉換成調色板圖像

$white = imagecolorresolve($img, 255, 255, 255); // 取得白色在調色板中的索引值
imagecolortransparent($img, $white); // 設定白色為透明色

header("Content-type: image/png"); // 設定 HTTP 標頭為 PNG 圖片
imagepng($img); // 輸出 PNG 圖片
?>
php at kybert dot com
21 年前
我遇到了 sandhawk at spies dot com 的範例程式碼的問題,因為我的 PNG 浮水印和 JPEG 畫布使用了不同的色彩深度,所以這個函式可以修正這個問題。

[程式碼]
function WatermarkImage($CanvasImage, $WatermarkImage /* 必須是 PNG */, $Opacity=10, $Quality=75)
{
// 建立真彩畫布圖像
$canvas_src = imagecreatefromjpeg($CanvasImage); // 從 JPEG 檔案建立畫布圖像
$canvas_w = ImageSX($canvas_src); // 取得畫布圖像寬度
$canvas_h = ImageSY($canvas_src); // 取得畫布圖像高度
$canvas_img = imagecreatetruecolor($canvas_w, $canvas_h); // 建立真彩畫布圖像
imagecopy($canvas_img, $canvas_src, 0,0,0,0, $canvas_w, $canvas_h); // 複製畫布圖像
imagedestroy($canvas_src); // 不再需要原始畫布圖像,釋放資源

// 建立真彩浮水印圖像
$overlay_src = imagecreatefrompng($WatermarkImage); // 從 PNG 檔案建立浮水印圖像
$overlay_w = ImageSX($overlay_src); // 取得浮水印圖像寬度
$overlay_h = ImageSY($overlay_src); // 取得浮水印圖像高度
$overlay_img = imagecreatetruecolor($overlay_w, $overlay_h); // 建立真彩浮水印圖像
imagecopy($overlay_img, $overlay_src, 0,0,0,0, $overlay_w, $overlay_h); // 複製浮水印圖像
imagedestroy($overlay_src); // 不再需要原始浮水印圖像,釋放資源

// 設定透明色 (擇一)
$black = imagecolorallocate($overlay_img, 0x00, 0x00, 0x00); // 黑色
$white = imagecolorallocate($overlay_img, 0xFF, 0xFF, 0xFF); // 白色
$magenta = imagecolorallocate($overlay_img, 0xFF, 0x00, 0xFF); // 洋紅色
// 在此使用設定的透明色
imagecolortransparent($overlay_img, $white); // 設定白色為透明色

// 複製並合併浮水印圖像和畫布圖像
imagecopymerge($canvas_img, $overlay_img, 0,0,0,0, $overlay_w, $overlay_h, $Opacity); // 合併圖像

// 輸出
header("Content-type: image/jpeg"); // 設定 HTTP 標頭為 JPEG 圖片
imagejpeg($canvas_img, '', $Quality); // 輸出 JPEG 圖片

imagedestroy($overlay_img); // 釋放浮水印圖像資源
imagedestroy($canvas_img); // 釋放畫布圖像資源
}

// 使用 50% 透明度和 95% 品質呼叫函式
WatermarkImage("canvas.jpg", "overlay.png", 50, 95);

[/程式碼]
calar at gamigo dot de
21 年前
好的,有些東西可以運作...

我有一個使用 GDLib 的測試系統,phpinfo 顯示我的版本是
「GD 版本 2.0 或更高版本」,到目前為止使用 imageColorTransparent 沒有問題。

然後我們必須將程式碼複製到另一台伺服器上,在這台伺服器上,phpinfo 顯示我的版本是
「GD 版本 bundled (2.0.12 compatible)」。

JPEG 圖像出錯了,而且沒有任何東西是透明的。
重點是,你必須使用:imagecopymerge

不確定是否在所有系統上都適用,只在
SUSE 8.2
PHP 4.3.2 上運作
Jonatan
21 年前
我發現 ImageColorTransparent() 只有在透明色是黑色的情況下才能在真彩圖像上運作。
(PHP 4.2.3/GD 2.0)
To Top