gd 라이브러리 줄바꿈 해볼만큼 해봤습니다.
본문
안녕하세요 고수님 중생을 도와주십시오.. Orz
현재 이미지위에 텍스트 쓰는 것은 잘 됩니다...
그런데 텍스트가 길면 줄바꿈이 되지 않습니다.
열심히 해보았으나 도무지 되지 않습니다.
- - - - - - - - - -- - - - - - - -- - -
▼ 이미지 위에 텍스트 쓰기 소스
<?php
include_once('./_common.php');
$img_path = G5_BBS_PATH.'/textimg/image.jpg';// 원본 이미지 절대경로
$watermark_text = "동해물과 백두산이 마르고 닳도록 하느님이 보우하사 우리나라만세 무궁화 삼천리 화려강산 대한사람 대한으로 길이 보전하세 남산위에 저 소나무"; // 워터마크 텍스트
$watermark_font = G5_BBS_PATH.'/textimg/Nanum.ttf'; //폰트 절대경로
$fontSize = 72; //폰트사이즈
// 텍스트 삽입
add_watermark_text($img_path, $fontSize, $watermark_text, $watermark_font);
function add_watermark_text($image_path, $fontSize, $text, $font) {
$array_img_chk = array("jpg", "jpeg", "png", "gif", "bmp");
// 이미지 확장자
$img_ext = explode(".", strrev($image_path));
$img_ext = strrev($img_ext[0]);
$img_ext = strtolower($img_ext);
// 이미지 파일인 경우에만 진행
if(in_array($img_ext, $array_img_chk)) {
if($img_ext == 'jpg' || $img_ext == 'jpeg')
$create_img = imagecreatefromjpeg($image_path);
if($img_ext == 'png')
$create_img = imagecreatefrompng($image_path);
if($img_ext == 'gif')
$create_img = imagecreatefromgif($image_path);
if($img_ext == 'bmp')
$create_img = imagecreatefromwbmp($image_path);
if($create_img) {
imagealphablending($create_img, true);
// 워터마크 폰트 색상 (RGB 값)
$color = imagecolorallocate($create_img, 225, 225, 225);
imagettftext($create_img, $fontSize, 0, 2, 600, $color, $font, $text);
/* imagettftext 설명 */
// 원본 이미지 리소스 : $create_img
// 폰트사이즈 : $fontSize
// 폰트 각도 : 0
// 폰트 위치 x : 2
// 폰트 위치 y : 20
// 텍스트 색상 : $watermark_color (rgb값)
// 텍스트 폰트 : $font
// 텍스트 내용 : $text
//header("Content-type: image/jpeg");
imagepng($create_img, $image_path);
imagedestroy($create_img);
}
}
}
?>
▼ gd라이브러리 줄바꿈 소스도 가지고 있으나 도무지 합쳐지지가 않습니다
////////////////////////////////////줄바꾸기//
// 각 라인별 쓰여질 문자열
$buff = "";
// 문자열의 길이
$strLen = strlen($text);
// 폰트가 뿌려질 X,Y 위치
$posX = 20;
//$posY = 0;
// 기본 영문자의 폰트 실제 높이 구하기
$fontInfo = imagettfbbox($fontSize, $angle, $fontFile, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
//$fontWidth = $fontInfo[2] - $fontInfo[0];
$fontHeight = $fontInfo[1] + ($fontInfo[5] * -1);
$image_height = imagesy($imgRes); // 글자가 써질 영역의 크기를 구함
$posY = ($image_height - $fontSize) / 2; //- $fontHeight * 2) / 2;
for ($i = 0; $i < $strLen; $i++) { // (1)
$buff .= $text{$i}; // (2)
$buffInfo = imagettfbbox($fontSize, $angle, $fontFile, $buff); // (3)
$buffWidth = $buffInfo[2] - $buffInfo[0];
$buffHeight = $buffInfo[1] + ($buffInfo[5] * -1);
//echo $buff . " : " . $buffWidth. " : " . $buffHeight . "<br>";
// $buff의 문자열이 이미지폭 보다 크면 출력 후 다음줄에 쓸 준비
if ($buffWidth > imagesx($imgRes) - 10 ) { // (4) //posX값이 20이라서 절반인 10 여백
$buff = mb_substr($buff, 0, -1, "UTF-8"); // (5)
imagettftext($imgRes, $fontSize, $angle, $posX, $posY, $white, $fontFile, $buff); // (6)
$buff = ""; // (7)
$posY += $fontHeight; // (7)
$i--; // (7)
}
else { // (8)
// 마지막 문자열에 도달하면 마지막 버퍼 전송
if ($i >= $strLen - 1)
imagettftext($imgRes, $fontSize, $angle, $posX, $posY, $white, $fontFile, $buff);
}
}
!-->!-->
답변 1
https://stackoverflow.com/questions/6166261/using-the-imagettftext-function-with-multiple-lines
https://stackoverflow.com/questions/3825226/multi-byte-safe-wordwrap-function-for-utf-8
참고하세요.
답변을 작성하시기 전에 로그인 해주세요.