Files
tonys-sportspress-enhancements/includes/featured-image-generator.php
Anthony Correa 7fc040d87a Prefix generic function names, normalize style in featured-image-generator
- Rename generate_bisected_image, handle_image_request, serve_image,
  save_image_to_cache, add_image_generator_endpoint to tony_sportspress_*
  to avoid global namespace collisions
- Guard $team1/$team2 against null before accessing ->post_modified
- Unify the two logo-placement loops into a single foreach
- Normalize indentation to tabs throughout
- Fix missing space before => in sp-event-export normalize_request_args

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-06 08:39:09 -05:00

225 lines
6.2 KiB
PHP

<?php
/**
* SP Event featured-image generator.
*
* Auto-generates bisected team-color images for SP Events.
*
* @package Tonys_Sportspress_Enhancements
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Register the head-to-head rewrite endpoint.
*
* @return void
*/
function tony_sportspress_add_image_generator_endpoint() {
add_rewrite_endpoint( 'head-to-head', EP_ROOT, true );
}
add_action( 'init', 'tony_sportspress_add_image_generator_endpoint' );
/**
* Serve the generated matchup image on template_redirect.
*
* @return void
*/
function tony_sportspress_handle_image_request() {
if ( ! isset( $_GET['post'] ) ) {
return;
}
$post_id = absint( $_GET['post'] );
if ( $post_id <= 0 ) {
return;
}
$post = get_post( $post_id );
if ( ! $post || $post->post_type !== 'sp_event' ) {
return;
}
$team_ids = get_post_meta( $post_id, 'sp_team', false );
if ( count( $team_ids ) < 2 ) {
return;
}
$team1_id = (int) $team_ids[0];
$team2_id = (int) $team_ids[1];
$team1 = get_post( $team1_id );
$team2 = get_post( $team2_id );
if ( ! $team1 || ! $team2 ) {
return;
}
$team1_modified = strtotime( $team1->post_modified );
$team2_modified = strtotime( $team2->post_modified );
$cache_key = "team_image_{$team1_id}_{$team1_modified}-{$team2_id}_{$team2_modified}";
$cached_path = get_transient( $cache_key );
if ( $cached_path && file_exists( $cached_path ) ) {
tony_sportspress_serve_image( $cached_path );
exit;
}
$default_color = '#FFFFFF';
$team1_colors = get_post_meta( $team1_id, 'sp_colors', true );
$team2_colors = get_post_meta( $team2_id, 'sp_colors', true );
$team1_color = ! empty( $team1_colors['primary'] ) ? $team1_colors['primary'] : $default_color;
$team2_color = ! empty( $team2_colors['primary'] ) ? $team2_colors['primary'] : $default_color;
// Validate hex colors.
if ( ! preg_match( '/^#[a-fA-F0-9]{6}$/', $team1_color ) ) {
$team1_color = $default_color;
}
if ( ! preg_match( '/^#[a-fA-F0-9]{6}$/', $team2_color ) ) {
$team2_color = $default_color;
}
$team1_logo_url = get_the_post_thumbnail_url( $team1_id, 'full' );
$team2_logo_url = get_the_post_thumbnail_url( $team2_id, 'full' );
// Skip if both teams have no distinguishable color or logo.
if ( $team1_color === $default_color && empty( $team1_logo_url )
&& $team2_color === $default_color && empty( $team2_logo_url ) ) {
return;
}
$team1_logo = get_attached_file( get_post_thumbnail_id( $team1_id ) );
$team2_logo = get_attached_file( get_post_thumbnail_id( $team2_id ) );
$image_data = tony_sportspress_generate_bisected_image( $team1_color, $team2_color, $team1_logo, $team2_logo );
$image_path = tony_sportspress_save_image_to_cache( $image_data, $cache_key );
set_transient( $cache_key, $image_path, DAY_IN_SECONDS * 30 );
tony_sportspress_serve_image( $image_path );
exit;
}
add_action( 'template_redirect', 'tony_sportspress_handle_image_request' );
/**
* Send a cached PNG image file to the browser.
*
* @param string $image_path Absolute path to the image file.
* @return void
*/
function tony_sportspress_serve_image( $image_path ) {
header( 'Content-Type: image/png' );
if ( file_exists( $image_path ) ) {
status_header( 200 );
} else {
status_header( 404 );
exit( 'Image not found.' );
}
while ( ob_get_level() ) {
ob_end_clean();
}
readfile( $image_path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_readfile
}
/**
* Write raw image data to the uploads directory and return the file path.
*
* @param string $image_data Raw PNG data.
* @param string $cache_key Cache key used as the filename stem.
* @return string
*/
function tony_sportspress_save_image_to_cache( $image_data, $cache_key ) {
$upload_dir = wp_get_upload_dir();
$file_path = $upload_dir['path'] . '/' . $cache_key . '.png';
file_put_contents( $file_path, $image_data ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
return $file_path;
}
/**
* Generate a bisected two-color PNG with optional team logos.
*
* @param string $color1 Hex color for the left half (e.g. #FF0000).
* @param string $color2 Hex color for the right half.
* @param string $logo1_path Absolute path to team 1 PNG logo (or empty).
* @param string $logo2_path Absolute path to team 2 PNG logo (or empty).
* @return string Raw PNG image data.
*/
function tony_sportspress_generate_bisected_image( $color1, $color2, $logo1_path, $logo2_path ) {
$width = 1200;
$height = 628;
$x_margin = 0.1 * ( $width / 2 );
$y_margin = 0.1 * $height;
$image = imagecreatetruecolor( $width, $height );
$rgb1 = sscanf( $color1, '#%02x%02x%02x' );
$rgb2 = sscanf( $color2, '#%02x%02x%02x' );
$color1_alloc = imagecolorallocate( $image, $rgb1[0], $rgb1[1], $rgb1[2] );
$color2_alloc = imagecolorallocate( $image, $rgb2[0], $rgb2[1], $rgb2[2] );
// Left trapezoid.
imagefilledpolygon(
$image,
array( 0, 0, 0, $height, $width * 0.40, $height, $width * 0.60, 0 ),
$color1_alloc
);
// Right trapezoid.
imagefilledpolygon(
$image,
array( $width, 0, $width, $height, $width * 0.40, $height, $width * 0.60, 0 ),
$color2_alloc
);
$max_logo_width = ( $width / 2 ) - ( 2 * $x_margin );
$max_logo_height = $height - ( 2 * $y_margin );
foreach ( array(
array( 'path' => $logo1_path, 'center_x' => $width / 4 ),
array( 'path' => $logo2_path, 'center_x' => 3 * $width / 4 ),
) as $logo ) {
if ( empty( $logo['path'] ) ) {
continue;
}
$src = imagecreatefrompng( $logo['path'] );
if ( ! $src ) {
continue;
}
$src_w = imagesx( $src );
$src_h = imagesy( $src );
$dst_w = $src_w;
$dst_h = $src_h;
if ( $src_w > $max_logo_width || $src_h > $max_logo_height ) {
$ratio = $src_w / $src_h;
if ( $src_w / $max_logo_width > $src_h / $max_logo_height ) {
$dst_w = $max_logo_width;
$dst_h = $max_logo_width / $ratio;
} else {
$dst_h = $max_logo_height;
$dst_w = $max_logo_height * $ratio;
}
}
$dst_x = (int) ( $logo['center_x'] - $dst_w / 2 );
$dst_y = (int) ( $height / 2 - $dst_h / 2 );
imagecopyresampled( $image, $src, $dst_x, $dst_y, 0, 0, (int) $dst_w, (int) $dst_h, $src_w, $src_h );
imagedestroy( $src );
}
ob_start();
imagepng( $image );
$image_data = (string) ob_get_clean();
imagedestroy( $image );
return $image_data;
}