117 lines
2.6 KiB
PHP
117 lines
2.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' => '업로드 오류 발생'
|
|
]);
|
|
}
|
|
|
|
// 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);
|
|
|
|
// 저장 경로
|
|
$fileName = $memberId . '_' . $sysCompCode . '.png';
|
|
$savePath = __DIR__ . '/../img/profile/' . $fileName;
|
|
$saveUrl = '/edu/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()
|
|
]);
|
|
} |