문제: GD 확장이 설치되지 않아 이미지 처리 함수를 사용할 수 없는 경우
// GD 라이브러리 확인
if (!extension_loaded('gd')) {
die('GD 라이브러리가 설치되지 않았습니다.');
}
// 지원하는 이미지 타입 확인
$supported = imagetypes();
if ($supported & IMG_JPG) {
echo "JPEG 지원됨";
}
문제: 큰 이미지 처리 시 메모리 한계 초과
// 메모리 제한 증가
ini_set('memory_limit', '256M');
// 또는 이미지 크기 확인 후 처리
list($width, $height) = getimagesize($image_path);
$memory_needed = ($width * $height * 4) + (1024 * 1024);
if (memory_get_usage() + $memory_needed > ini_get('memory_limit')) {
die('메모리가 부족합니다.');
}
문제: 지원하지 않는 이미지 형식을 처리하려고 할 때
function createImageFromFile($file_path) {
$image_info = getimagesize($file_path);
switch ($image_info['mime']) {
case 'image/jpeg':
return imagecreatefromjpeg($file_path);
case 'image/png':
return imagecreatefrompng($file_path);
case 'image/gif':
return imagecreatefromgif($file_path);
default:
throw new Exception('지원하지 않는 이미지 형식입니다.');
}
}
문제: 가로세로 비율을 유지하지 않아 이미지가 왜곡되는 경우
function calculateAspectRatio($original_width, $original_height, $max_width, $max_height) {
$ratio = min($max_width / $original_width, $max_height / $original_height);
return [
'width' => (int)($original_width * $ratio),
'height' => (int)($original_height * $ratio)
];
}
$new_size = calculateAspectRatio(800, 600, 400, 300);
문제: PNG나 GIF의 투명도가 사라지는 경우
function resizeWithTransparency($source, $width, $height) {
$resized = imagecreatetruecolor($width, $height);
// 투명도 보존
imagealphablending($resized, false);
imagesavealpha($resized, true);
$transparent = imagecolorallocatealpha($resized, 255, 255, 255, 127);
imagefill($resized, 0, 0, $transparent);
imagecopyresampled($resized, $source, 0, 0, 0, 0, $width, $height, imagesx($source), imagesy($source));
return $resized;
}
문제: 이미지 파일을 읽거나 쓸 수 없는 경우
// 파일 읽기 권한 확인
if (!is_readable($source_file)) {
throw new Exception('파일을 읽을 수 없습니다.');
}
// 디렉토리 쓰기 권한 확인
$target_dir = dirname($target_file);
if (!is_writable($target_dir)) {
throw new Exception('대상 디렉토리에 쓰기 권한이 없습니다.');
}
문제: 리사이즈 후 이미지 품질이 크게 떨어지는 경우
// JPEG 품질 설정 (0-100)
imagejpeg($resized_image, $output_path, 85);
// PNG 압축 레벨 설정 (0-9)
imagepng($resized_image, $output_path, 6);
// 더 나은 리샘플링을 위해 imagecopyresampled 사용
imagecopyresampled($dest, $src, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
문제: 이미지 리소스를 해제하지 않아 메모리 누수 발생
function resizeImage($source_path, $target_path, $width, $height) {
$source = imagecreatefromjpeg($source_path);
$resized = imagecreatetruecolor($width, $height);
imagecopyresampled($resized, $source, 0, 0, 0, 0, $width, $height, imagesx($source), imagesy($source));
imagejpeg($resized, $target_path, 85);
// 메모리 해제
imagedestroy($source);
imagedestroy($resized);
}
문제: 업로드된 파일이 실제 이미지인지 검증하지 않는 경우
function validateImage($file_path) {
// MIME 타입 확인
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $file_path);
finfo_close($finfo);
$allowed_types = ['image/jpeg', 'image/png', 'image/gif'];
if (!in_array($mime, $allowed_types)) {
throw new Exception('허용되지 않는 파일 형식입니다.');
}
// 실제 이미지인지 확인
if (!getimagesize($file_path)) {
throw new Exception('유효하지 않은 이미지 파일입니다.');
}
}