>  기사  >  백엔드 개발  >  동적 이미지 처리 — php (34)

동적 이미지 처리 — php (34)

WBOY
WBOY원래의
2016-08-08 09:23:361058검색

1 PHP에서 GD 라이브러리 사용

PHP는 HTML 출력 생성에만 국한되지 않고 다양한 형식의 이미지 파일을 생성하고 조작할 수도 있습니다. PHP는 몇 가지 내장된 이미지 정보 기능을 제공하며, GD 기능 라이브러리를 사용하여 새 이미지를 생성하거나 기존 이미지를 처리할 수도 있습니다. 현재 GD2 라이브러리는 JPEG, PNG 및 WBMP 형식을 지원합니다. 그러나 GIF 형식은 더 이상 지원되지 않습니다.

• JPEG는 풍부한 색상과 그라데이션이 포함된 사진이나 이미지를 저장하는 데 일반적으로 사용되는 압축 표준의 이름입니다. 이 형식은 손실 압축을 사용합니다.

• PNG는 이미지에 무손실 압축 표준을 사용하는 휴대용 웹 이미지입니다.

• WBMP는 무선 통신 장치용으로 특별히 설계된 파일 형식입니다. 그러나 널리 사용되지는 않았습니다.

2개의 이미지 생성 단계

PHP에서 이미지를 생성하려면 아래 표시된 4단계를 완료해야 합니다.
1. 배경 이미지(캔버스라고도 함)를 생성하면 모든 후속 작업은 이 배경 이미지를 기반으로 합니다.
2. 이미지 윤곽선을 그리거나 배경에 텍스트를 입력합니다.
3. 최종 그래픽 출력
4. 리소스 공개

<span><?<span>php
</span><span>//</span><span> 创建背景图像</span>$height = <span>200</span><span>;
$width </span>= <span>200</span><span>;
$im </span>= ImageCreateTrueColor($width, $height); <span>//</span><span>建立空白背景</span>$white = ImageColorAllocate ($im, <span>255</span>, <span>255</span>, <span>255</span>);<span>//</span><span>设置绘图颜色</span>$blue = ImageColorAllocate ($im, <span>0</span>, <span>0</span>, <span>64</span><span>);
imageFill($im, </span><span>0</span>, <span>0</span>, $blue);<span>//</span><span>绘制背景</span>imageLine($im, <span>0</span>, <span>0</span>, $width, $height, $white); <span>//</span><span>画线</span>imageString($im, <span>4</span>, <span>50</span>, <span>150</span>, <span>'</span><span>Sales</span><span>'</span>, $white); <span>//</span><span>添加字串</span>header(<span>'</span><span>Content-type: image/png</span><span>'</span><span>);
imagePng($im); </span><span>//</span><span>以PNG 格式将图像输出</span><span>imageDestroy($im);
</span>?></span>
3 캔버스 관리

imagecreate--새 팔레트 기반 이미지 만들기

??resource imagecreate(intx_size, inty_size)
??이 기능은 빈 새 캔버스를 만드는 데 사용됩니다. 매개 변수는 이미지 크기이고 단위는 픽셀입니다. 256색을 지원합니다.
??imagecreatetruecolor--새 트루 컬러 이미지 만들기
??resource imagecreatetruecolor(intx_size, inty_size)
??Create 새로운 트루컬러 이미지 캔버스는 GD 2.0.1 이상이 필요하며 GIF 파일 형식과 함께 사용할 수 없습니다.
??imagedestroy--이미지 삭제
??boolimagedestroy( 리소스 이미지 )
??imagedestroy() 릴리스 및 이미지 연관된 메모리.

기타 기능

resource imagecreatefrompng( string filename )

PNG 파일에서 새 이미지를 생성하거나 URL
??resource imagecreatefromjpeg( string filename )
??JPEG 파일 또는 URL에서 새 이미지 만들기
?? 리소스 이미지createfromgif( 문자열 파일 이름
??GIF 파일 또는 URL에서 이미지 만들기
??resource imagecreatefromwbmp( 문자열 파일 이름 )
WBMP 파일 또는 URL에서 새 이미지 만들기
??intimagesx( 리소스 이미지 ) --- 이미지 너비 가져오기
??ntimagesy( 리소스 이미지 ) --- 이미지 높이 가져오기

4 색상 설정

imagecolorallocate--이미지에 색상 할당

?? 구문: intimagecolorallocate(resource image, intred, intgreen, intblue )
??imagecolorallocate()는 주어진 RGB 구성 요소로 구성된 색상을 나타내는 식별자를 반환합니다. 이미지 매개 변수는
??imagecreatetruecolor() 함수의 반환 값입니다. 빨간색, 녹색 및 파란색은 필요한 색상의 빨간색, 녹색 및 파란색 구성 요소입니다. 이러한 매개 변수는 0~255 또는 16진수 0x00~0xFF입니다. . )를 호출하여 이미지로 표현되는 이미지에 사용되는 각 색상을 생성해야 합니다.
5 이미지 생성

imagegif-- 이미지 출력 GIF 형식의 브라우저 또는 파일

?? 구문: boolimagegif(resource image [, string filename] )
??imagejpeg -- JPEG 형식 출력 이미지 브라우저 또는 파일
?? 구문: boolimagejpeg(resource image [, string filename [, intquality]] )
??imagepng-- 이미지를 PNG 형식의 브라우저 또는 파일
?? 구문: boolimagepng(resource image [,string filename] )
??imagewbmp -- WBMP 형식의 이미지를 브라우저로 출력 or file
?? 구문: boolimagewbmp(resource image [, string filename [, intforeground]] )
6 Draw Image

imagefill--영역 채우기

?? 구문: boolimagefill(resourceimage,intx,inty, intcolor)
??imagefill()은 영역을 수행합니다. 이미지 이미지의 x, y 좌표에 색을 채워줍니다(이미지의 왼쪽 상단이 0, 0 입니다). (즉, x, y 포인트와 같은 색상의 포인트와 인접한 포인트가 채워지게 됩니다.)
??imagesetpixel--단일 픽셀 그리기
?? 구문: boolimagesetpixel(resource image, intx, inty, intcolor )
??imagesetpixel() 색상을 사용하여 이미지의 x, y 좌표(이미지의 왼쪽 상단이 0, 0임)에 점을 그립니다.
??imageline--선분 그리기
?? 구문: boolimageline(resource image, intx1, inty1, intx2, inty2, intcolor )
??imageline()은 색상을 사용하여 x1, y1 좌표에서 x2, y2까지 이미지의 선분을 그립니다(이미지의 왼쪽 상단은 0, 0입니다).
imageRectangle--직사각형을 그립니다

??구문: boolimageectangle(resource image, intx1, inty1, intx2, inty2, intcol)
??imageretangle() 이미지에 col 색상으로 직사각형을 그립니다. 왼쪽 위 모서리의 좌표는 x1, y1이고 오른쪽 아래 모서리의 좌표는 x2, y2입니다. 이미지의 왼쪽 상단의 좌표는 0, 0입니다.
??imagefilled직사각형--직사각형을 그리고 채우세요
?? 구문: boolimagefilled직사각형( 리소스 이미지, intx1, inty1, intx2, inty2, intcolor )
??imagefilled직사각형()은 이미지 이미지에 색상으로 채워진 직사각형을 그립니다. 왼쪽 위 모서리의 좌표는 x1, y1이고 오른쪽 아래 모서리의 좌표는 x2, y2입니다. 0은 이미지의 가장 왼쪽 상단입니다.

imagepolygon--다각형 그리기
?? 구문: boolimagepolygon( 리소스 이미지, 배열 포인트, intnum_points, intcolor)
??imagepolygon() 이미지에 다각형을 만듭니다. points는 다각형의 각 꼭지점 좌표(즉, points[0] = x0, points[1] = y0, points[2] = x1, points[3] = y1 등)를 포함하는 PHP 배열입니다. . num_points는 총 정점 수입니다.
??imagefilledpolygon--다각형을 그리고 채우세요
?? 구문: boolimagefilledpolygon( 리소스 이미지, 배열 포인트, intnum_points, intcolor )
??imagefilledpolygon() 이미지에 채워진 다각형을 그립니다. points 매개변수는 다각형의 각 꼭지점의 x 및 y 좌표를 순서대로 포함하는 배열입니다. num_points 매개변수는 총 정점 수이며 3보다 커야 합니다.

imageellipse--타원 그리기
?? 구문: boolimageellipse( 리소스 이미지, intcx, intcy, intw, inth, intcolor )
??imageellipse() image로 표현되는 이미지에 중심이 cx, cy(이미지의 왼쪽 상단이 0, 0)인 타원을 그립니다. w와 h는 각각 타원의 너비와 높이를 지정하고 타원의 색상은 색상으로 지정됩니다.
??imagefilledellipse--타원을 그리고 채우세요
??구문: boolimagefilledellipse( 리소스 이미지, intcx, intcy, intw, inth, intcolor )
??imagefilledellipse() image로 표현되는 이미지에 cx, cy(이미지의 왼쪽 상단이 0, 0임)를 중심으로 타원을 그린다. w와 h는 각각 타원의 너비와 높이를 지정합니다. 타원은 색상으로 채워집니다. 성공하면 TRUE를, 실패하면 FALSE를 반환합니다.

imagearc--타원형 호 그리기
??boolimagearc( 리소스 이미지, intcx, intcy, intw, inth, ints, inte, intcolor )
??imagearc() cx, cy(이미지의 왼쪽 상단이 0, 0)를 중심으로 image로 표현된 이미지에 타원형 호를 그립니다. w와 h는 각각 타원의 너비와 높이를 지정하고, 시작점과 끝점은 s 및 e 매개변수를 사용하여 각도로 지정합니다. 0°는 3시 방향이고 시계 방향으로 그려집니다.
??imagefilledarc--타원형 호를 그리고 채웁니다
??boolimagefilledarc( 리소스 이미지, intcx, intcy, intw, inth, ints, inte, intcolor , intstyle )
??imagefilledarc() image로 표현되는 이미지에 cx, cy(이미지의 왼쪽 상단이 0, 0임)를 이용하여 타원형의 호를 그린다. 성공하면 TRUE를, 실패하면 FALSE를 반환합니다. w와 h는 각각 타원의 너비와 높이를 지정하고, s와 e 매개변수는 각도의 시작점과 끝점을 지정합니다. 스타일은 IMG_ARC_PIE, IMG_ARC_CHORD, IMG_ARC_NOFILL, IMG_ARC_EDGED 값의 비트별 OR(OR)일 수 있습니다. 그중 IMG_ARC_PIE와 IMG_ARC_CHORD는 상호 배타적입니다. IMG_ARC_CHORD는 시작점과 끝점을 직선으로 연결하는 반면 IMG_ARC_PIE는 원형 경계를 생성합니다(둘 다 사용하는 경우 IMG_ARC_CHORD가 적용됩니다). IMG_ARC_NOFILL은 호 또는 현에 채우기가 아닌 윤곽선만 있음을 지정합니다. IMG_ARC_EDGED는 시작점과 끝점을 중심점으로 연결하는 직선을 지정합니다. IMG_ARC_NOFILL과 함께 사용하면 파이 차트의 윤곽선을 채우지 않고 그리는 좋은 방법입니다.

6 이미지에 그리기 텍스트

imagestring--문자열을 가로로 한 줄 그리기

?? 구문: boolimagestring( 리소스 이미지, intfont, intx , inty, string s, intcol)
??imagestring()은 col 색상을 사용하여 image가 나타내는 이미지의 x, y 좌표에 문자열 s를 그립니다. 문자열의 왼쪽 모서리, 전체 이미지의 왼쪽 상단 모서리는 0,0입니다. 글꼴이 1, 2, 3, 4, 5이면 내장 글꼴이 사용됩니다.
??imagestringup--문자열을 세로로 한 줄 그리기
??구문: boolimagestringup(resource image, intfont, intx, inty, string s, intcol)
??imagestring()은 col 색상을 사용하여 image가 나타내는 이미지의 x, y 좌표에 수직으로 문자열 s를 그립니다(이미지의 왼쪽 상단은 0, 0입니다). 글꼴이 1, 2, 3, 4, 5이면 내장 글꼴이 사용됩니다.

imagechar--水平地画一个字符
??语法:boolimagechar( resource image, intfont, intx, inty, string c, intcolor )
??imagechar() 将字符串c的第一个字符画在image指定的图像中,其左上角位于x,y(图像左上角为0, 0),颜色为color。如果font是1,2,3,4 或5,则使用内置的字体(更大的数字对应于更大的字体)。
??imagecharup--垂直地画一个字符
??语法:boolimagecharup( resource image, intfont, intx, inty, string c, intcolor )
??imagecharup() 将字符c垂直地画在image指定的图像上,位于x,y(图像左上角为0, 0),颜色为color。如果font为1,2,3,4 或5,则使用内置的字体。
??imagettftext--用TrueType 字体向图像写入文本
??语法:array imagettftext( resource image, float size, float angle, intx, inty, intcolor, string fontfile, string text )

例子:

<?<span>php
$im</span>= imagecreate(<span>150</span>,<span>150</span>); <span>//</span><span>创建一个150*150的画布</span>$bg= imagecolorallocate($im, <span>255</span>, <span>255</span>, <span>255</span><span>);
</span><span>//</span><span>设置画布的背景颜色为白色</span>$black = imagecolorallocate($im, <span>0</span>, <span>0</span>, <span>0</span>); <span>//</span><span>设置一个颜色变量为黑色</span>$<span>string</span>=<span>"</span><span>LAMPBrother</span><span>"</span>; <span>//</span><span>声明一个用于在图像中输出的字符串</span>imageString($im, <span>3</span>, <span>28</span>, <span>70</span>, $<span>string</span><span>, $black);
</span><span>//</span><span>水平将字符串$string输出到图像中</span>imageStringUp($im, <span>3</span>, <span>59</span>, <span>115</span>, $<span>string</span><span>, $black);
</span><span>//</span><span>垂直由下而上输出$string到图像中</span><span>for</span>($i=<span>0</span>,$j=strlen($<span>string</span>); $i<strlen($<span>string</span>); $i++,$j--<span>){
</span><span>//</span><span>使用循环单个字符输出到图像中</span>imageChar($im, <span>3</span>, <span>10</span>*($i+<span>1</span>), <span>10</span>*($i+<span>2</span>), $<span>string</span><span>[$i], $black);
</span><span>//</span><span>向下倾斜输出每个字符</span>imageCharUp($im, <span>3</span>, <span>10</span>*($i+<span>1</span>), <span>10</span>*($j+<span>2</span>), $<span>string</span>[$i], $black); <span>//</span><span>向上倾斜输出每个字符</span><span>}
header(</span><span>'</span><span>Content-type: image/png</span><span>'</span>); <span>//</span><span>设置输出的头部标识符</span>imagepng($im); <span>//</span><span>输出PNG格式的图片</span>?>

以上就介绍了动态图像处理 — php(34),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.