! * \brief Resizes an image converting it to the specified \a format using the specified \a quality. * \returns Returns the converted image or in case the conversion is not possible the original \a imageData. * \remarks * - In case \a maxSize is zero the original size will be kept. In this case the conversion to the specified * \a format with the specified \a quality will always be done. *
| 236 | * to true. |
| 237 | */ |
| 238 | QByteArray UtilityObject::convertImage(const QByteArray &imageData, const QSize &maxSize, const QString &format, int quality, bool force) |
| 239 | { |
| 240 | auto image = QImage::fromData(imageData); |
| 241 | auto imageChanged = false; |
| 242 | if (image.isNull()) { |
| 243 | return imageData; |
| 244 | } |
| 245 | if (maxSize.isNull()) { |
| 246 | imageChanged = true; |
| 247 | } else if (image.width() > maxSize.width() || image.height() > maxSize.height()) { |
| 248 | image = image.scaled(maxSize.width(), maxSize.height(), Qt::KeepAspectRatio, Qt::SmoothTransformation); |
| 249 | imageChanged = true; |
| 250 | if (image.isNull()) { |
| 251 | return imageData; |
| 252 | } |
| 253 | } |
| 254 | if (!imageChanged && !force) { |
| 255 | return imageData; |
| 256 | } |
| 257 | auto newData = QByteArray(); |
| 258 | auto buffer = QBuffer(&newData); |
| 259 | auto res = buffer.open(QIODevice::WriteOnly) && image.save(&buffer, format.isEmpty() ? "JPEG" : format.toUtf8().data(), quality); |
| 260 | return res ? newData : imageData; |
| 261 | } |
| 262 | |
| 263 | static QString propertyString(const QJSValue &obj, const QString &propertyName) |
| 264 | { |
no test coverage detected