Skip to content
This repository has been archived by the owner on Jan 2, 2023. It is now read-only.

Image: Add an option to crop on a single web element #4259

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/image/imagearguments.cc
Expand Up @@ -38,6 +38,7 @@ ImageCommandLineParser::ImageCommandLineParser(wkhtmltopdf::settings::ImageGloba
// addarg("scale-w",0,"Set width for resizing", new IntSetter(s.scale.width,"int"));
// addarg("scale-h",0,"Set height for resizing", new IntSetter(s.scale.height,"int"));

addarg("selector", 0, "Set a CSS2 selector that points to an element to crop on", new QStrSetter(s.selector, "selector"));
addarg("crop-x",0,"Set x coordinate for cropping", new IntSetter(s.crop.left,"int"));
addarg("crop-y",0,"Set y coordinate for cropping", new IntSetter(s.crop.top,"int"));
addarg("crop-w",0,"Set width for cropping", new IntSetter(s.crop.width,"int"));
Expand Down
4 changes: 4 additions & 0 deletions src/image/imagedocparts.cc
Expand Up @@ -118,5 +118,9 @@ void ImageCommandLineParser::outputExamples(Outputter * o) const {
o->verbatim("wkhtmltoimage http://www.google.com google.png\n");
o->paragraph("To convert a local HTML file to PNG:");
o->verbatim("wkhtmltoimage my.html my.png\n");
o->paragraph("To crop on a specific HTML element:");
o->verbatim("wkhtmltoimage --selector '#hplogo' https://google.com google.png\n");
o->paragraph("To crop a part of the element by an offset (use a negative offset for a margin):");
o->verbatim("wkhtmltoimage --selector '#hplogo' --crop-x 70 https://google.com google.png\n");
o->endSection();
}
28 changes: 22 additions & 6 deletions src/lib/imageconverter.cc
Expand Up @@ -166,12 +166,28 @@ void ImageConverterPrivate::pagesLoaded(bool ok) {
fail();
}

if (settings.crop.left < 0) settings.crop.left = 0;
if (settings.crop.top < 0) settings.crop.top = 0;
if (settings.crop.width < 0) settings.crop.width = 1000000;
if (settings.crop.height < 0) settings.crop.height = 1000000;
QRect rect = QRect(QPoint(0,0), loaderObject->page.viewportSize()).intersected(
QRect(settings.crop.left,settings.crop.top,settings.crop.width,settings.crop.height));
QRect rect;
if (settings.selector != "") {
QWebElement elem = frame->findFirstElement(settings.selector);
if (elem.isNull()) {
emit out.error("No element matched the selector");
fail();
return;
}
rect = elem.geometry().adjusted(
settings.crop.left == settings::CropSettings::DEFAULT ? 0 : settings.crop.left,
settings.crop.top == settings::CropSettings::DEFAULT ? 0 : settings.crop.top,
settings.crop.width == settings::CropSettings::DEFAULT ? 0 : settings.crop.width,
settings.crop.height == settings::CropSettings::DEFAULT ? 0 : settings.crop.height);

} else {
if (settings.crop.left < 0) settings.crop.left = 0;
if (settings.crop.top < 0) settings.crop.top = 0;
if (settings.crop.width < 0) settings.crop.width = 1000000;
if (settings.crop.height < 0) settings.crop.height = 1000000;
rect = QRect(QPoint(0,0), loaderObject->page.viewportSize()).intersected(
QRect(settings.crop.left,settings.crop.top,settings.crop.width,settings.crop.height));
}
if (rect.width() == 0 || rect.height() == 0) {
emit out.error("Will not output an empty image");
fail();
Expand Down
12 changes: 8 additions & 4 deletions src/lib/imagesettings.cc
Expand Up @@ -48,17 +48,20 @@ struct DLL_LOCAL ReflectImpl<ImageGlobal>: public ReflectClass {
WKHTMLTOPDF_REFLECT(in);
WKHTMLTOPDF_REFLECT(out);
WKHTMLTOPDF_REFLECT(fmt);
WKHTMLTOPDF_REFLECT(selector);
WKHTMLTOPDF_REFLECT(quality);
WKHTMLTOPDF_REFLECT(loadGlobal);
WKHTMLTOPDF_REFLECT(loadPage);
}
};

const int CropSettings::DEFAULT = std::numeric_limits<int>::min();

CropSettings::CropSettings():
left(-1),
top(-1),
width(-1),
height(-1) {}
left(DEFAULT),
top(DEFAULT),
width(DEFAULT),
height(DEFAULT) {}

ImageGlobal::ImageGlobal():
logLevel(Info),
Expand All @@ -67,6 +70,7 @@ ImageGlobal::ImageGlobal():
in(""),
out(""),
fmt(""),
selector(""),
screenWidth(1024),
screenHeight(0),
quality(94),
Expand Down
6 changes: 6 additions & 0 deletions src/lib/imagesettings.hh
Expand Up @@ -25,6 +25,7 @@
#include <logging.hh>
#include <loadsettings.hh>
#include <websettings.hh>
#include <limits>

#include <dllbegin.inc>
namespace wkhtmltopdf {
Expand All @@ -41,6 +42,8 @@ struct DLL_PUBLIC CropSettings {
int width;
//! Cropping height/h dime
int height;

static const int DEFAULT;
};

/*! \brief Class holding all user settings.
Expand Down Expand Up @@ -74,6 +77,9 @@ struct DLL_PUBLIC ImageGlobal {
QString out;
//! The output format
QString fmt;

//! Set the CSS selector of an element to crop on
QString selector;

//! Set the screen width
int screenWidth;
Expand Down