Skip to content

Commit 36ae9f7

Browse files
LiHua000deepin-bot[bot]
authored andcommitted
fix(image): detect image format by content instead of suffix
Prioritize content-based format detection to support opening images with modified file extensions. 优先通过文件内容检测格式,支持打开修改后缀的图片文件。 Log: 修复修改后缀后图片无法打开的问题 PMS: https://pms.uniontech.com/bug-view-355941.html Influence: 修改文件后缀的图片现在可以正常打开,提升用户体验。
1 parent 0e90e77 commit 36ae9f7

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

libimageviewer/unionimage/unionimage.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2020 - 2022 UnionTech Software Technology Co., Ltd.
1+
// SPDX-FileCopyrightText: 2020 - 2026 UnionTech Software Technology Co., Ltd.
22
//
33
// SPDX-License-Identifier: GPL-3.0-or-later
44

@@ -197,6 +197,9 @@ UNIONIMAGESHARED_EXPORT QString size2Human(const qlonglong bytes)
197197
}
198198
}
199199

200+
// Forward declaration for PrivateDetectImageFormat
201+
QString PrivateDetectImageFormat(const QString &filepath);
202+
200203
/**
201204
* @brief getFileFormat
202205
* @param path
@@ -206,9 +209,15 @@ UNIONIMAGESHARED_EXPORT QString size2Human(const qlonglong bytes)
206209
*/
207210
UNIONIMAGESHARED_EXPORT const QString getFileFormat(const QString &path)
208211
{
209-
QFileInfo fi(path);
210-
QString suffix = fi.suffix();
211-
return suffix;
212+
// 先尝试通过文件内容检测格式,避免修改后缀后无法正常打开图片的问题
213+
QString format = PrivateDetectImageFormat(path);
214+
if (format.isEmpty()) {
215+
// 如果内容检测失败,回退到后缀名判断
216+
QFileInfo fi(path);
217+
QString suffix = fi.suffix();
218+
return suffix;
219+
}
220+
return format;
212221
}
213222

214223
UNIONIMAGESHARED_EXPORT bool canSave(const QString &path)

0 commit comments

Comments
 (0)