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の透明な領域が再び表示されるようになりました。
では、フル アルファ チャネルを使用して特定の色の領域を透明にする方法はありますか?