Files
edu/ajax/upload_profile_image.php

166 lines
3.6 KiB
PHP

<?php
require_once __DIR__ . '/../bbs/db_conn.php';
header('Content-Type: application/json; charset=utf-8');
function response_json($data) {
echo json_encode($data, JSON_UNESCAPED_UNICODE);
exit;
}
// 세션 (실제 환경 기준)
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
$memberId = $_SESSION['member_id'] ?? '';
$sysCompCode= $_SESSION['sys_comp_code'] ?? '';
// 테스트용 (필요시 사용)
// $memberId = 'U001';
// $sysCompCode = 'COMP01';
try {
if (!$memberId || !$sysCompCode) {
response_json([
'success' => false,
'message' => '로그인 정보가 없습니다.'
]);
}
if (!isset($_FILES['profile_image'])) {
response_json([
'success' => false,
'message' => '파일이 없습니다.'
]);
}
$file = $_FILES['profile_image'];
if ($file['error'] !== UPLOAD_ERR_OK) {
response_json([
'success' => false,
'message' => '업로드 오류 발생'
]);
}
// 2차 검사: 서버에서 3MB 제한
$maxSize = 2 * 1024 * 1024; // 2MB
if ($file['size'] > $maxSize) {
response_json([
'success' => false,
'message' => '2MB 이하의 이미지 파일만 업로드 가능합니다.'
]);
}
// MIME 체크
$mime = mime_content_type($file['tmp_name']);
if (!in_array($mime, ['image/jpeg', 'image/png'])) {
response_json([
'success' => false,
'message' => 'jpg, png 파일만 업로드 가능합니다.'
]);
}
// 이미지 생성
if ($mime === 'image/jpeg') {
$srcImage = imagecreatefromjpeg($file['tmp_name']);
} else {
$srcImage = imagecreatefrompng($file['tmp_name']);
}
if (!$srcImage) {
response_json([
'success' => false,
'message' => '이미지 처리 실패'
]);
}
/*
// 원본 사이즈
$width = imagesx($srcImage);
$height = imagesy($srcImage);
// 새 이미지 생성 (png)
$newImage = imagecreatetruecolor($width, $height);
// 투명 배경 처리
imagealphablending($newImage, false);
imagesavealpha($newImage, true);
$transparent = imagecolorallocatealpha($newImage, 0, 0, 0, 127);
imagefilledrectangle($newImage, 0, 0, $width, $height, $transparent);
// 복사
imagecopy($newImage, $srcImage, 0, 0, 0, 0, $width, $height);
*/
// 원본 사이즈
$srcWidth = imagesx($srcImage);
$srcHeight = imagesy($srcImage);
// 최대 사이즈 (긴 변 기준)
$maxSize = 250;
// 비율 계산
if ($srcWidth >= $srcHeight) {
$newWidth = $maxSize;
$newHeight = intval($srcHeight * ($maxSize / $srcWidth));
} else {
$newHeight = $maxSize;
$newWidth = intval($srcWidth * ($maxSize / $srcHeight));
}
// 새 이미지 생성
$newImage = imagecreatetruecolor($newWidth, $newHeight);
// 투명 배경
imagealphablending($newImage, false);
imagesavealpha($newImage, true);
$transparent = imagecolorallocatealpha($newImage, 0, 0, 0, 127);
imagefilledrectangle($newImage, 0, 0, $newWidth, $newHeight, $transparent);
// 리사이즈 복사
imagecopyresampled(
$newImage,
$srcImage,
0, 0, 0, 0,
$newWidth, $newHeight,
$srcWidth, $srcHeight
);
// 저장 경로
$fileName = $memberId . '_' . $sysCompCode . '.png';
$savePath = __DIR__ . '/../img/profile/' . $fileName;
$saveUrl = '/img/profile/' . $fileName;
// 저장 (덮어쓰기)
if (!imagepng($newImage, $savePath)) {
response_json([
'success' => false,
'message' => '파일 저장 실패'
]);
}
// 메모리 해제
imagedestroy($srcImage);
imagedestroy($newImage);
response_json([
'success' => true,
'image_url' => $saveUrl
]);
} catch (Throwable $e) {
response_json([
'success' => false,
'message' => '프로필 업로드 중 오류가 발생했습니다.',
'error' => $e->getMessage()
]);
}