t

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

아래의 코드를 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개

감사합니다 ^^

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

그누보드5 팁자료실

+
제목 글쓴이 날짜 조회
1년 전 조회 874
1년 전 조회 1,119
1년 전 조회 890
1년 전 조회 656
1년 전 조회 865
1년 전 조회 1,175
1년 전 조회 1,562
1년 전 조회 916
1년 전 조회 969
1년 전 조회 858
1년 전 조회 1,082
1년 전 조회 918
1년 전 조회 874
1년 전 조회 1,196
1년 전 조회 851
1년 전 조회 1,441
1년 전 조회 811
1년 전 조회 1,175
1년 전 조회 1,032
1년 전 조회 924
1년 전 조회 957
1년 전 조회 1,404
1년 전 조회 1,258
1년 전 조회 1,233
1년 전 조회 1,005
1년 전 조회 1,375
1년 전 조회 971
1년 전 조회 1,087
1년 전 조회 2,237
1년 전 조회 966