PHP를 통해 카카오 Karlo 를 이용해보자 - v2 버전
PHP를 통해 카카오 Karlo 를 이용해보자 - v2 버전
REDINFO
몇달 전 2023-11-12 23:04:57

카카오에서 제공하는 Karlo (인공지능  이미지 생성 기능)가 v2 버전으로 상향 되었다. 물론 v1도 아직까지는 사용가능하지만 기능이 한층 더 상향된 v2를 권장하기에 이번에 변경된 API 기능에 대해 소개하려고한다. 

 

카카오 Karlo v2 업그레이드 안내문 

 

본 포스팅은 앞서 작성한 포스팅 v1 포스팅을 이용하여 진행하였으며 v2에서 요청 파라미터 및 응답 파라미터에 많은 부분들이 변경되었다. 자세한 옵션등은 아래의 카카오 Karlo API 문서를 살펴보도록 하자 

 

 
Kakao Developers
카카오 API를 활용하여 다양한 어플리케이션을 개발해보세요. 카카오 로그인, 메시지 보내기, 친구 API, 인공지능 API 등을 제공합니다.
developers.kakao.com/

 

참고로 이번에 소개할 예제는 아래와 같다. 

1. 이미지 생성하기 (t2i) 
| prompt(명령어)를 입력하면 이미지를 생성해 준다. 
2. 이미지 확대하기 (upscale)
| scale 값을 입력하면 이미지를 비율에 맞게 확대해준다. 
3. 이미지 변환하기 (variations)
| image 이미지를 전송하면 다른 스타일의 이미지로 만들어준다. 
4. 이미지 편집하기 (inpainting)
| 명령어를 통해 원본 이미지와 마스킹된 영역의 이미지를 기준으로 변환해준다. 

 

| 이미지 생성 (t2i)

<?php //t2i
$reqData = array();
$reqData['prompt'] = 'A cat with white fur'; // 이미지를 묘사하는 제시어, 영문만 지원 (최대: 2048자) <필수>
$reqData['negative_prompt'] = 'korea, human'; // 이미지 생성 시 제외할 요소를 묘사하는 부정 제시어, 영문만 지원 (최대: 2048자) <선택>
$reqData['image_format'] = 'png'; // 이미지 파일 형식 (webp,jpeg,png) - 기본 webp

// CURL 통신
$url = 'https://api.kakaobrain.com/v2/inference/karlo/t2i';
$headers = array(
	'Content-Type: application/json',
	'Authorization: KakaoAK ${REST_API_KEY}',
);

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($reqData)); 
if( count($headers) > 0){ 
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
}
$res = json_decode(curl_exec($ch)); 
curl_close($ch); 

// echo '<pre>'; print_r($res); echo '</pre>'; exit;  // 출력시 

if( empty($res->images[0]->image) ){ die('<h1>실패</h1>'); }

header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // 과거 아무 때나 잡으면 됨.
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");		
header("Content-Type: image/png");
die(file_get_contents($res->images[0]->image));	

 

| 이미지 확대하기 (upscale)

<?php //upscale
$reqData = array();
$reqData['images'][] = base64_encode(file_get_contents(CORE_CONFIG['dir']['public'].'/images/test/cat.png')); // 확대할 이미지 파일 (배열)
$reqData['scale'] = 4; // 확대 배율(integer), 2 또는 4 중 하나 (기본값: 2) - scale 값을 4로 지정하더라도 가로세로 최대 2048 픽셀 크기까지만 확대 가능
$reqData['image_format'] = 'png'; // 이미지 파일 형식 (webp,jpeg,png) - 기본 webp

// CURL 통신
$url = 'https://api.kakaobrain.com/v2/inference/karlo/upscale';
$headers = array(
	'Content-Type: application/json',
	'Authorization: KakaoAK ${REST_API_KEY}',
);

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($reqData)); 
if( count($headers) > 0){ 
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
}
$res = json_decode(curl_exec($ch)); 
curl_close($ch); 
// echo '<pre>'; print_r($res); echo '</pre>'; exit;  // 출력시 

if( empty($res->images[0]) ){ die('<h1>실패</h1>'); }

header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // 과거 아무 때나 잡으면 됨.
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");		
header("Content-Type: image/png");
die(file_get_contents($res->images[0]));	

 

| 이미지 변환하기(variations)

<?php //variations
$reqData = array();
$reqData['image'] = base64_encode(file_get_contents(CORE_CONFIG['dir']['public'].'/images/test/cat.png')); // 원본 이미지 파일을 Base64 인코딩한 값	(string)
$reqData['image_format'] = 'png'; // 이미지 파일 형식 (webp,jpeg,png) - 기본 webp

// CURL 통신
$url = 'https://api.kakaobrain.com/v2/inference/karlo/variations';
$headers = array(
	'Content-Type: application/json',
	'Authorization: KakaoAK ${REST_API_KEY}',
);

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($reqData)); 
if( count($headers) > 0){ 
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
}
$res = json_decode(curl_exec($ch)); 
curl_close($ch); 
// echo '<pre>'; print_r($res); echo '</pre>'; exit;  // 출력시 

if( empty($res->images[0]->image) ){ die('<h1>실패</h1>'); }

header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // 과거 아무 때나 잡으면 됨.
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");		
header("Content-Type: image/png");
die(file_get_contents($res->images[0]->image));	

 

| 이미지 편집하기(inpainting)

<?php //inpainting
$reqData = array();
$reqData['prompt'] = 'tiger'; // 이미지를 묘사하는 제시어, 영문만 지원 (최대: 2048자) <필수>
$reqData['image'] = base64_encode(file_get_contents(CORE_CONFIG['dir']['public'].'/images/test/cat.png')); // 원본 이미지 파일을 Base64 인코딩한 값	(string)
$reqData['mask'] = base64_encode(file_get_contents(CORE_CONFIG['dir']['public'].'/images/test/mask-image.png'));; // 편집할 영역을 검정(RGB 또는 Grayscale 값 0)으로 표시한 이미지를 Base64 인코딩한 값 (string)
$reqData['image_format'] = 'png'; // 이미지 파일 형식 (webp,jpeg,png) - 기본 webp

// CURL 통신
$url = 'https://api.kakaobrain.com/v2/inference/karlo/inpainting';
$headers = array(
	'Content-Type: application/json',
	'Authorization: KakaoAK ${REST_API_KEY}',
);

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($reqData)); 
if( count($headers) > 0){ 
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
}
$res = json_decode(curl_exec($ch)); 
curl_close($ch); 

// echo '<pre>'; print_r($res); echo '</pre>'; exit;  // 출력시 

if( empty($res->images[0]->image) ){ die('<h1>실패</h1>'); }

header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // 과거 아무 때나 잡으면 됨.
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");		
header("Content-Type: image/png");
die(file_get_contents($res->images[0]->image));	

 

| 결과 

 

참고로 결과 이미지는 포스팅 글의 컨텐츠 내용만 길어지고 의미가 없을 것 같아 일부 제외 하였으니 직접 실행하여 결과를 확인해 보자.

 

이 포스트글이 도움이 되었나요?
6
그룹 목록
카테고리 연관글

Comment

댓글작성

0(500) | 1(30)