Skip to content

Commit

Permalink
share vertical test
Browse files Browse the repository at this point in the history
  • Loading branch information
bramses committed Sep 27, 2023
1 parent f5e6b6f commit 5484248
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
8 changes: 7 additions & 1 deletion question-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const configuration = new Configuration({
organization: process.env.OPENAI_ORG,
});

console.log("org: " + process.env.OPENAI_ORG);
const openai = new OpenAIApi(configuration);

const supabaseUrl = process.env.SUPABASE_URL;
Expand All @@ -32,6 +33,8 @@ const generateQuestion = async (row) => {
const highlight = row.text;

const prompt = `Generate a single question from this quote. The end user cannot see the quote so DO NOT use any abstract concepts like "the speaker" or "the writer" in your question. BE EXPLICIT. DO NOT ASSUME the reader has read the quote. DO NOT use passive voice and do not use passive pronouns like he/she/they/him/her etc. You can use any of who/what/where/when/why. Say nothing else.\n\nQuote:\n\n${highlight}\n\nQ:`;

console.log("prompt: " + highlight);
const completion = await openai.createChatCompletion({
messages: [
{
Expand All @@ -46,6 +49,7 @@ const generateQuestion = async (row) => {

return content;
} catch (err) {
console.log("START ERROR")
console.error(err);
console.error(err.response);
console.error(err.response.data);
Expand All @@ -54,6 +58,7 @@ const generateQuestion = async (row) => {
console.error(err.response.data.error.code);
console.error(err.response.data.error.status);
console.error(err.response.data.error.request);
console.log("END ERROR")
throw err;
}
};
Expand All @@ -66,6 +71,7 @@ const addQuestionToHighlight = async (row, question) => {
.update({ question })
.match({ id: row.id });
} catch (err) {
console.log("Error adding question to highlight");
console.error(err);
throw err;
}
Expand Down Expand Up @@ -178,7 +184,7 @@ const fetchAllRecords = async (table_name, page_size = 1000) => {
await addQuestionToHighlight(row, question);
console.log(question + " || " + row.text + " || " + row.id);
// wait 1 second between each request
//await new Promise((resolve) => setTimeout(resolve, 3500));
// await new Promise((resolve) => setTimeout(resolve, 500));
}
}
}
Expand Down
75 changes: 75 additions & 0 deletions share-pic.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,81 @@ async function overlayTextOnImageOverlay(imageUrl, text) {
}

async function overlayTextOnImage(imageUrl, text) {
try {
const canvas = createCanvas(1024, 2048); // Height is doubled to stack image and text
const ctx = canvas.getContext("2d");

// Fill the canvas with white background
ctx.fillStyle = "white";
ctx.fillRect(0, 0, canvas.width, canvas.height);

const rectWidth = 1024 * 0.92;
const rectHeight = 1024 * 0.92;
const rectX = (1024 - rectWidth) / 2;
const rectY = 1024 + (1024 - rectHeight) / 2; // Position the text block under the image

// Fetch and load the image
const img = await loadImage(imageUrl);
ctx.drawImage(img, 0, 0, 1024, 1024); // Draw the image at the top

// Word wrapping logic and text rendering
let fontSize = 80;
ctx.font = `${fontSize}px Georgia`;
let lines = [];
let blockHeight;

do {
lines = [];
const paragraphs = text.split("\n");

for (const paragraph of paragraphs) {
let currentLine = "";
const words = paragraph.split(" ");

for (let i = 0; i < words.length; i++) {
const word = words[i];
const width = ctx.measureText(currentLine + " " + word).width;
if (width < rectWidth - 162) { // ~80px padding on each side
currentLine += (currentLine ? " " : "") + word;
} else {
lines.push(currentLine);
currentLine = word;
}
}
lines.push(currentLine);
lines.push(""); // Empty line to simulate paragraph spacing
}

// Remove the last empty line
if (lines[lines.length - 1] === "") {
lines.pop();
}

// Calculate total block height
blockHeight = lines.length * (fontSize + 10); // 10px line spacing
if (blockHeight > rectHeight) {
fontSize -= 1; // Reduce the font size
ctx.font = `${fontSize}px Georgia`; // Maintain the original font for simplicity
}
} while (blockHeight > rectHeight);

// Draw each line of text in the bottom half
ctx.fillStyle = "black";
for (let i = 0; i < lines.length; i++) {
ctx.fillText(lines[i], rectX, 1024 + 20 + i * (fontSize + 10)); // Start drawing 20px below the image
}

// Convert canvas to buffer
const buffer = canvas.toBuffer();
return { buffer };
} catch (err) {
console.error(err);
return null;
}
}


async function overlayTextOnImageHorizontal(imageUrl, text) {
try {
const canvas = createCanvas(2048, 1024); // Width is doubled to accommodate the image and text
const ctx = canvas.getContext("2d");
Expand Down

0 comments on commit 5484248

Please sign in to comment.