Computer >> 컴퓨터 >  >> 프로그래밍 >> PHP

PHP imagelayereffect() 함수로 알파 블렌딩 플래그 설정하고 레이어 효과 적용하기

imagelayereffect()는 PHP의 내장(GD) 함수로, 레이어 효과(layering effect)를 적용할 수 있도록 알파 블렌딩(alpha blending) 플래그를 설정합니다. 실행에 성공하면 true를, 실패하면 false를 반환합니다.

문법

bool imagelayereffect($image, $effect)

매개변수

imagelayereffect() 함수는 두 개의 매개변수 $image$effect를 받습니다.

  • $image − imagecreatetruecolor()와 같은 이미지 생성 함수가 반환하는 이미지 리소스입니다. 생성할 이미지의 크기를 지정하는 데 사용됩니다.

  • $effect − 알파 블렌딩 플래그의 값을 설정하는 매개변수로, 아래와 같은 효과 상수들을 사용합니다.

    • IMG_EFFECT_REPLACE − 픽셀 대체(pixel replacement) 모드를 설정합니다. imagealphablending() 함수에 true를 전달하는 것과 비슷한 결과를 냅니다.

    • IMG_EFFECT_ALPHABLEND − 일반적인 픽셀 블렌딩을 설정합니다. imagealphablending() 함수에 false를 전달하는 것과 동일합니다.

    • IMG_EFFECT_NORMAL − IMG_EFFECT_ALPHABLEND와 동일하게 작동합니다.

    • IMG_EFFECT_OVERLAY − 오버레이(overlay) 효과를 적용합니다. 이 경우 흰색 배경 픽셀은 흰색으로, 검은색 배경 픽셀은 검은색으로 그대로 유지되며, 회색 배경 픽셀은 전경(foreground) 픽셀의 색상을 따르게 됩니다.

    • IMG_EFFECT_MULTIPLY − 곱하기(multiply) 효과를 설정합니다.

반환 값

imagelayereffect()는 성공 시 true, 실패 시 false를 반환합니다.

참고 사항

이 함수를 사용하려면 PHP 환경에 GD 라이브러리가 활성화되어 있어야 하며, PHP 4.3.0 이상 버전에서 사용할 수 있습니다.

예제 1

<?php
    // imagecreatetruecolor() 함수로 이미지 생성
    $img = imagecreatetruecolor(700, 300);
    
    // 배경색 지정
    imagefilledrectangle($img, 0, 0, 150, 150, imagecolorallocate($img, 122, 122, 122));

    // 오버레이(OVERLAY) 알파 블렌딩 플래그 적용
    imagelayereffect($img, IMG_EFFECT_OVERLAY);

    // 색상이 다른 타원 세 개를 겹쳐 그리기
    imagefilledellipse($img, 50, 50, 40, 40, imagecolorallocate($img, 100, 255, 100));
    imagefilledellipse($img, 50, 50, 50, 80, imagecolorallocate($img, 100, 100, 255));
    imagefilledellipse($img, 50, 50, 80, 50, imagecolorallocate($img, 255, 0, 0));

    // 이미지 출력
    header('Content-type: image/png');
    imagepng($img);
    imagedestroy($img);
?>

출력 결과

PHP imagelayereffect() 함수로 알파 블렌딩 플래그 설정하고 레이어 효과 적용하기

예제 2

<?php
    // imagecreatetruecolor() 함수로 이미지 생성
    $img = imagecreatetruecolor(700, 200);

    // 배경색 지정
    imagefilledrectangle($img, 0, 0, 200, 200, imagecolorallocate($img, 122, 122, 122));

    // 픽셀 대체(REPLACE) 알파 블렌딩 플래그 적용
    imagelayereffect($img, IMG_EFFECT_REPLACE);

    // 색상이 다른 원 세 개를 겹쳐 그리기
    imagefilledellipse($img,100,100,160,160, imagecolorallocate($img,0,0,0));
    imagefilledellipse($img,100,100,140,140, imagecolorallocate($img,0,0,255));
    imagefilledellipse($img,100,100,100,100, imagecolorallocate($img,255,0,0));

    // 이미지 출력
    header('Content-type: image/png');
    imagepng($img);
    imagedestroy($img);
?>

출력 결과

PHP imagelayereffect() 함수로 알파 블렌딩 플래그 설정하고 레이어 효과 적용하기