0

aplha チャネルを使用して、画像に透明な領域を作成する方法を探しています。

私はちょうどその方法を見つけました:

色を定義し、透明に設定します。

imagealphablending($img, false);   // has to be false for imagecolortransparent
imagesavealpha($img, false);       // false = single color transparency

$mask_color = imagecolorallocatealpha($img, 0, 255, 0, 127);
imagecolortransparent( $img, $mask_color );

しかし、結果は単色透明になります。アルファチャンネルのある画像とマージしようとすると、マスクの色が再び表示されます。

これは、そのマージプロセスに必要な imagesavealpha() を再度有効にした瞬間に発生します。

ところで、ソース画像とマスク画像をマージして$imgを作成します。

$new_img = imagecreatetruecolor( 150, 100 );
$new_white = imagecolorallocatealpha( $new_img, 255, 255, 255, 0 );
imagefill( $new_img, 0, 0, $new_white );

imagealphablending($new_img, false);   // has to be false for full alpha
imagesavealpha($new_img, true);       // true = full alpha channel

imagecopyresampled( $new_img, $img       ,0, 0, 0, 0, 150, 100, 150, 100);
imagecopyresampled( $new_img, $alpha_img ,0, 0, 0, 0, 150, 100, 150, 100);

// save preview
imagepng( $new_img, $image_path . $preview_dir_name . $new_file_name );

$imgの透明な領域が再び表示されるようになりました。

では、フル アルファ チャネルを使用して特定の色の領域を透明にする方法はありますか?

4

1 に答える 1

0

私は方法を発見しました:

// get source image
$img = imagecreatefromjpeg( $image_path . $image_file );

// save transparency in alpha channel
imagealphablending($img, false);
imagesavealpha($img, true);

// define index for transparent color
$transparent = imagecolorallocatealpha( $preview_temp, 255, 255, 255, 127 );

// replace pixel information
imagesetpixel( $img, $x, $y, $transparent );

を使用するimagesetpixel()と、ピクセルを置き換えて、アルファ チャネルを使用して特定の画像の透明な領域を取得できます。

それは私にとってはうまくいくので、後で完全なコードを投稿する予定です。

しかし、他の解決策を知っていますか?

于 2012-06-07T00:36:11.737 に答える