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;
}