Skip to content

Commit 148ea78

Browse files
committed
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: BUG-355941 Influence: 修改文件后缀的图片现在可以正常打开,提升用户体验。
1 parent 0e90e77 commit 148ea78

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

libimageviewer/unionimage/unionimage.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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)