Skip to content

Commit 27ba6dc

Browse files
authored
Merge pull request #84 from kc3hack/taro/develop
画像の圧縮ルールを変更
2 parents 19784a7 + cc8ea36 commit 27ba6dc

File tree

2 files changed

+55
-33
lines changed

2 files changed

+55
-33
lines changed

backend/src/controllers/Post/createPostHandler.ts

+18-33
Original file line numberDiff line numberDiff line change
@@ -61,45 +61,16 @@ const createPostHandler: RouteHandler<typeof createPostRoute, {}> = async (c: Co
6161
} else {
6262
// ここに圧縮処理 (jpegにしてqualityさげる.めざせ500KB)
6363
// File型からbufferへ
64-
//console.log(image);
6564
const arrayBuffer = await image.arrayBuffer();
6665
const buffer = Buffer.from(arrayBuffer);
6766
const new_file_name = 'file.jpg';
6867

69-
await sharp(buffer)
70-
.resize({ height: 1080 })
71-
.jpeg()
72-
.toBuffer()
73-
.then(async (resultBuffer) => {
74-
// BufferからFile型へ変換
75-
const file = new File([resultBuffer], new_file_name, { type: 'image/jpeg' });
76-
//console.log(file);
77-
// アップロード
78-
image_path = await uploadFile(file);
79-
//console.log(image_path);
80-
})
81-
.catch((err) => {
82-
console.error('画像圧縮エラー:', err);
83-
return c.json(
84-
{
85-
message: '投稿に失敗しました.',
86-
statusCode: 500,
87-
error: 'Internal Server Error',
88-
},
89-
500
90-
);
91-
});
92-
93-
// jpegへの変換&圧縮 (二分探索で500KB以下にする)
94-
/*
9568
await compressImage(buffer)
9669
.then(async (resultBuffer) => {
9770
// BufferからFile型へ変換
9871
const file = new File([resultBuffer], new_file_name, { type: 'image/jpeg' });
99-
console.log(file);
10072
// アップロード
10173
image_path = await uploadFile(file);
102-
//console.log(image_path);
10374
})
10475
.catch((err) => {
10576
console.error('画像圧縮エラー:', err);
@@ -112,10 +83,8 @@ const createPostHandler: RouteHandler<typeof createPostRoute, {}> = async (c: Co
11283
500
11384
);
11485
});
115-
*/
11686
}
11787

118-
//console.log(image_path);
11988
// ここからDBのpostテーブルへ情報登録
12089
const sql = `insert into ${env.POSTS_TABLE_NAME} (original, tanka, image_path, user_name, user_icon) values (:original, :tanka, :image_path, :user_name, :user_icon)`;
12190
await db.query(sql, { original, tanka, image_path, user_name, user_icon });
@@ -144,17 +113,33 @@ const createPostHandler: RouteHandler<typeof createPostRoute, {}> = async (c: Co
144113

145114
export default createPostHandler;
146115

147-
// 二分探索で500MB以下にする
116+
// 1080pに圧縮.それでも500KB超えていたら二分探索で500KB以下にする
148117
async function compressImage(inputBuffer: Buffer): Promise<Buffer> {
149118
let minQuality = 1;
150119
let maxQuality = 100;
151120
let bestQuality = maxQuality;
152121
let bestBuffer: Buffer | null = null;
153122
const targetFileSize = 500 * 1024; // 500KB
154123

124+
// jpegに変換して,それ以外何もしなくても500KB以下か?
125+
const compressedJpegBuffer = await sharp(inputBuffer).jpeg().toBuffer();
126+
if (compressedJpegBuffer.length <= targetFileSize) {
127+
return compressedJpegBuffer;
128+
}
129+
130+
// 1080pにして500KB以下か?
131+
const compressed1080pBuffer = await sharp(inputBuffer).resize({ height: 1080 }).jpeg().toBuffer();
132+
if (compressed1080pBuffer.length <= targetFileSize) {
133+
return compressed1080pBuffer;
134+
}
135+
136+
// 二分探索
155137
while (minQuality <= maxQuality) {
156138
const midQuality = Math.floor((minQuality + maxQuality) / 2);
157-
const compressedBuffer = await sharp(inputBuffer).jpeg({ quality: midQuality }).toBuffer();
139+
const compressedBuffer = await sharp(inputBuffer)
140+
.resize({ height: 1080 })
141+
.jpeg({ quality: midQuality })
142+
.toBuffer();
158143

159144
// サイズがtargetFileSizeより大きければ,maxQualityを小さく
160145
if (compressedBuffer.length > targetFileSize) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { z } from '@hono/zod-openapi';
2+
3+
// リクエストの型
4+
export const getMiyabiRankingSchema = z.object({
5+
limit: z.number().openapi({
6+
example: 10,
7+
description: '取得する投稿の数',
8+
}),
9+
my_icon: z.string().optional().openapi({
10+
example: 'https://avatars.githubusercontent.com/u/131171129?v=4',
11+
description: 'git hubのアイコンURL',
12+
}),
13+
post_id: z.string().optional().openapi({
14+
example: 'cb3adc47-eba3-11ef-9ce7-0242ac130002',
15+
description: '投稿id',
16+
}),
17+
});
18+
19+
// postのスキーマ
20+
export const postSchema = z.object({
21+
rank: z.number(),
22+
id: z.string(),
23+
original: z.string(),
24+
tanka: z.array(z.string()),
25+
image_path: z.string(),
26+
created_at: z.string(),
27+
user_name: z.string(),
28+
user_icon: z.string(),
29+
miyabi_count: z.number(),
30+
is_miyabi: z.boolean(),
31+
});
32+
33+
// レスポンスの型
34+
export const getMiyabiRankingResponseSchema = z.object({
35+
message: z.string(),
36+
posts: z.array(postSchema),
37+
});

0 commit comments

Comments
 (0)