그누보드5 링크 썸네일을 게시판 첨부파일 사용하도록 하는 코드 (OG이미지메타테그)

· 2024-12-25 (수) 09:42:04 · 2700 · 2

아래의 코드를 head.sub.php의 </head> 위에 삽입

<?php

function override_og_image() {

    $html = ob_get_contents();

    libxml_use_internal_errors(true);

    

    $dom = new DOMDocument();

    @$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

    $xpath = new DOMXPath($dom);

    

    // Find view_image class images

    $images = $xpath->query('//a[@class="view_image"]//img');

    

    // Extract current OG image

    preg_match('/<meta\s+property="og:image"\s+content="([^"]*)"[^>]*>/i', $html, $matches);

    $defaultImageUrl = isset($matches[1]) ? $matches[1] : 'https://내도메인/이미지';

    

    // Select random image or fallback

    if ($images->length > 0) {

        $randomIndex = rand(0, $images->length - 1);

        $newImageUrl = $images->item($randomIndex)->getAttribute('src');

        

        // Validate URL format

        if (!filter_var($newImageUrl, FILTER_VALIDATE_URL)) {

            $newImageUrl = $defaultImageUrl;

        }

    } else {

        $newImageUrl = $defaultImageUrl;

    }

    

    // Replace OG image meta tag

    $pattern = '/<meta\s+property="og:image"\s+content="[^"]*"[^>]*>/i';

    $replacement = '<meta property="og:image" content="' . htmlspecialchars($newImageUrl, ENT_QUOTES, 'UTF-8') . '">';

    $modified_html = preg_replace($pattern, $replacement, $html);

    

    ob_clean();

    echo $modified_html;

}

 

ob_start();

register_shutdown_function('override_og_image');

?>

 

*클로드ai 작품

|

댓글 2개

2024-12-26 (목) 13:13:47

감사합니다 ^^

감사합니다.

댓글을 작성하시려면 로그인이 필요합니다.

그누보드5 팁자료실

2,788건
+
제목 글쓴이 날짜 조회
24-12-28 조회 2,661
24-12-26 조회 3,317
24-12-25 조회 2,701
24-12-24 조회 3,273
24-12-24 조회 2,552
24-12-19 조회 3,007
24-12-17 조회 2,812
24-12-17 조회 2,629
24-12-16 조회 2,662
24-12-14 조회 3,228
24-12-14 조회 2,999
24-12-12 조회 3,008
24-12-10 조회 2,782
24-12-03 조회 3,140
24-12-01 조회 2,761
24-11-30 조회 2,859
24-11-29 조회 4,289
24-11-28 조회 2,716
24-11-25 조회 2,531
24-11-22 조회 2,586
24-11-20 조회 2,905
24-11-19 조회 2,519
24-11-16 조회 3,059
24-11-13 조회 2,547
24-11-11 조회 3,051
24-11-09 조회 2,839
24-11-04 조회 3,481
24-11-02 조회 2,850
24-10-28 조회 3,226
24-10-24 조회 3,235