ปรับการอัปโหลรูป Profile
This commit is contained in:
parent
728f45e489
commit
d4696409b2
1 changed files with 82 additions and 21 deletions
|
|
@ -105,8 +105,7 @@ const itemsMenu = computed(() => {
|
||||||
if (
|
if (
|
||||||
leaveReason.value ===
|
leaveReason.value ===
|
||||||
"(พ้นจากราชการด้วยสาเหตุ: ได้รับโทษทางวินัย ให้ออกจากราชการไว้ก่อน)" ||
|
"(พ้นจากราชการด้วยสาเหตุ: ได้รับโทษทางวินัย ให้ออกจากราชการไว้ก่อน)" ||
|
||||||
leaveReason.value ===
|
leaveReason.value === "(พ้นจากราชการด้วยสาเหตุ: ลาออกจากราชการ)"
|
||||||
"(พ้นจากราชการด้วยสาเหตุ: ลาออกจากราชการ)"
|
|
||||||
) {
|
) {
|
||||||
return (
|
return (
|
||||||
baseItemsMenu.value?.filter(
|
baseItemsMenu.value?.filter(
|
||||||
|
|
@ -140,7 +139,7 @@ const activeImage = ref<any | null>(null);
|
||||||
const images = ref<any[]>([]);
|
const images = ref<any[]>([]);
|
||||||
const imagesAlldata = ref<any[]>([]);
|
const imagesAlldata = ref<any[]>([]);
|
||||||
input.type = "file";
|
input.type = "file";
|
||||||
input.accept = ".jpg,.png,.tif,.pic";
|
input.accept = ".jpg,.png,.tif,.pic,.jfif";
|
||||||
|
|
||||||
input.addEventListener("change", (e) => {
|
input.addEventListener("change", (e) => {
|
||||||
profileFile.value = (e.currentTarget as HTMLInputElement).files?.[0];
|
profileFile.value = (e.currentTarget as HTMLInputElement).files?.[0];
|
||||||
|
|
@ -156,31 +155,96 @@ function imageActive(n: any) {
|
||||||
activeImage.value = n;
|
activeImage.value = n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ฟังก์ชันสำหรับปรับขนาดภาพ
|
||||||
|
function resizeImage(file: File): Promise<File> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const img = new Image();
|
||||||
|
const reader = new FileReader();
|
||||||
|
|
||||||
|
reader.onload = (e) => {
|
||||||
|
img.src = e.target!.result as string; // แปลงเป็น string
|
||||||
|
img.onload = () => {
|
||||||
|
const canvas = document.createElement("canvas");
|
||||||
|
const ctx = canvas.getContext("2d");
|
||||||
|
const width = 150;
|
||||||
|
const height = 200;
|
||||||
|
|
||||||
|
// ปรับขนาดภาพ
|
||||||
|
canvas.width = width;
|
||||||
|
canvas.height = height;
|
||||||
|
|
||||||
|
if (ctx) {
|
||||||
|
// ตั้งค่าสีพื้นหลังเป็นสีขาว
|
||||||
|
ctx.fillStyle = "white";
|
||||||
|
ctx.fillRect(0, 0, width, height); // วาดสี่เหลี่ยมสีขาวที่เต็ม canvas
|
||||||
|
|
||||||
|
// ตรวจสอบว่า ctx ไม่เป็น null
|
||||||
|
const imgAspectRatio = img.width / img.height;
|
||||||
|
const targetAspectRatio = width / height;
|
||||||
|
|
||||||
|
let drawWidth: number, drawHeight: number;
|
||||||
|
|
||||||
|
if (imgAspectRatio > targetAspectRatio) {
|
||||||
|
drawWidth = width;
|
||||||
|
drawHeight = width / imgAspectRatio;
|
||||||
|
} else {
|
||||||
|
drawHeight = height;
|
||||||
|
drawWidth = height * imgAspectRatio;
|
||||||
|
}
|
||||||
|
|
||||||
|
const xOffset = (width - drawWidth) / 2;
|
||||||
|
const yOffset = (height - drawHeight) / 2;
|
||||||
|
|
||||||
|
ctx.drawImage(img, xOffset, yOffset, drawWidth, drawHeight);
|
||||||
|
|
||||||
|
// แปลง canvas เป็น Blob
|
||||||
|
canvas.toBlob((blob) => {
|
||||||
|
if (blob) {
|
||||||
|
resolve(new File([blob], file.name, { type: file.type })); // สร้าง File ใหม่จาก Blob
|
||||||
|
} else {
|
||||||
|
reject(new Error("Failed to convert canvas to blob."));
|
||||||
|
}
|
||||||
|
}, "image/jpeg");
|
||||||
|
} else {
|
||||||
|
reject(new Error("Failed to get canvas context."));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
img.onerror = (err) => reject(err);
|
||||||
|
};
|
||||||
|
reader.readAsDataURL(file);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const newProfileFile = ref<any>(null);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ฟังก์ชันอัปโหลด
|
* ฟังก์ชันอัปโหลด
|
||||||
*/
|
*/
|
||||||
function uploadImg() {
|
async function uploadImg() {
|
||||||
|
showLoader();
|
||||||
|
newProfileFile.value = await resizeImage(profileFile.value);
|
||||||
|
closeImage();
|
||||||
http
|
http
|
||||||
.post(config.API.orgProfileAvatarbyType(empType.value), {
|
.post(config.API.orgProfileAvatarbyType(empType.value), {
|
||||||
profileId: empType.value == "" ? profileId.value : undefined,
|
profileId: empType.value == "" ? profileId.value : undefined,
|
||||||
profileEmployeeId: empType.value !== "" ? profileId.value : undefined,
|
profileEmployeeId: empType.value !== "" ? profileId.value : undefined,
|
||||||
})
|
})
|
||||||
.then((res) => {
|
.then(async (res) => {
|
||||||
fileName.value = res.data.result.avatarName;
|
fileName.value = res.data.result.avatarName;
|
||||||
uploadProfile(res.data.result.avatar);
|
await uploadProfile(res.data.result.avatar);
|
||||||
})
|
})
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
messageError($q, e);
|
messageError($q, e);
|
||||||
})
|
hideLoader();
|
||||||
.finally(() => {});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ฟังก์ชันสร้าง path อัปโหลไฟล์
|
* ฟังก์ชันสร้าง path อัปโหลไฟล์
|
||||||
* @param path
|
* @param path
|
||||||
*/
|
*/
|
||||||
function uploadProfile(path: string) {
|
async function uploadProfile(path: string) {
|
||||||
http
|
await http
|
||||||
.post(config.API.fileByPath(path), {
|
.post(config.API.fileByPath(path), {
|
||||||
replace: true,
|
replace: true,
|
||||||
fileList: [
|
fileList: [
|
||||||
|
|
@ -189,15 +253,12 @@ function uploadProfile(path: string) {
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
.then((res) => {
|
.then(async (res) => {
|
||||||
uploadUrl.value = res.data[fileName.value].uploadUrl;
|
uploadUrl.value = res.data[fileName.value].uploadUrl;
|
||||||
uploadFileURL(uploadUrl.value, profileFile.value);
|
await uploadFileURL(uploadUrl.value, newProfileFile.value);
|
||||||
closeImage();
|
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
messageError($q, err);
|
messageError($q, err);
|
||||||
})
|
|
||||||
.finally(() => {
|
|
||||||
hideLoader();
|
hideLoader();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -207,7 +268,7 @@ function uploadProfile(path: string) {
|
||||||
* @param uploadUrl path อัปโหลไฟล์
|
* @param uploadUrl path อัปโหลไฟล์
|
||||||
* @param file ไฟล์
|
* @param file ไฟล์
|
||||||
*/
|
*/
|
||||||
function uploadFileURL(uploadUrl: string, file: any) {
|
async function uploadFileURL(uploadUrl: string, file: any) {
|
||||||
showLoader();
|
showLoader();
|
||||||
axios
|
axios
|
||||||
.put(uploadUrl, file, {
|
.put(uploadUrl, file, {
|
||||||
|
|
@ -215,8 +276,8 @@ function uploadFileURL(uploadUrl: string, file: any) {
|
||||||
"Content-Type": file.type,
|
"Content-Type": file.type,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(async () => {
|
||||||
fetchProfile(profileId.value);
|
await fetchProfile(profileId.value);
|
||||||
success($q, "อัปโหลดไฟล์สำเร็จ");
|
success($q, "อัปโหลดไฟล์สำเร็จ");
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
|
|
@ -231,8 +292,8 @@ function uploadFileURL(uploadUrl: string, file: any) {
|
||||||
* ฟังก์ชันดึงข้อมูลรูปโปรไฟล์
|
* ฟังก์ชันดึงข้อมูลรูปโปรไฟล์
|
||||||
* @param id โปรไฟล์
|
* @param id โปรไฟล์
|
||||||
*/
|
*/
|
||||||
function fetchProfile(id: string) {
|
async function fetchProfile(id: string) {
|
||||||
http
|
await http
|
||||||
.get(config.API.fileByFile("ทะเบียนประวัติ", "โปรไฟล์", id, fileName.value))
|
.get(config.API.fileByFile("ทะเบียนประวัติ", "โปรไฟล์", id, fileName.value))
|
||||||
.then(async (res) => {
|
.then(async (res) => {
|
||||||
profilePicture.value = res.data.downloadUrl;
|
profilePicture.value = res.data.downloadUrl;
|
||||||
|
|
@ -1028,7 +1089,7 @@ onMounted(async () => {
|
||||||
<!-- Dialog เลือก Image -->
|
<!-- Dialog เลือก Image -->
|
||||||
<q-dialog v-model="dialogImage" persistent>
|
<q-dialog v-model="dialogImage" persistent>
|
||||||
<q-card style="width: 100vw; max-width: 60vw">
|
<q-card style="width: 100vw; max-width: 60vw">
|
||||||
<DialogHeader :tittle="'เลือกรูปภาพ'" :close="closeImage" />
|
<DialogHeader :tittle="'เลือกรูปภาพ (150 x 200 px)'" :close="closeImage" />
|
||||||
|
|
||||||
<q-separator />
|
<q-separator />
|
||||||
<q-card-section class="col-12 row">
|
<q-card-section class="col-12 row">
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue