Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions plugins/experimental/webp_transform/ImageTransform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#include <sstream>
#include <iostream>
#include <string_view>

#include "ts/ts.h"

#include "tscpp/api/PluginInit.h"
#include "tscpp/api/GlobalPlugin.h"
#include "tscpp/api/TransformationPlugin.h"
Expand All @@ -43,6 +46,8 @@ namespace
{
GlobalPlugin *plugin;

auto webp_dbg_ctl = TSDbgCtlCreate(TAG);

enum class ImageEncoding { webp, jpeg, png, unknown };

bool config_convert_to_webp = false;
Expand Down Expand Up @@ -106,11 +111,11 @@ class ImageTransform : public TransformationPlugin
Blob output_blob;
if (_transform_image_type == ImageEncoding::webp) {
stat_convert_to_webp.increment(1);
TSDebug(TAG, "Transforming jpeg or png to webp");
TSDbg(webp_dbg_ctl, "Transforming jpeg or png to webp");
image.magick("WEBP");
} else {
stat_convert_to_jpeg.increment(1);
TSDebug(TAG, "Transforming webp to jpeg");
TSDbg(webp_dbg_ctl, "Transforming webp to jpeg");
image.magick("JPEG");
}
image.write(&output_blob);
Expand Down Expand Up @@ -173,23 +178,23 @@ class GlobalHookPlugin : public GlobalPlugin
}
}

TSDebug(TAG, "User-Agent: %s transaction_convert_to_webp: %d transaction_convert_to_jpeg: %d", ctype.c_str(),
transaction_convert_to_webp, transaction_convert_to_jpeg);
TSDbg(webp_dbg_ctl, "Content-Type: %s transaction_convert_to_webp: %d transaction_convert_to_jpeg: %d", ctype.c_str(),
transaction_convert_to_webp, transaction_convert_to_jpeg);

// If we might need to convert check to see if what the browser supports
if (transaction_convert_to_webp == true || transaction_convert_to_jpeg == true) {
std::string accept = transaction.getServerRequest().getHeaders().values("Accept");
bool webp_supported = accept.find("image/webp") != std::string::npos;
TSDebug(TAG, "Accept: %s webp_suppported: %d", accept.c_str(), webp_supported);
TSDbg(webp_dbg_ctl, "Accept: %s webp_suppported: %d", accept.c_str(), webp_supported);

if (webp_supported == true && transaction_convert_to_webp == true) {
TSDebug(TAG, "Content type is either jpeg or png. Converting to webp");
TSDbg(webp_dbg_ctl, "Content type is either jpeg or png. Converting to webp");
transaction.addPlugin(new ImageTransform(transaction, input_image_type, ImageEncoding::webp));
} else if (webp_supported == false && transaction_convert_to_jpeg == true) {
TSDebug(TAG, "Content type is webp. Converting to jpeg");
TSDbg(webp_dbg_ctl, "Content type is webp. Converting to jpeg");
transaction.addPlugin(new ImageTransform(transaction, input_image_type, ImageEncoding::jpeg));
} else {
TSDebug(TAG, "Nothing to convert");
TSDbg(webp_dbg_ctl, "Nothing to convert");
}
}

Expand All @@ -207,19 +212,18 @@ TSPluginInit(int argc, const char *argv[])
if (argc >= 2) {
std::string option(argv[1]);
if (option.find("convert_to_webp") != std::string::npos) {
TSDebug(TAG, "Configured to convert to webp");
TSDbg(webp_dbg_ctl, "Configured to convert to webp");
config_convert_to_webp = true;
}
if (option.find("convert_to_jpeg") != std::string::npos) {
TSDebug(TAG, "Configured to convert to jpeg");
TSDbg(webp_dbg_ctl, "Configured to convert to jpeg");
config_convert_to_jpeg = true;
}
if (config_convert_to_webp == false && config_convert_to_jpeg == false) {
TSDebug(TAG, "Unknown option: %s", option.c_str());
TSError("Unknown option: %s", option.c_str());
}
} else {
TSDebug(TAG, "Default configuration is to convert both webp and jpeg");
TSDbg(webp_dbg_ctl, "Default configuration is to convert both webp and jpeg");
config_convert_to_webp = true;
config_convert_to_jpeg = true;
}
Expand Down