라이브러리

[PHP] imagealphablending - 이미지의 혼합 모드를 설정합니다.




이미지 알파 블렌딩 (Image Alpha Blending)

이미지 알파 블렌딩은 두 개 이상의 이미지의 알파 채널을 결합하여 새로운 이미지를 생성하는 기술입니다. 알파 채널은 이미지의 투명도 정보를 담고 있으며, 이미지의 투명도에 따라 이미지의 색상이 변경됩니다.

PHP에서 이미지 알파 블렌딩

PHP에서 이미지 알파 블렌딩을 수행하는 방법은 `imagealphablending` 함수를 사용하는 것입니다. 이 함수는 두 개의 이미지의 알파 채널을 결합하여 새로운 이미지를 생성합니다.

예제


#hostingforum.kr
php

<?php



// 두 개의 이미지 로드

$image1 = imagecreatefrompng('image1.png');

$image2 = imagecreatefrompng('image2.png');



// 이미지 크기 확인

$width = imagesx($image1);

$height = imagesy($image1);



// 이미지 알파 블렌딩

imagealphablending($image1, true);

imagealphablending($image2, true);



// 두 개의 이미지 결합

for ($y = 0; $y < $height; $y++) {

    for ($x = 0; $x < $width; $x++) {

        $color1 = imagecolorat($image1, $x, $y);

        $color2 = imagecolorat($image2, $x, $y);



        $alpha1 = (imagecolorat($image1, $x, $y) >> 24) & 0xFF;

        $alpha2 = (imagecolorat($image2, $x, $y) >> 24) & 0xFF;



        $red = (($color1 >> 16) & 0xFF) * (1 - $alpha2 / 255) + (($color2 >> 16) & 0xFF) * $alpha2 / 255;

        $green = (($color1 >> 8) & 0xFF) * (1 - $alpha2 / 255) + (($color2 >> 8) & 0xFF) * $alpha2 / 255;

        $blue = ($color1 & 0xFF) * (1 - $alpha2 / 255) + ($color2 & 0xFF) * $alpha2 / 255;



        $new_color = imagecolorallocatealpha($image1, $red, $green, $blue, $alpha1 + $alpha2 - $alpha1 * $alpha2 / 255);

        imagesetpixel($image1, $x, $y, $new_color);

    }

}



// 이미지 저장

imagepng($image1, 'result.png');



// 이미지 삭제

imagedestroy($image1);

imagedestroy($image2);



?>



이 예제에서는 두 개의 PNG 이미지 (`image1.png` 및 `image2.png`)를 로드하고, 이미지 크기를 확인한 후, 이미지 알파 블렌딩을 수행합니다. 두 개의 이미지의 알파 채널을 결합하여 새로운 이미지를 생성하고, 결과 이미지를 `result.png` 파일로 저장합니다.

참고


* `imagealphablending` 함수는 두 개의 이미지의 알파 채널을 결합하여 새로운 이미지를 생성합니다.
* `imagecolorat` 함수는 이미지의 픽셀 색상을 반환합니다.
* `imagecolorallocatealpha` 함수는 새로운 색상을 생성하고 알파 채널을 설정합니다.
* `imagesetpixel` 함수는 이미지의 픽셀 색상을 설정합니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 10,077건 / 500 페이지

검색

게시물 검색