>  기사  >  백엔드 개발  >  PHP의 GD 라이브러리 운영 가이드

PHP의 GD 라이브러리 운영 가이드

王林
王林원래의
2023-05-20 14:40:403646검색

1.GD도서관이란?

GD 라이브러리는 다양한 이미지 형식을 생성하고 처리하기 위한 라이브러리 함수 집합으로, PHP에서 가장 일반적으로 사용되는 이미지 처리 라이브러리 중 하나입니다.

2. GD 라이브러리 설치

CentOS/RedHat에 GD 라이브러리 설치

1. PHP의 GD 확장 라이브러리 설치

yum php-gd 설치

2. 웹 서버 다시 시작

service httpd restart

3. PHP에서 지원하는 GD 라이브러리 버전을 확인하세요

php -i | grep -i gd

Ubuntu/Debian

1에 GD 라이브러리를 설치하세요. php5-gd 모듈

apt-get update && apt- php5-gd 설치

2. 웹 서버 다시 시작

service apache2 restart

3. PHP

php -i | grep -i gd

3에서 지원하는 GD 라이브러리 버전을 확인하세요.

1. 이미지 생성

1) 200X200 픽셀의 검정색 이미지 생성
$image = imagecreate(200,200);
$black = imagecolorallocate($image,0,0,0);
imagefill($image,0,0 ,$black);

2) 이미지에 텍스트 추가
$white = imagecolorallocate($image,255,255,255);
$text = 'Hello, GD!';
imagettftext($image,20,0,70,100,$ white,'arial.ttf' ,$text);

3) 이미지를 파일로 저장
imagepng($image,'test.png');

4) 메모리 해제
imagedestroy($image);

2. 이미지 처리

1) 이미지 확대
$src_image = imagecreatefrompng('test.png');
$src_width = Imagesx($src_image);
$src_height = Imagesy($src_image);
$new_height = $src_height * 0.5;
$new_image = imagecreatetruecolor($new_width,$new_height);
imagecopyresampled($new_image,$src_image,0,0,0,0,$new_width,$new_height,$src_width,$src_height );
imagepng($new_image ,'test-resize.png');

2) 테두리 추가

$border_color = imagecolorallocate($new_image,128,128,128);
imageRectangle($new_image,0,0,$new_width-1, $new_height-1,$border_color );
imagepng($new_image,'test-bordered.png');

3) 잘린 이미지

$cropped_image = imagecrop($new_image,['x'=>40,'y '=>40,' 너비'=>100,'높이'=>100]);
imagepng($cropped_image,'test-cropped.png');

4) 흐린 이미지

$blurred_image = imagefilter ($new_image,IMG_FILTER_GAUSSIAN_BLUR);
imagepng($blurred_image,'test-blurred.png');

3. 이미지 요소 조작

1) 픽셀 RGB 값 가져오기

$pixel = imagecolorat($new_image,50,50) ;
$red = ($ pixel >> 16) & 0xFF;
$green = ($pixel >> 8) & 0xFF;
$blue = $pixel & 0xFF;

2) 픽셀 RGB 값 수정

$new_color = imagecolorallocate($ new_image,255,0,0);
imagesetpixel($new_image,50,50,$new_color);
imagepng($new_image,'test-pixel.png');

3) 채우기 image

$fill_color = imagecolorallocate( $new_image,0,255,0);
imagefill($new_image,0,0,$fill_color);
imagepng($new_image,'test-filled.png');

4. GD 라이브러리

1. 워터마크 처리

1) 텍스트 워터마크 추가

$watermark_text = 'COPYRIGHT';
$font_size = 20;
$font_color = imagecolorallocate($new_image,0,0,0);
imagettftext($ new_image,$font_size,0, 10,20,$font_color,'arial.ttf',$watermark_text);
imagepng($new_image,'test-watermark.png');

2) 이미지 워터마크 추가

$watermark_image = imagecreatefrompng('watermark.png' );
$watermark_width = 이미지x($watermark_image);
$watermark_height = Imagesy($watermark_image);
$pos_x = ($new_width - $watermark_width) / 2;
$pos_y = ($new_height - $watermark_height) / 2;
imagecopy($new_image,$watermark_image,$pos_x,$pos_y,0,0,$watermark_width,$watermark_height);
imagepng($new_image,'test-watermark.png');

2. 그리기 작업

1 ) 직선 그리기

$line_color = imagecolorallocate($new_image,0,0,255);
imageline($new_image,0,0,$new_width,$new_height,$line_color);
imagepng($ new_image,'test-line.png' );

2) 직사각형 그리기

$ret_color = imagecolorallocate($new_image,0,255,0);
imageRectangle($new_image,20,20,$new_width-20,$new_height- 20,$ret_color);
imagepng($ new_image,'test-ectangle.png');

3) 원 그리기

$circle_color = imagecolorallocate($new_image,255,0,0);
$circle_center_x = $new_width /2;
$circle_center_y = $new_height/ 2;
$circle_diameter = $new_height * 0.8;
$circle_radius = $circle_diameter / 2;
imageellipse($new_image,$circle_center_x,$circle_center_y,$circle_diameter,$circle_diameter,$circle_color );
imagepng($new_image,'test -circle.png');

V.Summary

이 글에서는 이미지 생성, 이미지 처리, 이미지 요소 운영, 워터마크 등 GD 라이브러리의 기본 작업과 고급 작업을 소개합니다. 처리, 그리기 작업 등 GD 라이브러리는 PHP 개발에서 가장 실용적인 이미지 처리 도구 중 하나이며, 이미지 확인 코드 생성, QR 코드, 차트, 포스터 생성 등에 사용할 수 있습니다. GD 라이브러리 사용 기술을 익히면 PHP 개발자가 비즈니스 요구 사항을 보다 효율적으로 완료하는 데 도움이 될 수 있습니다.

위 내용은 PHP의 GD 라이브러리 운영 가이드의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

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