PHP php GD 도형 그리기 함수 총정리
페이지 정보
작성자본문
Summary 8 : PHP Graphics, Image Processing
==========================================
1. 선 그리기
boolean imageline(integer image, integer start_x, integer start_y, integer end_x,
integer end_y, integer color)
<?php
header("Content-type: image/gif");
$image=imagecreate(200,200);
$colorWhite=imagecolorallocate($image,255,255,255);
$colorMagenta=imagecolorallocate($image,255,0,255);
imagefill($image,0,0,$colorWhite);
imageline($image,50,60,150,160,$colorMagenta);
imageline($image,150,60,50,160,$colorMagenta);
imagegif($image);
?>
2. 원 그리기
boolean imagearc(integer image, integer center_x, integer center_y,
integer width, integer height, integer start, integer end, integer color)
// 호(arc) 그림
<?php
header("Content-type: image/gif");
$image=imagecreate(200,200);
$colorWhite=imagecolorallocate($image,255,255,255);
$colorMagenta=imagecolorallocate($image,255,0,255);
imagefill($image,0,0,$colorWhite);
imagearc($image,100,100,80,80,0,360,$colorMagenta);
imagegif($image);
?>
<?php
header("Content-type: image/gif");
$image=imagecreate(200,200);
$colorWhite=imagecolorallocate($image,255,255,255);
$colorMagenta=imagecolorallocate($image,255,0,255);
$colorBlue=imagecolorallocate($image,0,0,255);
imagefill($image,0,0,$colorWhite);
imagearc($image,100,100,100,100,0,360,$colorMagenta);
imagearc($image,100,100,70,70,0,80,$colorBlue);
imagegif($image);
?>
* 채워진 원 그리기
boolean imagefilledarc ( resource $image , int $cx , int $cy , int $width ,
int $height , int $start , int $end , int $color , int $style )
$style : A bitwise OR of the following possibilities:
1. IMG_ARC_PIE
2. IMG_ARC_CHORD
3. IMG_ARC_NOFILL
4. IMG_ARC_EDGED
<?php
header("Content-type: image/gif");
$image=imagecreate(200,200);
$colorWhite=imagecolorallocate($image,255,255,255);
$colorMagenta=imagecolorallocate($image,255,0,255);
imagefill($image,0,0,$colorWhite);
imagefilledarc($image,100,100,80,80,0,360,$colorMagenta,IMG_ARC_PIE);
imagegif($image);
?>
* 여러개의 채워진 동심원 그리기
<?php
header("Content-type: image/gif");
$image=imagecreate(500,500);
$c[0] = imagecolorallocate($image,255,255,255);
imagefill($image,0,0,$c[0]);
$c[1] = imagecolorallocate($image,255,0,0);
$c[2] = imagecolorallocate($image,0,255,0);
$c[3] = imagecolorallocate($image,0,0,255);
$c[4] = imagecolorallocate($image,255,255,0);
$c[5] = imagecolorallocate($image,255,0,255);
$c[6] = imagecolorallocate($image,0,255,255);
$c[7] = imagecolorallocate($image,0,0,0);
for($i=6;$i>0;$i--) {
$s = $i*80;
imagefilledarc($image,250,250,$s,$s,0,360,$c[$i],
IMG_ARC_PIE);
}
imagegif($image);
?>
3. 사각형 그리기
imagerectangle(integer image, integer top_left_x, integer top_left_y, integer
bottom_right_x, integer bottom_right_y, integer color)
// 직사각형
<?php // image rectangle
header("Content-type: image/gif");
$size = 300;
$im=imagecreate($size,$size);
$wh = imagecolorallocate($im,255,255,255);
$black = imagecolorallocate($im,0,0,0);
imagefill($im,0,0,$wh);
imagerectangle($im,70,70,230,230,$black);
imagegif($im);
?>
* 채워진 사각형(여러개 동심)
imagefilledrectangle(integer image, integer top_left_x, integer top_left_y,
integer bottom_right_x, integer bottom_right_y, integer color)
// 색으로 채워진 직사각형
<?php
header("Content-type: image/gif");
$size = 500;
$n = 8;
$image=imagecreate($size,$size);
$c[0] = imagecolorallocate($image,255,255,255);
imagefill($image,0,0,$c[0]);
$c[1] = imagecolorallocate($image,255,0,0);
$c[2] = imagecolorallocate($image,0,255,0);
$c[3] = imagecolorallocate($image,0,0,255);
$c[4] = imagecolorallocate($image,255,255,0);
$c[5] = imagecolorallocate($image,255,0,255);
$c[6] = imagecolorallocate($image,255,255,0);
$c[7] = imagecolorallocate($image,0,255,255);
$c[8] = imagecolorallocate($image,0,0,0);
$cc = $size/2;
$rs = intval($size/$n);
for($i=$n; $i>0; $i--) {
$s = $cc - $i*$rs/2;
$e = $cc + $i*$rs/2;
imagefilledrectangle($image,$s,$s,$e,$e,$c[$i]);
}
imagegif($image);
?>
4. 색 채우기
boolean imagefill(integer image, integer x, integer y, integer color)
// 지정된 색으로 채우기
<?php // image collored rectangles
// look at the difference between imagefill & imagefilledrectangle
header("Content-type: image/gif");
$size = 300;
$im=imagecreate($size,$size);
$wh = imagecolorallocate($im,255,255,255);
$re = imagecolorallocate($im,255,0,0);
$ye = imagecolorallocate($im,255,255,0);
$bl = imagecolorallocate($im,0,0,255);
$gr = imagecolorallocate($im,0,255,0);
$black = imagecolorallocate($im,0,0,0);
imagefill($im,0,0,$wh);
imagerectangle($im,0,0,299,299,$black);
imagefilledrectangle($im,30,30,$size-30,$size-30,$bl);
imagerectangle($im,90,90,210,210,$black);
imagefill($im,150,150,$gr);
imagerectangle($im,150,170,230,230,$black);
imagefill($im,151,195,$re);
imagefilledrectangle($im,15,50,160,130,$ye);
imagegif($im);
?>
5. 문자(글자)열 넣기
boolean imagestring(integer image, integer font, integer x, integer y, string text, integer color)
// 문자열 넣기
<?php
header("Content-type: image/gif");
$size = 500;
$n = 8;
$image=imagecreate($size,$size);
$c[0] = imagecolorallocate($image,255,255,255);
imagefill($image,0,0,$c[0]);
$c[1] = imagecolorallocate($image,255,0,0);
$c[2] = imagecolorallocate($image,0,255,0);
$c[3] = imagecolorallocate($image,0,0,255);
$c[4] = imagecolorallocate($image,255,255,0);
$c[5] = imagecolorallocate($image,255,0,255);
$c[6] = imagecolorallocate($image,255,255,0);
$c[7] = imagecolorallocate($image,0,255,255);
$c[8] = imagecolorallocate($image,0,0,0);
$cc = $size/2;
$rs = intval($size/$n);
for($i=$n; $i>0; $i--) {
$s = $cc - $i*$rs/2;
$e = $cc + $i*$rs/2;
imagefilledrectangle($image,$s,$s,$e,$e,$c[$i]);
}
imagestring($image,5,5,5,"Computer Science",$c[0]);
imagegif($image);
?>
<?php //ipimg.php
$img_number = imagecreate(140,25);
$backcolor = imagecolorallocate($img_number,255,255,255);
$textcolor = imagecolorallocate($img_number,50,104,28);
imagefill($img_number,0,0,$backcolor);
$number = "IP - $_SERVER[REMOTE_ADDR]";
Imagestring($img_number,3,0,5,$number,$textcolor);
header("Content-type: image/jpeg");
imagejpeg($img_number);
?>
6. 다각형 그리기
boolean imagepolygon(integer image, array points, integer number, integer color)
// 다각형 그리기
boolean imagefilledpolygon(integer image, array points, integer number, integer color)
// 채워진 다각형
7. 지정된 그림으로부터 이미지 불러오기, 편집
지정된 크기의 이미지 생성, 아래는 그림으로부터 생성.
integer imagecreatefromjpeg(string filename)
integer imagecreatefromgif(string filename)
integer imagecreatefrompng(string filename)
8. Truefont 이용
array imagettfbbox ( float $size , float $angle , string $fontfile , string $text )
This function calculates and returns the bounding box in pixels for a TrueType text.
size : The font size. Depending on your version of GD, this should be specified as the pixel size (GD1) or point size (GD2).
angle :
Angle in degrees in which text will be measured.
fontfile : The name of the TrueType font file (can be a URL). Depending on which version of the GD library that PHP is using, it may attempt to search for files that do not begin with a leading '/' by appending '.ttf' to the filename and searching along a library-defined font path.
text : The string to be measured.
Report a bug Return Values
imagettfbbox() returns an array with 8 elements representing four points making the bounding box of the text on success and FALSE on error. key contents
0 lower left corner, X position
1 lower left corner, Y position
2 lower right corner, X position
3 lower right corner, Y position
4 upper right corner, X position
5 upper right corner, Y position
6 upper left corner, X position
7 upper left corner, Y position
The points are relative to the text regardless of the angle, so "upper left" means in the top left-hand corner seeing the text horizontally.
<?php
header("Content-type: image/png");
$image = imagecreate(200, 200);
$size = 40;
$angle = 30;
$startX = 50;
$startY = 100;
// flood square with red
$colorRed = imagecolorallocate($image, 0xFF, 0x00, 0x00);
imagefill($image, 10, 10, $colorRed);
$colorBlack = imagecolorallocate($image, 0, 0, 0);
//get bounding box
$Box = imagettfbbox($size, $angle, "GOTHIC.TTF", "PHP");
//move bounding box to starting point (100,100)
for($index = 0; $index < count($Box); $index += 2) {
$Box[$index] += $startX;
$Box[$index+1] += $startY;
//print("$Box[$index] $Box[$index+1]");
}
//draw bounding box
imagepolygon($image, $Box, count($Box)/2, $colorBlack);
imagettftext($image, $size, $angle,
$startX, $startY, $colorBlack,
"GOTHIC.TTF", "PHP");
imagepng($image);
?>
9. 이미지 회전 : 이미지를 주어진 각도 만큼 회전(시계방향)
$newimage = imagerotate($image, $angle, $bgColour);
$image=원본 이미지, $angle=회전 각도, $bgColour=배경색
10. 예제 프로그램
http://jongp.com/php/gr
http://info.skuniv.ac.kr/~jong/php/ex/gr
1) 정사각형 반복 그리기
<?php // image squares1b.php
//
header("Content-type: image/gif");
$size = 300;
$ns = 5; // the nuumber of squares
$inc = $size / $ns; // increment size
$im=imagecreate($size,$size);
$wh = imagecolorallocate($im,255,255,255);
$re = imagecolorallocate($im,255,0,0);
$ye = imagecolorallocate($im,255,255,0);
$bl = imagecolorallocate($im,0,0,255);
$gr = imagecolorallocate($im,0,255,0);
$black = imagecolorallocate($im,0,0,0);
imagefill($im,0,0,$wh);
for ($i=0; $i<$ns; $i++) {
$p1 = $i * $inc;
$p2 = $p1 + $inc;
imagerectangle($im,$p1,$p1,$p2,$p2,$bl);
// imagefilledrectangle($im,$p1,$p1,$p2,$p2,$bl);
}
imagerectangle($im,0,0,$size-1,$size-1,$black);
imagegif($im);
?>
2) 색깔을 달리하여 정사각형 반복 그리기
<?php // image squares1e.php
//
header("Content-type: image/gif");
$size = 300;
$ns = 6; // the nuumber of squares
$inc = $size / $ns; // increment size
$im=imagecreate($size,$size);
$c = array();
$c[0] = imagecolorallocate($im,0,0,0);
$c[1] = imagecolorallocate($im,255,255,255);
$c[2] = imagecolorallocate($im,255,0,0);
$c[3] = imagecolorallocate($im,255,255,0);
$c[4] = imagecolorallocate($im,0,0,255);
$c[5] = imagecolorallocate($im,0,255,0);
imagefill($im,0,0,$c[1]);
for ($i=0; $i<$ns; $i++) {
for ($j=0; $j<$ns; $j++) {
$p1x = $j * $inc;
$p1y = $i * $inc;
$p2x = $p1x + $inc;
$p2y = $p1y + $inc;
$cc = $c[($i+$j)%6];
imagefilledrectangle($im,$p1x,$p1y,$p2x,$p2y,$cc);
}
}
imagerectangle($im,0,0,$size-1,$size-1,$c[0]);
imagegif($im);
?>
댓글목록
등록된 댓글이 없습니다.
