From b3ccbde0d3e540453b80709002cae0630172f1dc Mon Sep 17 00:00:00 2001 From: Arno Date: Sat, 18 Jul 2015 08:52:46 +0200 Subject: Automatically convert invalid jpg pics Well, seems that's Qt's ImageReader got pickier regarding jpeg-files. If the resulting QPixmap is null and void, try to convert them to png. This introduces a new dependency to ImageMagic++. I don't like it, but it works, kinda... I saw random crashes while testing... --- helper.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'helper.cpp') diff --git a/helper.cpp b/helper.cpp index 3a1e780..ed1d31a 100644 --- a/helper.cpp +++ b/helper.cpp @@ -26,6 +26,9 @@ #include #include #include +#include +#include +#include #include @@ -313,6 +316,54 @@ namespace Helper { return QVariant(); } + PicData convertToPng(PicData data){ + if(data.at(2) != "image/jpeg"){ + return PicData(); + } + PicData retval; + QString newFn = data.at(0).toString(); + newFn.replace(QRegularExpression("(jpg|jpeg)$", QRegularExpression::CaseInsensitiveOption), "png"); + Magick::Image img; + img.read(data.at(3).toByteArray().data()); + QTemporaryFile outFile("shemovconvertXXXXXX.png"); + if(outFile.open()){ + try { + img.write(outFile.fileName().toStdString()); + } + catch(Magick::Exception &e) { + return retval; + } + outFile.rename(newFn); + QString newMd5 = md5Sum(outFile.fileName()); + QString dest = moveToArchive(outFile.fileName(), newMd5, true); + QFileInfo destFi(dest); + if(!destFi.exists()){ + return PicData(); + } + QString mt = mimeType(dest); + retval = data; + retval[0] = newFn; //Filename + retval[1] = destFi.size(); //Size + retval[2] = mt; //Mime-Type + retval[3] = dest; //Full path + retval[6] = newMd5; //md5sum + QSqlDatabase db = QSqlDatabase::database("treedb"); + int id = data.at(4).toInt(); + QSqlQuery q(db); + q.prepare("UPDATE pics SET tfilename = :fn, cmd5sum = :md5, isize = :size, tformat = :format WHERE ipicsid = :id"); + q.bindValue(":fn", newFn); + q.bindValue(":md5", newMd5); + q.bindValue(":size", destFi.size()); + q.bindValue(":format", mt); + q.bindValue(":id", id); + + if(q.exec()){ + return retval; + } + } + return PicData(); + } + Duration::Duration() : mHours(0), mMinutes(0), mSeconds(0) {} Duration::Duration(qint64 seconds){ -- cgit v1.2.3-70-g09d2