Exemple #1
0
bool SwWindow::prepareImageForUpload(const QString& imgPath, bool isRAW, QString& caption)
{
    QImage image;

    if (isRAW)
    {
        qCDebug(KIPIPLUGINS_LOG) << "Get RAW preview " << imgPath;
        KDcraw::loadRawPreview(image, imgPath);
    }
    else
    {
        image.load(imgPath);
    }

    if (image.isNull())
        return false;

    // get temporary file name
    m_tmpPath = m_tmpDir + QFileInfo(imgPath).baseName().trimmed() + QString(".jpg");

    // rescale image if requested
    int maxDim = m_widget->m_dimensionSpB->value();

    if (m_widget->m_resizeChB->isChecked() && (image.width() > maxDim || image.height() > maxDim))
    {
        qCDebug(KIPIPLUGINS_LOG) << "Resizing to " << maxDim;
        image = image.scaled(maxDim, maxDim, Qt::KeepAspectRatio,
                                             Qt::SmoothTransformation);
    }

    qCDebug(KIPIPLUGINS_LOG) << "Saving to temp file: " << m_tmpPath;
    image.save(m_tmpPath, "JPEG", m_widget->m_imageQualitySpB->value());

    // copy meta data to temporary image
    KPMetadata meta;

    if (meta.load(imgPath))
    {
        caption = getImageCaption(meta);
        meta.setImageDimensions(image.size());
        meta.setImageProgramId("Kipi-plugins", kipiplugins_version);
        meta.save(m_tmpPath);
    }
    else
    {
        caption.clear();
    }

    return true;
}
Exemple #2
0
bool WMWindow::prepareImageForUpload(const QString& imgPath, QString& caption)
{
    QImage image;
    image.load(imgPath);


    if (image.isNull())
    {
        return false;
    }

    // get temporary file name
    m_tmpPath = m_tmpDir + QFileInfo(imgPath).baseName().trimmed() + ".jpg";

    // rescale image if requested
    int maxDim = m_widget->dimension();

    if (image.width() > maxDim || image.height() > maxDim)
    {
        kDebug() << "Resizing to " << maxDim;
        image = image.scaled(maxDim, maxDim, Qt::KeepAspectRatio,
                             Qt::SmoothTransformation);
    }

    kDebug() << "Saving to temp file: " << m_tmpPath;
    image.save(m_tmpPath, "JPEG", m_widget->quality());

    // copy meta data to temporary image
    KPMetadata meta;

    if (meta.load(imgPath))
    {
        caption = getImageCaption(imgPath);
        meta.setImageDimensions(image.size());
        meta.save(m_tmpPath);
    }
    else
    {
        caption.clear();
    }

    return true;
}
bool PiwigoTalker::addPhoto(int   albumId,
                            const QString& photoPath,
                            bool  rescale,
                            int   maxWidth,
                            int   maxHeight,
                            int   thumbDim)
{
    KUrl photoUrl = KUrl(photoPath);

    m_job     = 0;
    m_state   = GE_CHECKPHOTOEXIST;
    m_talker_buffer.resize(0);

    m_path    = photoPath;
    m_albumId = albumId;
    m_md5sum  = computeMD5Sum(photoPath);

    if (!rescale)
    {
        m_hqpath = photoPath;
        kDebug() << "Download HQ version: " << m_hqpath;
    }
    else
    {
        m_hqpath = "";
    }

    kDebug() << photoPath << " " << m_md5sum.toHex();

    QImage image;

    // Check if RAW file.
    if (KPMetadata::isRawFile(photoPath))
        KDcrawIface::KDcraw::loadDcrawPreview(image, photoPath);
    else
        image.load(photoPath);

    if (!image.isNull())
    {
        QFileInfo fi(photoPath);
        QImage thumbnail = image.scaled(thumbDim, thumbDim, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        m_thumbpath      = KStandardDirs::locateLocal("tmp", "thumb-" + KUrl(photoPath).fileName());
        thumbnail.save(m_thumbpath, "JPEG", 95);

        kDebug() << "Thumbnail to temp file: " << m_thumbpath ;

        // image file - see if we need to rescale it
        if (image.width() > maxWidth || image.height() > maxHeight)
        {
            image = image.scaled(maxWidth, maxHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        }

        m_path = KStandardDirs::locateLocal("tmp", KUrl(photoPath).fileName());
        image.save(m_path, "JPEG", 95);

        kDebug() << "Resizing and saving to temp file: " << m_path ;


        // Complete name and comment for summary sending
        m_title = fi.completeBaseName();
        m_comment = "";
        m_author = "";
        m_date    = fi.created();

        // Look in the Digikam database
        KPImageInfo info(photoUrl);
        if (info.hasTitle() && !info.title().isEmpty())
            m_title = info.title();
        if (info.hasDescription() && !info.description().isEmpty())
            m_comment = info.description();
        if (info.hasCreators() && !info.creators().isEmpty())
            m_author = info.creators().join(" / ");
        if (info.hasDate())
            m_date = info.date();
        kDebug() << "Title: " << m_title;
        kDebug() << "Comment: " << m_comment;
        kDebug() << "Author: " << m_author;
        kDebug() << "Date: " << m_date;

        // Restore all metadata with EXIF
        // in the resized version
        KPMetadata meta;
        if (meta.load(photoPath))
        {
            meta.setImageProgramId(QString("Kipi-plugins"), QString(kipiplugins_version));
            meta.setImageDimensions(image.size());
            meta.save(m_path);
        }
        else
        {
            kDebug() << "Image " << photoPath << " has no exif data";
        }
    }
    else
    {
        // Invalid image
        return false;
    }

    QStringList qsl;
    qsl.append("method=pwg.images.exist");
    qsl.append("md5sum_list=" + m_md5sum.toHex());
    QString dataParameters = qsl.join("&");
    QByteArray buffer;
    buffer.append(dataParameters.toUtf8());

    m_job = KIO::http_post(m_url, buffer, KIO::HideProgressInfo);
    m_job->addMetaData("content-type", "Content-Type: application/x-www-form-urlencoded" );
    m_job->addMetaData("customHTTPHeader", "Authorization: " + s_authToken );

    emit signalProgressInfo( i18n("Check if %1 already exists", KUrl(m_path).fileName()) );

    connect(m_job, SIGNAL(data(KIO::Job*,QByteArray)),
            this, SLOT(slotTalkerData(KIO::Job*,QByteArray)));

    connect(m_job, SIGNAL(result(KJob*)),
            this, SLOT(slotResult(KJob*)));

    emit signalBusy(true);

    return true;
}
Exemple #4
0
bool PiwigoTalker::addPhoto(int   albumId,
                            const QString& mediaPath,
                            bool  rescale,
                            int   maxWidth,
                            int   maxHeight,
                            int   quality)
{
    KUrl mediaUrl = KUrl(mediaPath);

    m_job     = 0;
    m_state   = GE_CHECKPHOTOEXIST;
    m_talker_buffer.resize(0);

    m_path    = mediaPath; // By default, m_path contains the original file
    m_tmpPath = ""; // By default, no temporary file (except with rescaling)
    m_albumId = albumId;

    m_md5sum  = computeMD5Sum(mediaPath);

    kDebug() << mediaPath << " " << m_md5sum.toHex();

    if (mediaPath.endsWith(".mp4") || mediaPath.endsWith(".MP4") ||
        mediaPath.endsWith(".ogg") || mediaPath.endsWith(".OGG") ||
        mediaPath.endsWith(".webm") || mediaPath.endsWith(".WEBM")) {
        // Video management
        // Nothing to do
    } else {
        // Image management
        QImage image;

        // Check if RAW file.
        if (KPMetadata::isRawFile(mediaPath))
            KDcrawIface::KDcraw::loadRawPreview(image, mediaPath);
        else
            image.load(mediaPath);

        if (image.isNull())
        {
            // Invalid image
            return false;
        }

        if (!rescale)
        {
            kDebug() << "Upload the original version: " << m_path;
        } else {
            // Rescale the image
            if (image.width() > maxWidth || image.height() > maxHeight)
            {
                image = image.scaled(maxWidth, maxHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
            }

            m_path = m_tmpPath = KStandardDirs::locateLocal("tmp", KUrl(mediaPath).fileName());
            image.save(m_path, "JPEG", quality);

            kDebug() << "Upload a resized version: " << m_path ;

            // Restore all metadata with EXIF
            // in the resized version
            KPMetadata meta;
            if (meta.load(mediaPath))
            {
                meta.setImageProgramId(QString("Kipi-plugins"), QString(kipiplugins_version));
                meta.setImageDimensions(image.size());
                meta.save(m_path);
            }
            else
            {
                kDebug() << "Image " << mediaPath << " has no exif data";
            }
        }
    }

    // Metadata management

    // Complete name and comment for summary sending
    QFileInfo fi(mediaPath);
    m_title   = fi.completeBaseName();
    m_comment = "";
    m_author  = "";
    m_date    = fi.created();

    // Look in the Digikam database
    KPImageInfo info(mediaUrl);

    if (info.hasTitle() && !info.title().isEmpty())
        m_title = info.title();

    if (info.hasDescription() && !info.description().isEmpty())
        m_comment = info.description();

    if (info.hasCreators() && !info.creators().isEmpty())
        m_author = info.creators().join(" / ");

    if (info.hasDate())
        m_date = info.date();

    kDebug() << "Title: " << m_title;
    kDebug() << "Comment: " << m_comment;
    kDebug() << "Author: " << m_author;
    kDebug() << "Date: " << m_date;

    QStringList qsl;
    qsl.append("method=pwg.images.exist");
    qsl.append("md5sum_list=" + m_md5sum.toHex());
    QString dataParameters = qsl.join("&");
    QByteArray buffer;
    buffer.append(dataParameters.toUtf8());

    m_job = KIO::http_post(m_url, buffer, KIO::HideProgressInfo);
    m_job->addMetaData("content-type", "Content-Type: application/x-www-form-urlencoded" );
    m_job->addMetaData("customHTTPHeader", "Authorization: " + s_authToken );

    emit signalProgressInfo( i18n("Check if %1 already exists", KUrl(mediaPath).fileName()) );

    connect(m_job, SIGNAL(data(KIO::Job*,QByteArray)),
            this, SLOT(slotTalkerData(KIO::Job*,QByteArray)));

    connect(m_job, SIGNAL(result(KJob*)),
            this, SLOT(slotResult(KJob*)));

    emit signalBusy(true);

    return true;
}
void SaveImgThread::run()
{
    // Perform saving ---------------------------------------------------------------

    QImage prev     = d->img.scaled(1280, 1024, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    QImage thumb    = d->img.scaled(160, 120,   Qt::KeepAspectRatio, Qt::SmoothTransformation);
    QByteArray prof = KPWriteImage::getICCProfilFromFile(RawDecodingSettings::SRGB);

    KPMetadata meta;
    meta.setImageProgramId(QString("Kipi-plugins"), QString(kipiplugins_version));
    meta.setImageDimensions(d->img.size());

    if (d->format != QString("JPEG"))
        meta.setImagePreview(prev);

    meta.setExifThumbnail(thumb);
    meta.setExifTagString("Exif.Image.DocumentName", QString("Scanned Image")); // not i18n
    meta.setExifTagString("Exif.Image.Make",  d->make);
    meta.setXmpTagString("Xmp.tiff.Make",     d->make);
    meta.setExifTagString("Exif.Image.Model", d->model);
    meta.setXmpTagString("Xmp.tiff.Model",    d->model);
    meta.setImageDateTime(QDateTime::currentDateTime());
    meta.setImageOrientation(KPMetadata::ORIENTATION_NORMAL);
    meta.setImageColorWorkSpace(KPMetadata::WORKSPACE_SRGB);

    KPWriteImage wImageIface;

    if (d->frmt != KSaneIface::KSaneWidget::FormatRGB_16_C)
    {
        QByteArray data((const char*)d->img.bits(), d->img.numBytes());
        wImageIface.setImageData(data, d->img.width(), d->img.height(), false, true, prof, meta);
    }
    else
    {
        // 16 bits color depth image.
        wImageIface.setImageData(d->ksaneData, d->width, d->height, true, false, prof, meta);
    }

    bool    ret = false;
    QString path;

    if(d->newUrl.isLocalFile())
        path = d->newUrl.toLocalFile();
    else
        path = d->newUrl.path();

    if (d->format == QString("JPEG"))
    {
        ret = wImageIface.write2JPEG(path);
    }
    else if (d->format == QString("PNG"))
    {
        ret = wImageIface.write2PNG(path);
    }
    else if (d->format == QString("TIFF"))
    {
        ret = wImageIface.write2TIFF(path);
    }
    else
    {
        ret = d->img.save(path, d->format.toAscii().data());
    }

    emit signalComplete(d->newUrl, ret);
}
Exemple #6
0
bool Utils::updateMetadataImageMagick(const QString& src, QString& err)
{
    QFileInfo finfo(src);
    if (src.isEmpty() || !finfo.isReadable())
    {
        err = i18n("unable to open source file");
        return false;
    }

    QImage img(src);
    QImage iptcPreview   = img.scaled(1280, 1024, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    QImage exifThumbnail = iptcPreview.scaled(160, 120, Qt::KeepAspectRatio, Qt::SmoothTransformation);

    KPMetadata meta;
    meta.load(src);
    meta.setImageOrientation(KPMetadata::ORIENTATION_NORMAL);
    meta.setImageProgramId(QString("Kipi-plugins"), QString(kipiplugins_version));
    meta.setImageDimensions(img.size());
    meta.setExifThumbnail(exifThumbnail);
    meta.setImagePreview(iptcPreview);

#if KEXIV2_VERSION >= 0x010000
    QByteArray exifData = meta.getExifEncoded(true);
#else
    QByteArray exifData = meta.getExif(true);
#endif

    QByteArray iptcData = meta.getIptc(true);
    QByteArray xmpData  = meta.getXmp();

    KTemporaryFile exifTemp;
    exifTemp.setSuffix(QString("kipipluginsexif.app1"));
    exifTemp.setAutoRemove(true);
    if ( !exifTemp.open() )
    {
        err = i18n("unable to open temp file");
        return false;
    }
    QString exifFile = exifTemp.fileName();
    QDataStream streamExif( &exifTemp );
    streamExif.writeRawData(exifData.data(), exifData.size());
    exifTemp.close();

    KTemporaryFile iptcTemp;
    iptcTemp.setSuffix(QString("kipipluginsiptc.8bim"));
    iptcTemp.setAutoRemove(true);
    iptcTemp.open();
    if ( !iptcTemp.open() )
    {
        err = i18n("Cannot rotate: unable to open temp file");
        return false;
    }
    QString iptcFile = iptcTemp.fileName();
    QDataStream streamIptc( &iptcTemp );
    streamIptc.writeRawData(iptcData.data(), iptcData.size());
    iptcTemp.close();

    KTemporaryFile xmpTemp;
    xmpTemp.setSuffix(QString("kipipluginsxmp.xmp"));
    xmpTemp.setAutoRemove(true);
    if ( !xmpTemp.open() )
    {
        err = i18n("unable to open temp file");
        return false;
    }
    QString xmpFile = xmpTemp.fileName();
    QDataStream streamXmp( &xmpTemp );
    streamXmp.writeRawData(xmpData.data(), xmpData.size());
    xmpTemp.close();

    KProcess process;
    process.clearProgram();
    process << "mogrify";

    process << "-profile";
    process << exifFile;

    process << "-profile";
    process << iptcFile;

    process << "-profile";
    process << xmpFile;

    process << src + QString("[0]");

    kDebug() << "ImageMagick Command line: " << process.program();

    process.start();

    if (!process.waitForFinished())
        return false;

    if (process.exitStatus() != QProcess::NormalExit)
        return false;

    switch (process.exitCode())
    {
        case 0:  // Process finished successfully !
        {
            return true;
            break;
        }
        case 15: //  process aborted !
        {
            return false;
            break;
        }
    }

    // Processing error !
    m_stdErr = process.readAllStandardError();
    err      = i18n("Cannot update metadata: %1", m_stdErr.replace('\n', ' '));
    return false;
}
bool FlickrTalker::addPhoto(const QString& photoPath, const FPhotoInfo& info,
                            bool sendOriginal, bool rescale, int maxDim, int imageQuality)
{
    if (m_job)
    {
        m_job->kill();
        m_job = 0;
    }

    KUrl    url(m_uploadUrl);

    // We dont' want to modify url as such, we just used the KURL object for storing the query items.
    KUrl  url2("");
    QString path = photoPath;
    MPForm  form;

    form.addPair("auth_token", m_token, "text/plain");
    url2.addQueryItem("auth_token", m_token);

    form.addPair("api_key", m_apikey, "text/plain");
    url2.addQueryItem("api_key", m_apikey);

    QString ispublic = (info.is_public == 1) ? "1" : "0";
    form.addPair("is_public", ispublic, "text/plain");
    url2.addQueryItem("is_public", ispublic);

    QString isfamily = (info.is_family == 1) ? "1" : "0";
    form.addPair("is_family", isfamily, "text/plain");
    url2.addQueryItem("is_family", isfamily);

    QString isfriend = (info.is_friend == 1) ? "1" : "0";
    form.addPair("is_friend", isfriend, "text/plain");
    url2.addQueryItem("is_friend", isfriend);

    QString safetyLevel = QString::number(static_cast<int>(info.safety_level));
    form.addPair("safety_level", safetyLevel, "text/plain");
    url2.addQueryItem("safety_level", safetyLevel);

    QString contentType = QString::number(static_cast<int>(info.content_type));
    form.addPair("content_type", contentType, "text/plain");
    url2.addQueryItem("content_type", contentType);

    QString tags = "\"" + info.tags.join("\" \"") + "\"";

    if (tags.length() > 0)
    {
        form.addPair("tags", tags, "text/plain");
        url2.addQueryItem("tags", tags);
    }

    if (!info.title.isEmpty())
    {
        form.addPair("title", info.title, "text/plain");
        url2.addQueryItem("title", info.title);
    }

    if (!info.description.isEmpty())
    {
        form.addPair("description", info.description, "text/plain");
        url2.addQueryItem("description", info.description);
    }

    QString md5 = getApiSig(m_secret, url2);
    form.addPair("api_sig", md5, "text/plain");
    QImage image;

    // Check if RAW file.
    if (KPMetadata::isRawFile(photoPath))
    {
        KDcrawIface::KDcraw::loadDcrawPreview(image, photoPath);
    }
    else
    {
        image.load(photoPath);
    }

    if (!image.isNull())
    {
        path = KStandardDirs::locateLocal("tmp", QFileInfo(photoPath).baseName().trimmed() + ".jpg");

        if (sendOriginal)
        {
            QFile imgFile(photoPath);
            imgFile.copy(path);
        }
        else
        {
            if (rescale && (image.width() > maxDim || image.height() > maxDim))
                image = image.scaled(maxDim, maxDim, Qt::KeepAspectRatio,
                                     Qt::SmoothTransformation);

            image.save(path, "JPEG", imageQuality);
        }

        // Restore all metadata.

        KPMetadata meta;

        if (meta.load(photoPath))
        {
            meta.setImageDimensions(image.size());

            // NOTE: see B.K.O #153207: Flickr use IPTC keywords to create Tags in web interface
            //       As IPTC do not support UTF-8, we need to remove it.
            meta.removeIptcTag("Iptc.Application2.Keywords", false);

            meta.setImageProgramId(QString("Kipi-plugins"), QString(kipiplugins_version));
            meta.save(path);
        }
        else
        {
            kWarning() << "(flickrExport::Image doesn't have metadata)";
        }

        kDebug() << "Resizing and saving to temp file: " << path;
    }

    if (!form.addFile("photo", path))
    {
        return false;
    }

    form.finish();

    KIO::TransferJob* job = KIO::http_post(url, form.formData(), KIO::HideProgressInfo);
    job->addMetaData("content-type", form.contentType());

    connect(job, SIGNAL(data(KIO::Job*,QByteArray)),
            this, SLOT(data(KIO::Job*,QByteArray)));

    connect(job, SIGNAL(result(KJob*)),
            this, SLOT(slotResult(KJob*)));

    m_state = FE_ADDPHOTO;
    m_job   = job;
    m_buffer.resize(0);
    emit signalBusy(true);
    return true;
}
Exemple #8
0
bool DBTalker::addPhoto(const QString& imgPath, const QString& uploadFolder, bool rescale, int maxDim, int imageQuality)
{
    if(m_job)
    {
        m_job->kill();
        m_job = 0;
    }

    emit signalBusy(true);
    MPForm form;
    QString path = imgPath;
    QImage image;

    if(KPMetadata::isRawFile(imgPath))
    {
        KDcrawIface::KDcraw::loadRawPreview(image,imgPath);
    }
    else
    {
        image.load(imgPath);
    }

    if(image.isNull())
    {
        return false;
    }

    path = KStandardDirs::locateLocal("tmp",QFileInfo(imgPath).baseName().trimmed()+".jpg");

    if(rescale && (image.width() > maxDim || image.height() > maxDim))
    {
        image = image.scaled(maxDim,maxDim,Qt::KeepAspectRatio,Qt::SmoothTransformation);
    }

    image.save(path,"JPEG",imageQuality);

    KPMetadata meta;

    if(meta.load(imgPath))
    {
        meta.setImageDimensions(image.size());
        meta.setImageProgramId("Kipi-plugins",kipiplugins_version);
        meta.save(path);
    }

    if(!form.addFile(path))
    {
        emit signalBusy(false);
        return false;
    }

    QString uploadPath = uploadFolder + KUrl(imgPath).fileName();
    QString m_url = QString("https://api-content.dropbox.com/1/files_put/dropbox/") + "/" +uploadPath;
    KUrl url(m_url);
    url.addQueryItem("oauth_consumer_key",m_oauth_consumer_key);
    url.addQueryItem("oauth_nonce", nonce);
    url.addQueryItem("oauth_signature",m_access_oauth_signature);
    url.addQueryItem("oauth_signature_method",m_oauth_signature_method);
    url.addQueryItem("oauth_timestamp", QString::number(timestamp));
    url.addQueryItem("oauth_version",m_oauth_version);
    url.addQueryItem("oauth_token",m_oauthToken);
    url.addQueryItem("overwrite","false");

    KIO::TransferJob* const job = KIO::http_post(url,form.formData(),KIO::HideProgressInfo);
    job->addMetaData("content-type","Content-Type : application/x-www-form-urlencoded");

    connect(job,SIGNAL(data(KIO::Job*,QByteArray)),
            this,SLOT(data(KIO::Job*,QByteArray)));

    connect(job,SIGNAL(result(KJob*)),
            this,SLOT(slotResult(KJob*)));

    m_state = DB_ADDPHOTO;
    m_job   = job;
    m_buffer.resize(0);
    emit signalBusy(true);
    return true;
}