首页 > 解决方案 > 如何将 Open Graph 图像设置为 PHP 生成的图像?

问题描述

我有一个 PHP 页面,它打开一个目录,选择一个随机图像,并将页面设置为仅该图像。

<?php
$imageDir = 'random_images_dir/';
$images = glob($imageDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$randomImage = $images[array_rand($images)];
$file_out = $randomImage;

if (file_exists($file_out)) {

   $image_info = getimagesize($file_out);

   header('Content-Type: ' . $image_info['mime']);

   header('Content-Length: ' . filesize($file_out));

   readfile($file_out);
}
else {
    header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found");
}
?>

是否可以将链接发送为https://example.com/random_image.php(例如在 Discord 中)并将生成的图像嵌入 Open Graph 中?通常在发送链接时,它会选择一张图片,然后只发送该图片。

标签: phphtmlimagefacebook-opengraph

解决方案


推荐阅读