C++
C++ example for image generation using the Canvasflare API.
The Canvasflare API provides the functionality to create images from HTML, CSS, and additional rendering options. This example illustrates how to make a POST request to generate an image using C++.
Rendering an Image
Send a POST request to https://api.canvasflare.com/render?api_key=YOUR_API_KEY with the required parameters.
Parameters
html- The HTML content you wish to render, e.g.,<div>Your content</div>.css- The CSS styling for your rendered image, e.g.,.h1 { color: red }.device_scale- Modifies the pixel density of the screenshot.viewport_width- The width of the Chrome viewport.viewport_height- The height of the Chrome viewport.viewport_device- Emulates a given device's metrics and user agent.viewport_has_touch- Indicates whether the viewport supports touch events.viewport_landscape- Indicates whether the viewport is in landscape mode.viewport_mobile- Specifies whether the meta viewport tag is considered.full_page- If set to true screenshots entire webpage.selector- A CSS selector corresponding to an element in the HTML.ms_delay- Additional delay to ensure completion of JavaScript execution.user_agent- An user agent for the request.
Example Request in C++
You can use libraries like libcurl (for HTTP requests) and nlohmann/json (for JSON handling) in C++:
#include <curl/curl.h>
#include <nlohmann/json.hpp>
#include <iostream>
#include <string>
using json = nlohmann::json;
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, std::string *s) {
size_t newLength = size * nmemb;
try {
s->append((char*)contents, newLength);
return newLength;
} catch(std::bad_alloc &e) {
// handle memory problem
return 0;
}
}
int main() {
curl_global_init(CURL_GLOBAL_ALL);
CURL *curl = curl_easy_init();
if(curl) {
const std::string url = "https://api.canvasflare.com/render?api_key=YOUR_API_KEY";
json j;
j["html"] = "<div>This is something</div>";
j["css"] = ".h1 { color: red }";
j["device_scale"] = 2;
j["viewport_width"] = 640;
j["viewport_height"] = 480;
j["ms_delay"] = 500;
std::string readBuffer;
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, j.dump().c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
CURLcode res = curl_easy_perform(curl);
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
} else {
std::cout << readBuffer << std::endl; // Output response
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
Response
The API response will return a URL to the generated image that you can download or display.
You can add the parameter ?dl=true to the image URL to make the browser download the image automatically. For example:
https://api.canvasflare.com/image/8d063c49-3f15-46c1-91c7-9cf61e6758c6?dl=true
To run this C++ code, you will need to have libcurl for making HTTP requests and nlohmann/json for JSON handling. Make sure these libraries are installed and properly set up in your C++ project environment. The code is a basic example and might require additional error handling or adjustments based on your specific requirements and setup.