GD PHP TEXT ALIGN 한글


function text_UCS2($text)
{
$text = mb_convert_encoding($text, "UCS-2", "UTF-8");
$data = "";
for ($i=0; $i<strlen($text); $i+=2) {
$data .= "".(ord(substr($text, $i, 1))*256+ord(substr($text, $i+1, 1))).";";
}
return $data;
}


$text = "Your Text Here한국은 언제나";
$font_size = "15";
$color = "#000000";
$font_file = '/home/chakgong/public_html/fonts/NanumPen.ttf'; //font
$image_path = "/home/chakgong/public_html/img/sample/sample_styleC.png"; // 원본 이미지 절대경로

$info = getimagesize($image_path);
if($info === false) {
return "No Image";
}
// echo $info['mime'];
$img_mime = $info['mime'];
$wrap_width = $info[0];
$img_height = $info[1];

$background = "#FFFFFF";
// $wrap_width = "130";
$alpha = 0;
$padding = 0;

// right, left, center, or justified
$align = "center";

// wrap word first
$text = wrapText($font_size, $font_file, $text, $wrap_width);

// Retrieve bounding box:
$bbox = imagettfbbox($font_size, 0, $font_file, $text);

// Determine image width and height, plus padding:
$baseline = abs($bbox[7]);
$descent = abs($bbox[1]);
$image_width = abs($bbox[0])+abs($bbox[2]) + ($padding*2) + 5;
$image_height = $baseline+$descent + ($padding*2);

// Create image
$image = imagecreatetruecolor($image_width, $image_height);

$rgb = hexToRGB($background);
$bgcolor = imagecolorallocate($image, $rgb[0], $rgb[1], $rgb[2]);

// create a solid background first
imagefill($image, 0, 0, $bgcolor);
imagealphablending($image, false);
imagesavealpha($image, true);

//테스트 이미지 입력시///////////////////////
$image = imagecreatefrompng($image_path);
imagealphablending($image, true);

// determine text color
$rgb = hexToRGB($color);
$textcolor = imagecolorallocate($image, $rgb[0], $rgb[1], $rgb[2]);

// Fix starting x and y coordinates for the text:
//새 이미지 생성시
$x = $bbox[0] + $padding;
$y = $baseline + $padding;

// print_r($bbox);
// exit;
//기존 이미지 위에 입력시////////////////////////
$x = (imagesx($image) / 2) - abs((abs($bbox[2]) - abs($bbox[0])) / 2);
$y = ($img_height - $padding)/2;

// Add TrueType text to image:
imagealphablending($image, true);

// draw the image
switch($align){
case "center":
$z = imagettftextcenter($image, $font_size, $x, $y, $textcolor, $font_file, $text);
break;
case "justified":
$z = imagettftextjustified($image, $font_size, $x, $y, $textcolor, $font_file, $text);
break;
case "right":
$z = imagettftextright($image, $font_size, $x, $y, $textcolor, $font_file, $text);
break;
case "left":
default:
$z = imagettftext($image, $font_size, 0, $x, $y, $textcolor, $font_file, $text);
}

// output the image
header('Content-Type: image/png');
imagepng($image, null, 9);

function imagettftextjustified($image, $size, $x, $y, $color, $fontfile, $text){
// Get height of single line
$rect = imagettfbbox($size, 0, $fontfile, "Tq");
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));
$h1 = $maxY - $minY;

// Get height of two lines
$rect = imagettfbbox($size, 0, $fontfile, "Tq\nTq");
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));
$h2 = $maxY - $minY;

// amount of padding that should be between each line
$vpadding = $h2 - $h1 - $h1;

// calculate the dimensions of the text itself
$frect = imagettfbbox($size, 0, $fontfile, $text);
$minX = min(array($frect[0],$frect[2],$frect[4],$frect[6]));
$maxX = max(array($frect[0],$frect[2],$frect[4],$frect[6]));
$text_width = $maxX - $minX;

// left align any line whose whitespace accounts
// for this much (percent) of the the line.
$max_whitespace_pct = 70;

$threshold = $text_width / 100 * $max_whitespace_pct;
$lines = explode("\n", $text);
foreach($lines as $line){
$rect = imagettfbbox($size, 0, $fontfile, $line);
$minX = min(array($rect[0],$rect[2],$rect[4],$rect[6]));
$maxX = max(array($rect[0],$rect[2],$rect[4],$rect[6]));
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));

$line_width = $maxX - $minX;
$line_height = $maxY - $minY;

$words = explode(" ", $line);
$word_width = 0;
$word_data = array();
foreach($words as $word){
$rect = imagettfbbox($size, 0, $fontfile, $word);
$minX = min(array($rect[0],$rect[2],$rect[4],$rect[6]));
$maxX = max(array($rect[0],$rect[2],$rect[4],$rect[6]));
$width = $maxX - $minX;
$word_width += $width;
$word_data[] = array(
"width" => $width,
"word" => $word
);
}

$available_space = $text_width - $word_width;
$_x = $x;
if($threshold > $available_space && count($words) > 1){
$total_spaces = count($words) - 1;
$space_size = $available_space / $total_spaces;
foreach($word_data as $word){
imagettftext($image, $size, 0, $_x, $y, $color, $fontfile, $word['word']);
$_x += ($space_size + $word['width']);
}
}else{
imagettftext($image, $size, 0, $_x, $y, $color, $fontfile, $line);
}
$y += ($line_height + $vpadding);
}
return $rect;
}

function imagettftextcenter($image, $size, $x, $y, $color, $fontfile, $text){
// Get height of single line
$rect = imagettfbbox($size, 0, $fontfile, "Tq");
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));
$h1 = $maxY - $minY;

// Get height of two lines
$rect = imagettfbbox($size, 0, $fontfile, "Tq\nTq");
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));
$h2 = $maxY - $minY;

// amount of padding that should be between each line
$vpadding = $h2 - $h1 - $h1;

// calculate the dimensions of the text itself
$frect = imagettfbbox($size, 0, $fontfile, $text);
$minX = min(array($frect[0],$frect[2],$frect[4],$frect[6]));
$maxX = max(array($frect[0],$frect[2],$frect[4],$frect[6]));
$text_width = $maxX - $minX;

$text = explode("\n", $text);
foreach($text as $txt){
$rect = imagettfbbox($size, 0, $fontfile, $txt);
$minX = min(array($rect[0],$rect[2],$rect[4],$rect[6]));
$maxX = max(array($rect[0],$rect[2],$rect[4],$rect[6]));
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));

$width = $maxX - $minX;
$height = $maxY - $minY;

$_x = $x + (($text_width - $width) / 2);

imagettftext($image, $size, 0, $_x, $y, $color, $fontfile, $txt);
$y += ($height + $vpadding);
}

return $rect;
}

function imagettftextright($image, $size, $x, $y, $color, $fontfile, $text){
// Get height of single line
$rect = imagettfbbox($size, 0, $fontfile, "Tq");
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));
$h1 = $maxY - $minY;

// Get height of two lines
$rect = imagettfbbox($size, 0, $fontfile, "Tq\nTq");
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));
$h2 = $maxY - $minY;

// amount of padding that should be between each line
$vpadding = $h2 - $h1 - $h1;

// calculate the dimensions of the text itself
$frect = imagettfbbox($size, 0, $fontfile, $text);
$minX = min(array($frect[0],$frect[2],$frect[4],$frect[6]));
$maxX = max(array($frect[0],$frect[2],$frect[4],$frect[6]));
$text_width = $maxX - $minX;

$text = explode("\n", $text);
foreach($text as $txt){
$rect = imagettfbbox($size, 0, $fontfile, $txt);
$minX = min(array($rect[0],$rect[2],$rect[4],$rect[6]));
$maxX = max(array($rect[0],$rect[2],$rect[4],$rect[6]));
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));

$width = $maxX - $minX;
$height = $maxY - $minY;

$_x = ($x + $text_width) - $width;

imagettftext($image, $size, 0, $_x, $y, $color, $fontfile, $txt);
$y += ($height + $vpadding);
}

return $rect;
}

function hexToRGB($hex){
// Remove the # if there is one
$hex = str_replace("#", "", $hex);

// Convert the hex to rgb
if(strlen($hex) == 3){
$r = hexdec(substr($hex, 0, 1) . substr($hex, 0, 1));
$g = hexdec(substr($hex, 1, 1) . substr($hex, 1, 1));
$b = hexdec(substr($hex, 2, 1) . substr($hex, 2, 1));
}else{
$r = hexdec(substr($hex, 0, 2));
$g = hexdec(substr($hex, 2, 2));
$b = hexdec(substr($hex, 4, 2));
}

return array($r, $g, $b);
}

function wrapText($fontSize, $fontFace, $string, $width){

$ret = "";
// $arr = explode(" ", $string);
$arr = preg_split('//u', $string, null, PREG_SPLIT_NO_EMPTY);

foreach($arr as $word){
$testboxWord = imagettfbbox($fontSize, 0, $fontFace, $word);

// huge word larger than $width, we need to cut it internally until it fits the width
$len = strlen($word);
while($testboxWord[2] > $width && $len > 0){
$word = substr($word, 0, $len);
$len--;
$testboxWord = imagettfbbox($fontSize, 0, $fontFace, $word);
}

$teststring = $ret . ' ' . $word;
$testboxString = imagettfbbox($fontSize, 0, $fontFace, $teststring);
if($testboxString[2] > $width){
$ret.=($ret == "" ? "" : "\n") . text_UCS2($word);
}else{
$ret.=($ret == "" ? "" : '') . text_UCS2($word);
}
}

return $ret;
}


 
0
0
이 글을 페이스북으로 퍼가기 이 글을 트위터로 퍼가기 이 글을 카카오스토리로 퍼가기 이 글을 밴드로 퍼가기

PHP

번호 제목 글쓴이 날짜 조회수
17 [PHP] 구글 캡차(Captcha) 달기 - V3, V2 버전 관리자 12-12 23
16 PhpSpreadsheet 설정 샘플 코드 관리자 06-21 255
15 PHP & JavaScript 엑셀 파일 다운로드 및 업로드 구현 (PhpSpreadsheet 사용) 관리자 06-21 257
14 PHP 스크립트에서 JSON 반환하기 관리자 11-26 2,534
13 PhpSpreadsheet 설치 및 사용법 정리 관리자 11-07 551
12 PHP 프로그램에서 BULK INSERT 문장을 만드는 방법 관리자 07-07 508
11 PHP 문자 암호화하여 Form 전송하기 관리자 07-07 482
10 전화번호 체크하기(휴대전화, 유선, 대표번호 등등) 관리자 07-03 435
9 get vimeo thumb 관리자 04-14 447
8 PHP 에서 MySQL 사용하기 (연결, DB&테이블 생성, 데이터 삽입/선택) 관리자 04-13 602
7 [PHP] 특정 영역 자동 스크린샷 저장 후 가장 최신 이미지 DB 저장 관리자 03-15 519
6 GD PHP TEXT ALIGN 한글 관리자 03-14 424
5 [PHP] 이미지파일에 텍스트 넣기 (라이브러리) 관리자 03-12 756
4 PHP 이미지 워터마크(텍스트&이미지) 삽입하기 관리자 03-11 425
3 한글 종성유무에 맞는 조사(은/는/이/가/을/를/과/와) 변환 관리자 06-22 662
2 한국어 조사 처리 함수 관리자 04-08 760
1 PHP 에서 callback 함수를 이용하여서 mysql select row 함수 개발하는 방법 관리자 10-22 920