2018년 5월 22일 화요일

[PHP예제] 142 이미지에 텍스트 추가하기




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
// 기존의 이미지 파일을 설정함
$fileName = __DIR__ . '/test.jpg';
if( !file_exists($fileName) )
{
  die('파일이 존재하지 않음');
}

$image = imagecreatefromjpeg($fileName);

// 입력 요소를 설정함
// 글자 크기
$size = 24;

// 각도 설정(왼쪽에서 오른족으로 수평이0, 플러스: 좌표를 축으로 반시계 방향쪽)
$angle = 0;

// 좌표(왼쪽 위 모서리가 0, 0)
$x = 50;
$y = 50;

// 글자색을 설정
$textColor = imagecolorallocate($image, 128, 0, 0);

// 글꼴파일(적절하게 경로 설정)
$font = __DIR__ . '/NanumGothic.ttf';
if( !file_exists($font) )
{
  die('폰트 파일이 존재하지 않음');
}

// 텍스트를 추가함
$string = '테스트 글쓰기 Wooguy 81';
imagettftext($image, $size, $angle, $x, $y, $textColor, $font, $string);

// Content-Type 헤더를 전송함
header('Content-type: image/jpeg');
// IE가 Content-Type 헤더를 무시하지 않게 함
header('X-Content-Type-Optoins: nosniff');

imagejpeg($image);

// 이미지를 삭제하고 메모리 점유를 해제함
imagedestory($image);



  • 이미지에 점을 그리기
    • imagesetpixel()
      • 제2인수 - X좌표
      • 제3인수 - Y좌표
      • 제4인수 - 색
      • imagesetpixel($image, $x, $y, $color);



댓글 없음:

댓글 쓰기