<?php
// og_banner.php - Dynamic Open Graph 1200x630 Banner Generator for Facebook
error_reporting(0);

$site_root_dir = __DIR__;
$project_root_dir = dirname($site_root_dir);

$img_rel = isset($_GET['img']) ? $_GET['img'] : '';
$img_rel = urldecode($img_rel);

// Strip full domain prefix if full URL was passed
if (preg_match('/^https?:\/\/[^\/]+\/(.*)$/i', $img_rel, $m)) {
    $img_rel = $m[1];
}

$img_rel = preg_replace('/^(\.\.\/|\.\/)+/', '', trim($img_rel));

$local_file = '';
if (!empty($img_rel)) {
    if (file_exists($project_root_dir . '/' . $img_rel) && !is_dir($project_root_dir . '/' . $img_rel)) {
        $local_file = $project_root_dir . '/' . $img_rel;
    } else if (file_exists($site_root_dir . '/' . $img_rel) && !is_dir($site_root_dir . '/' . $img_rel)) {
        $local_file = $site_root_dir . '/' . $img_rel;
    }
}

if (empty($local_file) || !file_exists($local_file)) {
    if (file_exists($site_root_dir . '/images/logo.png')) {
        $local_file = $site_root_dir . '/images/logo.png';
    }
}

// Check cache
$cache_dir = $site_root_dir . '/images/cache_og/';
if (!file_exists($cache_dir)) {
    @mkdir($cache_dir, 0755, true);
}

$cache_key = md5($local_file . filemtime($local_file));
$cache_file = $cache_dir . $cache_key . '.jpg';

if (file_exists($cache_file) && filesize($cache_file) > 0) {
    header("Content-Type: image/jpeg");
    header("Content-Length: " . filesize($cache_file));
    readfile($cache_file);
    exit;
}

// If GD is available, generate 1200x630 banner
if (function_exists('imagecreatetruecolor') && !empty($local_file)) {
    $info = @getimagesize($local_file);
    $src_img = false;
    if ($info) {
        $type = $info[2];
        if ($type == IMAGETYPE_JPEG) {
            $src_img = @imagecreatefromjpeg($local_file);
        } else if ($type == IMAGETYPE_PNG) {
            $src_img = @imagecreatefrompng($local_file);
        } else if ($type == IMAGETYPE_GIF) {
            $src_img = @imagecreatefromgif($local_file);
        }
    }

    if ($src_img) {
        $orig_w = imagesx($src_img);
        $orig_h = imagesy($src_img);

        $target_w = 1200;
        $target_h = 630;

        $canvas = imagecreatetruecolor($target_w, $target_h);

        // Fill background
        $bg_fill = imagecreatetruecolor($target_w, $target_h);
        imagecopyresampled($bg_fill, $src_img, 0, 0, 0, 0, $target_w, $target_h, $orig_w, $orig_h);
        if (function_exists('imagefilter')) {
            for ($b = 0; $b < 12; $b++) {
                imagefilter($bg_fill, IMG_FILTER_GAUSSIAN_BLUR);
            }
        }
        imagecopy($canvas, $bg_fill, 0, 0, 0, 0, $target_w, $target_h);
        imagedestroy($bg_fill);

        // Dark overlay for contrast
        $overlay = imagecreatetruecolor($target_w, $target_h);
        $black = imagecolorallocate($overlay, 0, 0, 0);
        imagefill($overlay, 0, 0, $black);
        imagecopymerge($canvas, $overlay, 0, 0, 0, 0, $target_w, $target_h, 30);
        imagedestroy($overlay);

        // Calculate aspect fit size
        $scale = min($target_w / $orig_w, $target_h / $orig_h);
        $new_w = round($orig_w * $scale);
        $new_h = round($orig_h * $scale);

        $dst_x = round(($target_w - $new_w) / 2);
        $dst_y = round(($target_h - $new_h) / 2);

        // Draw centered foreground image
        imagecopyresampled($canvas, $src_img, $dst_x, $dst_y, 0, 0, $new_w, $new_h, $orig_w, $orig_h);
        imagedestroy($src_img);

        imagejpeg($canvas, $cache_file, 90);
        imagedestroy($canvas);

        header("Content-Type: image/jpeg");
        header("Content-Length: " . filesize($cache_file));
        readfile($cache_file);
        exit;
    }
}

if (!empty($local_file) && file_exists($local_file)) {
    $mime = mime_content_type($local_file);
    header("Content-Type: " . ($mime ?: "image/jpeg"));
    header("Content-Length: " . filesize($local_file));
    readfile($local_file);
    exit;
}
?>
