VTS Auto Editorial AI:用 HTML5 Canvas 與 PHP GD 打造零依賴 1200x750 WebP 資訊圖及 SERP 工作室
Building a Zero-Dependency 1200x750 WebP Infographic & SERP Studio with HTML5 Canvas and PHP GD
Viettechsoft 工程團隊打造開源 VTS Auto Editorial AI 視覺引擎,透過 HTML5 Canvas 與 PHP 8.2 GD 在瀏覽器及伺服器端生成 1200x750 WebP 資訊圖。以 quality = 88 匯出的圖片通常為 28 KB 至 45 KB。瀏覽器端毋須上傳文字或圖片資料,亦無須依賴第三方 SaaS。
Starting a modern content-heavy WordPress build often hits the same Core Web Vitals bottleneck: editors upload oversized 2400x1600 PNGs or rely on heavy SaaS image APIs, hurting Largest Contentful Paint (LCP) and missing Google Discover's recommended 1200px+ wide 16:10 / 16:9 aspect ratio.
While building high-speed, mobile-first WordPress architectures for SMEs at Viettechsoft, our engineering team wanted a lightweight, open-source way to generate crisp 1200x750 WebP featured infographics with deterministic typography, zero third-party SaaS lock-in, and sub-second LCP delivery.
In this post, we will walk through the dual-runtime architecture behind our open-source VTS Auto Editorial AI visual engine — running identically inside a zero-upload browser and on the server via native PHP 8.2 GD.
- Why 1200x750 WebP Hits the Sweet Spot for SEO & Core Web Vitals
Google Discover and OpenGraph cards require high-resolution featured images at least 1200px wide (max-image-preview:large). At 1200x750 (8:5 ratio):
Zero Layout Shift (CLS = 0): Every single post thumbnail shares identical width="1200" height="750" intrinsic dimensions across archive grids and single headers.
Tiny Payload (< 45 KB): Clean vector-style cards with subtle radial gradients compress extraordinarily well in lossy/near-lossless WebP (quality = 88), routinely landing between 28 KB and 45 KB — loading in < 0.5s even on mid-tier 4G connections.
Readable SERP & Social Previews: Pairing a concise 4–8 word visual headline with two KPI metric cards turns generic blog thumbnails into scannable technical summaries.
- Client-Side Rendering with Zero Server Uploads (HTML5 Canvas -> WebP)
For editors drafting inside a browser Side Panel (or testing layouts in our live GitHub Pages 1200x750 WebP Studio), the entire graphic is rendered locally using the 2D Canvas API and exported via HTMLCanvasElement.toBlob().
Here is the core word-wrapping and WebP export pattern that prevents multi-line headlines (including Vietnamese UTF-8 diacritics) from overflowing card boundaries:
javascript
/**
* Wrap UTF-8 headline text cleanly within maxWidth and maxLines on a 2D Canvas.
*/
function drawWrappedHeadline(ctx, text, x, y, maxWidth, lineHeight, maxLines = 3) {
const words = text.trim().split(/\s+/);
const lines = [];
let current = '';
for (const word of words) {
const candidate = current ? `${current} ${word}` : word;
if (ctx.measureText(candidate).width > maxWidth && current) {
lines.push(current);
current = word;
if (lines.length === maxLines - 1) break;
} else {
current = candidate;
}
}
if (current && lines.length < maxLines) {
lines.push(current);
}
lines.forEach((line, idx) => {
ctx.fillText(line, x, y + idx * lineHeight);
});
return y + lines.length * lineHeight;
}
/**
* Export a 1200x750 <canvas> directly as a downloadable .webp Blob.
*/
function exportCanvasAsWebP(canvas, slug = 'featured-infographic') {
canvas.toBlob(
(blob) => {
if (!blob) return;
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = `${slug}-1200x750.webp`;
link.click();
setTimeout(() => URL.revokeObjectURL(url), 1500);
},
'image/webp',
0.88
);
}
Because canvas.toBlob(..., 'image/webp', 0.88) executes natively in Chromium and Firefox engines, zero user text or image bytes ever leave the browser tab.
- Server-Side Fallback in WordPress Using Native PHP 8.2 GD
When running unattended scheduled drafts via WP-Cron, there is no browser DOM available. Instead of requiring ImageMagick binaries or external headless browsers that shared hosts block, native PHP GD (imagecreatetruecolor + imagettftext + imagewebp) produces deterministic WebP files right inside wp-content/uploads/:
php
<?php
function vtsae_render_local_webp_card( string $headline, string $output_path, string $font_file ): bool {
if ( ! function_exists( 'imagewebp' ) || ! is_readable( $font_file ) ) {
return false;
}
$width = 1200;
$height = 750;
$canvas = imagecreatetruecolor( $width, $height );
// Deep navy background + accent panel
$bg_navy = imagecolorallocate( $canvas, 15, 29, 68 );
$card_bg = imagecolorallocate( $canvas, 248, 250, 252 );
$text_main = imagecolorallocate( $canvas, 255, 255, 255 );
imagefill( $canvas, 0, 0, $bg_navy );
imagefilledrectangle( $canvas, 680, 120, 1140, 630, $card_bg );
// Render crisp TrueType headline
imagettftext( $canvas, 34, 0, 64, 240, $text_main, $font_file, $headline );
$saved = imagewebp( $canvas, $output_path, 88 );
imagedestroy( $canvas );
return $saved;
}
Once written to disk, wp_insert_attachment() and set_post_thumbnail() attach the .webp media item and populate _wp_attachment_image_alt automatically so accessibility and image-search metadata are never left blank.
- Passing Strict WordPress.org & Manifest V3 Store Audits
Shipping the same editorial engine across WordPress, Chrome Web Store, Microsoft Edge Add-ons, and Firefox AMO surfaced several practical compliance lessons worth sharing:
Zero innerHTML in Browser Extensions: Modern Mozilla AMO automated linters flag dynamic HTML string injection. Parsing sanitized fragments via new DOMParser().parseFromString(markup, 'text/html') and cloning nodes into the target container keeps the Side Panel fast and warning-free.
No Heredoc (<<<) in WordPress Plugin Check: WordPress.org's automated Plugin Check (PCP) rejects heredoc/nowdoc strings under WordPress Coding Standards. Building structured multi-line AI prompts with implode( "\n", array( ... ) ) passes static checks cleanly.
Bring-Your-Own-Key (BYOK) Privacy: Storing the user's Gemini API key exclusively in chrome.storage.local (or WordPress options) with direct HTTPS requests to generativelanguage.googleapis.com avoids routing customer content through any intermediary proxy server.
Try the Open-Source Code & Live WebP Studio
Everything discussed above is open-source (GPL-2.0-or-later) and free to inspect, fork, or self-host:
GitHub Repository: github.com/viettechsoft/vts-auto-editorial-ai
Interactive Browser Demo (No Login): viettechsoft.github.io/vts-auto-editorial-ai
Homepage: viettechsoft.com
Architecture & Setup Docs: VTS Auto Editorial AI Documentation
Chrome & Edge Extensions: Available on the Chrome Web Store and Microsoft Edge Add-ons
If you have questions about tuning PHP GD typography for non-Latin scripts or keeping WordPress Core Web Vitals at 99–100/100 in production, drop a comment below!
來源:DEV Community · dev.to