사이트 내 전체검색
PHP
[php] GD Library사용하기
로빈아빠
https://cmd.kr/php/453 URL이 복사되었습니다.

본문

GD Library사용하기

Apache+PHP+MySQL+GD+Imap를 설치해 놓고도 GD를 사용하지 않았죠...

이번에 GD를 사용해 보겠습니다.
아시겠지만 GD1.6이후부터 gif방식을 지원하지 않습니다. 현제 설치한 Version이 1.8.3
혹 gif방식으로 사용하시는 분은 다음과 같은 Error을 보게 될겁니다.
"ImageGif: No GIF support in this PHP build in "
gif를 대신하여 png(Portable Network Graphics)를 사용하겠습니다.
png는 웹상에서 이미지 전송을 위해 특별히 고안된 포맷이라 합니다.
gif보다 크기와 품질이 더 우수하다고 합니다. 물론 Netscape와 IE모두 지원합니다.

간단한 예를 하나 보고 시작하겠습니다.

결과보기) gd1.php3 [ImageLine]
<?php 
Header("Content-Type:image/png"); 
//Png이미지를생성하겠다는해더를보냄 

$im=ImageCreate(100,100); 
//100X100크기의새로운이미지생성 

$black=ImageColorAllocate($im,0,0,0); 
$white=ImageColorAllocate($im,255,255,255); 
//배경색 

ImageLine($im,0,0,99,99,$white); 
//흰색의대각선그림 

ImagePng($im); 
//Png이미지생성 

ImageDestroy($im); 
//생성된이미지파기 

?> 


결과보기) gd2.php3 [Image DashedLine]
<?php 
Header("Content-Type:image/png"); 
//Png이미지를생성하겠다는해더를보냄 

$im=ImageCreate(100,100); 
//100X100크기의새로운이미지생성 

$black=ImageColorAllocate($im,0,0,0); 
$white=ImageColorAllocate($im,255,255,255); 
//배경색 

ImageDashedLine($im,0,50,99,50,$white); 
//흰색점선을그림 

ImagePng($im); 
//Png이미지생성 

ImageDestroy($im); 
//생성된이미지파기 

?> 


결과보기) gd3.php3 [Image Arc]
<?php 
Header("Content-Type:image/png"); 
//Png이미지를생성하겠다는해더를보냄 

$im=ImageCreate(100,100); 
//100X100크기의새로운이미지생성 

$black=ImageColorAllocate($im,0,0,0); 
$white=ImageColorAllocate($im,255,255,255); 
//배경색 

ImageArc($im,50,50,50,25,0,360,$white); 
//50,50을기준으로50,25의원을0도에서360도까지white색상으로그림 

ImagePng($im); 
//Png이미지생성 

ImageDestroy($im); 
//생성된이미지파기 

?> 


결과보기) gd4.php3 [ImageArc]
<?php 
Header("Content-Type:image/png"); 
//Png이미지를생성하겠다는해더를보냄 

$im=ImageCreate(100,100); 
//100X100크기의새로운이미지생성 

$black=ImageColorAllocate($im,0,0,0); 
$white=ImageColorAllocate($im,255,255,255); 
//배경색 

ImageRectangle($im,10,10,80,80,$white); 
//흰색의사각형그림 

ImagePng($im); 
//Png이미지생성 

ImageDestroy($im); 
//생성된이미지파기 

?> 


결과보기) gd5.php3 [ImagePolygon]
<?php 
Header("Content-Type:image/png"); 
//Png이미지를생성하겠다는해더를보냄 

$im=ImageCreate(100,100); 
//100X100크기의새로운이미지생성 

$black=ImageColorAllocate($im,0,0,0); 
$white=ImageColorAllocate($im,255,255,255); 
//배경색 

$points[0]=30;$points[1]=10; 
$points[2]=70;$points[3]=10; 
$points[4]=90;$points[5]=50; 
$points[6]=70;$points[7]=90; 
$points[8]=30;$points[9]=90; 
$points[10]=10;$points[11]=50; 
//좌표입력값 

ImagePolygon($im,$points,6,$white); 
//각좌표값으로다각형그리기 
ImagePng($im); 
//Png이미지생성 

ImageDestroy($im); 
//생성된이미지파기 

?> 


결과보기) gd6.php3 [ImageFilledPolygon & Rectangle]
<?php 
Header("Content-Type:image/png"); 
//Png이미지를생성하겠다는해더를보냄 

$im=ImageCreate(200,200); 
//100X100크기의새로운이미지생성 

$black=ImageColorAllocate($im,0,0,0); 
$white=ImageColorAllocate($im,255,255,255); 
//배경색 

$points[0]=30;$points[1]=10; 
$points[2]=70;$points[3]=10; 
$points[4]=90;$points[5]=50; 
$points[6]=70;$points[7]=90; 
$points[8]=30;$points[9]=90; 
$points[10]=10;$points[11]=50; 
//좌표입력값 

ImageFilledPolygon($im,$points,6,$white); 
//속이찬다각형그리기 

ImageFilledRectangle($im,125,125,175,175,$white); 
//속이찬사각형그리기 

ImagePng($im); 
//Png이미지생성 

ImageDestroy($im); 
//생성된이미지파기 

?>


결과보기) gd7.php3 [ImageFill]
<?php 
Header("Content-Type:image/png"); 
//Png이미지를생성하겠다는해더를보냄 

$im=ImageCreate(200,200); 
//100X100크기의새로운이미지생성 

$black=ImageColorAllocate($im,0,0,0); 
$white=ImageColorAllocate($im,255,255,255); 
$red=ImageColorAllocate($im,255,0,0); 
//배경색 

ImageArc($im,50,25,98,48,0,360,$white); 
//흰색원을그림 

ImageFill($im,50,50,$red); 
//바탕을red로채움 

ImagePng($im); 
//Png이미지생성 

ImageDestroy($im); 
//생성된이미지파기 

?>


결과보기) gd8.php3 [ImageFillToBorder]
<?php 
Header("Content-Type:image/png"); 
//Png이미지를생성하겠다는해더를보냄 

$im=ImageCreate(200,200); 
//100X100크기의새로운이미지생성 

$black=ImageColorAllocate($im,0,0,0); 
$white=ImageColorAllocate($im,255,255,255); 
$red=ImageColorAllocate($im,255,0,0); 
//배경색 

ImageArc($im,50,25,98,48,0,360,$white); 
//흰색원을그림 

ImageFillToBorder($im,50,25,$white,$red); 
//타원을적새으로채움,보드는흰색 

ImagePng($im); 
//Png이미지생성 

ImageDestroy($im); 
//생성된이미지파기 

?>


결과보기) gd-char1.php3 [ImageChar]
<?php 
Header("Content-Type:image/png"); 
//Png이미지를생성하겠다는해더를보냄 

$im=ImageCreate(100,100); 
//100X100크기의새로운이미지생성 

$black=ImageColorAllocate($im,0,0,0); 
$white=ImageColorAllocate($im,255,255,255); 
$red=ImageColorAllocate($im,255,0,0); 
//배경색 

$char="L"; 
//입력할문자 

ImageChar($im,5,45,40,$char,$white); 
//문자(1개)출력을한다. 

ImageCharUp($im,5,45,40,$char,$white); 
//문자(1개)90도회전출력을한다. 

ImagePng($im); 
//Png이미지생성 

ImageDestroy($im); 
//생성된이미지파기 

?>


결과보기) gd-char2.php3 [ImageString]
<?php 
Header("Content-Type:image/png"); 
//Png이미지를생성하겠다는해더를보냄 

$im=ImageCreate(800,600); 
//100X100크기의새로운이미지생성 

$black=ImageColorAllocate($im,0,0,0); 
$white=ImageColorAllocate($im,255,255,255); 
$red=ImageColorAllocate($im,255,0,0); 
//배경색 

$char="Linuxand.net"; 
//입력할문자 

ImageInterlace($im,1); 

$strart_x=400-(strlen($char)*ImageFontWidth(5)/2); 
$strart_y=300-ImageFontHeight(5)/2; 

ImageString($im,5,$strart_x,$strart_y,$char,$white); 
//문자열을출력을한다. 

$strart_x=400-ImageFontHeight(5)/2; 
$strart_y=300+(strlen($char)*ImageFontWidth(5)/2); 

ImageStringUp($im,5,$strart_x,$strart_y,$char,$white); 
//문자열을90도회전출력을한다. 

ImagePng($im); 
//Png이미지생성 

ImageDestroy($im); 
//생성된이미지파기 

?> 


GD를 이용해 만든 간단한 서버 정보보기

결과보기) gd-info.php3
<?php 

Header("Content-Type:image/png"); 

$im=ImageCreate(800,600); 

$black=ImageColorAllocate($im,0,0,0); 
$white=ImageColorAllocate($im,255,255,255); 
$red=ImageColorAllocate($im,255,0,0); 
$yellow=ImageColorAllocate($im,255,255,0); 

//메모리체크부분 
$mem=file("/proc/meminfo"); 

//물리적메모리 
$get[total]=eregi_replace("[^0-9]","",$mem[3]);//메모리합계Kb 
$get[free]=eregi_replace("[^0-9]","",$mem[4]);//여유메모리Kb 
$get[used]=$get[total]-$get[free];//사용한메모리Kb 
$get[freepercent]=round(($get[free]/$get[total])*100);//여유메모리퍼센트 
$get[usedpercent]=100-$get[freepercent];//사용한메모리퍼센트 

$get[total]=number_format($get[total]); 
$get[free]=number_format($get[free]); 
$get[used]=number_format($get[used]); 

$char="MemoryInfo"; 
ImageInterlace($im,1); 
$strart_x=100; 
$strart_y=60; 
ImageString($im,5,$strart_x,$strart_y,$char,$yellow); 

ImageRectangle($im,100,80,200,90,$yellow); 
ImageFilledRectangle($im,102,82,100+$get[usedpercent],88,$red); 

$char="Total".$get[total]."Kbyte"; 
ImageInterlace($im,1); 
$strart_x=100; 
$strart_y=100; 
ImageString($im,5,$strart_x,$strart_y,$char,$white); 

$char="Free".$get[free]."Kbyte".$get[freepercent]."%"; 
ImageInterlace($im,1); 
$strart_x=100; 
$strart_y=120; 
ImageString($im,5,$strart_x,$strart_y,$char,$white); 

$char="Used".$get[used]."Kbyte".$get[usedpercent]."%"; 
ImageInterlace($im,1); 
$strart_x=100; 
$strart_y=140; 
ImageString($im,5,$strart_x,$strart_y,$char,$white); 

//메모리체크부분 

//Swap디스크사용량 
$get[swaptotal]=eregi_replace("[^0-9]","",$mem[count($mem)-2]);//스왑합계Kb 
$get[swapfree]=eregi_replace("[^0-9]","",$mem[count($mem)-1]);//스왑여유Kb 
$get[swapused]=$get[swaptotal]-$get[swapfree];//스왑사용량Kb 
$get[swapfreepercent]=round(($get[swapfree]/$get[swaptotal])*100);//스왑여유공간퍼센트 
$get[swapusedpercent]=100-$get[swapfreepercent];//사용한스왑퍼센트 

$get[swaptotal]=number_format($get[swaptotal]); 
$get[swapfree]=number_format($get[swapfree]); 
$get[swapused]=number_format($get[swapused]); 

$char="SwapInfo"; 
ImageInterlace($im,1); 
$strart_x=400; 
$strart_y=60; 
ImageString($im,5,$strart_x,$strart_y,$char,$yellow); 

ImageRectangle($im,400,80,500,90,$yellow); 
ImageFilledRectangle($im,402,82,400+$get[swapusedpercent],88,$red); 

$char="Total".$get[swaptotal]."Kbyte"; 
ImageInterlace($im,1); 
$strart_x=400; 
$strart_y=100; 
ImageString($im,5,$strart_x,$strart_y,$char,$white); 

$char="Free".$get[swapfree]."Kbyte".$get[swapfreepercent]."%"; 
ImageInterlace($im,1); 
$strart_x=400; 
$strart_y=120; 
ImageString($im,5,$strart_x,$strart_y,$char,$white); 

$char="Used".$get[swapused]."Kbyte".$get[swapusedpercent]."%"; 
ImageInterlace($im,1); 
$strart_x=400; 
$strart_y=140; 
ImageString($im,5,$strart_x,$strart_y,$char,$white); 

//Swap디스크사용량 


$DataFile="/usr/local/apache/htdocs/counter.data"; 
if(file_exists($DataFile)) 
{ 
$fp=fopen($DataFile,'r'); 
$buffer=fread($fp,filesize($DataFile)); 
fclose($fp); 
$Temp=explode("|",$buffer); 
$Count=intval($Temp[0]); 
$LastIP=$Temp[1]; 
$Time=intval($Temp[2]); 
} 

$server_name=getenv("SERVER_NAME"); 
$server_software=getenv("SERVER_SOFTWARE"); 
$remote_ip="ConnectIP:".getenv("REMOTE_ADDR"); 
$server_count="Visit:".$Count; 

$char=$server_name; 
ImageInterlace($im,1); 
$strart_x=400-(strlen($char)*ImageFontWidth(5)/2); 
$strart_y=300-ImageFontHeight(5)/2; 
ImageString($im,5,$strart_x,$strart_y,$char,$white); 

$char=$server_software; 
ImageInterlace($im,1); 
$strart_x=400-(strlen($char)*ImageFontWidth(5)/2); 
$strart_y=320-ImageFontHeight(5)/2; 
ImageString($im,5,$strart_x,$strart_y,$char,$white); 

$char=$remote_ip; 
ImageInterlace($im,1); 
$strart_x=400-(strlen($char)*ImageFontWidth(5)/2); 
$strart_y=340-ImageFontHeight(5)/2; 
ImageString($im,5,$strart_x,$strart_y,$char,$white); 

$char=$server_count; 
ImageInterlace($im,1); 
$strart_x=400-(strlen($char)*ImageFontWidth(5)/2); 
$strart_y=360-ImageFontHeight(5)/2; 
ImageString($im,5,$strart_x,$strart_y,$char,$white); 

ImagePng($im); 
ImageDestroy($im); 

?> 

댓글목록

등록된 댓글이 없습니다.

PHP
871 (7/18P)

Search

Copyright © Cmd 명령어 18.119.118.180