GD는 이미지 생성과 조작을 위한 PHP 확장 기능입니다.
이미지 생성, 처리, 저장 등을 할 수 있고, PHP 대부분 환경에 기본 설치되어 있습니다.
php -m | grep gd // 설치 여부 확인
설치가 안 된 경우 (Linux 기준):
sudo apt install php-gd
sudo service apache2 restart
if ($_FILES['image']['error'] === UPLOAD_ERR_OK) {
$tmpName = $_FILES['image']['tmp_name'];
$saveName = 'uploads/' . basename($_FILES['image']['name']);
move_uploaded_file($tmpName, $saveName);
echo "업로드 완료";
}
GD는 이미지 형식에 따라 다른 함수를 사용합니다.
$image = imagecreatefromjpeg('path/image.jpg'); // JPEG
$image = imagecreatefrompng('path/image.png'); // PNG
$image = imagecreatefromgif('path/image.gif'); // GIF
$width = 200;
$height = 100;
$image = imagecreatetruecolor($width, $height);
// 색상 설정 (RGB)
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
// 배경 채우기
imagefill($image, 0, 0, $white);
// 텍스트 삽입
imagestring($image, 5, 10, 10, "Hello PHP", $black);
// 이미지 출력
header("Content-Type: image/png");
imagepng($image);
imagedestroy($image);
$src = imagecreatefromjpeg("original.jpg");
$width = imagesx($src);
$height = imagesy($src);
$newWidth = 200;
$newHeight = 200;
$dst = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagejpeg($dst, "resized.jpg");
imagedestroy($src);
imagedestroy($dst);
$src = imagecreatefromjpeg("original.jpg");
$crop = imagecrop($src, ['x' => 100, 'y' => 50, 'width' => 200, 'height' => 200]);
if ($crop !== FALSE) {
imagejpeg($crop, "cropped.jpg");
imagedestroy($crop);
}
imagedestroy($src);
$base = imagecreatefromjpeg("image.jpg");
$watermark = imagecreatefrompng("watermark.png");
$bw = imagesx($base);
$bh = imagesy($base);
$ww = imagesx($watermark);
$wh = imagesy($watermark);
$destX = $bw - $ww - 10;
$destY = $bh - $wh - 10;
imagecopy($base, $watermark, $destX, $destY, 0, 0, $ww, $wh);
imagejpeg($base, "watermarked.jpg");
imagedestroy($base);
imagedestroy($watermark);
$image = imagecreatefrompng("logo.png");
imagejpeg($image, "logo_converted.jpg", 90); // 품질 90%
imagedestroy($image);
$image = imagecreatefromjpeg("background.jpg");
$black = imagecolorallocate($image, 0, 0, 0);
$font = "arial.ttf";
imagettftext($image, 20, 0, 50, 100, $black, $font, "텍스트 삽입 예시");
imagejpeg($image, "texted.jpg");
imagedestroy($image);
arial.ttf 파일은 같은 경로에 있어야 합니다.
header("Content-Type: image/jpeg");
$image = imagecreatefromjpeg("photo.jpg");
imagejpeg($image);
imagedestroy($image);
function createThumbnail($srcPath, $destPath, $thumbWidth) {
$srcImage = imagecreatefromjpeg($srcPath);
$width = imagesx($srcImage);
$height = imagesy($srcImage);
$thumbHeight = floor($height * ($thumbWidth / $width));
$thumbImage = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresampled($thumbImage, $srcImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $width, $height);
imagejpeg($thumbImage, $destPath);
imagedestroy($srcImage);
imagedestroy($thumbImage);
}
PHP memory_limit 설정을 초과하면 오류 발생
업로드된 파일의 MIME 타입 검사 필요 (exif_imagetype() 또는 finfo_file())
이미지 조작 후에는 반드시 imagedestroy()로 메모리 해제
웹에 출력할 땐 header() 함수로 Content-Type 지정 필요
함수 |
설명 |
---|---|
imagecreatefromjpeg() |
JPEG 이미지 불러오기 |
imagecreatetruecolor() |
빈 이미지 생성 |
imagecopyresampled() |
리사이즈 복사 |
imagecrop() |
이미지 자르기 |
imagestring() |
텍스트 그리기 |
imagettftext() |
TrueType 텍스트 삽입 |
imagejpeg() / imagepng() |
이미지 저장 또는 출력 |
imagecolorallocate() |
색상 정의 |
imagedestroy() |
이미지 리소스 해제 |
PHP로 이미지 업로드, 처리, 출력, 보안 검사까지 모두 구현할 수 있으며, 필요 시 Imagick(Imagemagick) 같은 외부 확장도 활용할 수 있습니다.