링크1, 링크2에 유튜브, 비메오 영상 링크 등록 시 본문 출력 및 자동재생
01. 함수추가 페이지 상단 <?php ?> 사이에 추가
<?php
if (!defined("_GNUBOARD_")) exit; // 개별 페이지 접근 불가
include_once(G5_LIB_PATH.'/thumbnail.lib.php');
// add_stylesheet('css 구문', 출력순서); 숫자가 작을 수록 먼저 출력됨
add_stylesheet('<link rel="stylesheet" href="'.$board_skin_url.'/style.css">', 0);
// 링크1과 링크2에 대해 YouTube, Vimeo 링크를 감지하고 변환
function embed_video_with_autoplay($url) {
// 유튜브 URL 처리
if (preg_match('/youtu\.be\/([a-zA-Z0-9_-]+)/', $url, $matches) ||
preg_match('/youtube\.com\/.*v=([a-zA-Z0-9_-]+)/', $url, $matches)) {
$video_id = $matches[1];
return "<iframe width='100%' height='315' src='https://www.youtube.com/embed/{$video_id}?autoplay=1&mute=1' frameborder='0' allow='autoplay; encrypted-media' allowfullscreen></iframe>";
}
// 비메오 URL 처리
if (preg_match('/vimeo\.com\/(\d+)/', $url, $matches)) {
$video_id = $matches[1];
return "<iframe width='100%' height='315' src='https://player.vimeo.com/video/{$video_id}?autoplay=1&mute=1' frameborder='0' allow='autoplay; fullscreen' allowfullscreen></iframe>";
}
// 유튜브나 비메오 URL이 아니면 빈 문자열 반환
return "";
}
?>
02. style 부분 추가
<style>
iframe {
width: 100%;
aspect-ratio: 16 / 9; /* 16:9 비율 */
height: auto; /* 실제 높이는 aspect-ratio에 의해 자동 계산됨 */
}
</style>
03. 본문 출력
<?php echo get_view_thumbnail($view['content']); ?> 위 또는 아래 원하는 위치에 아래 코드 추가
<!-- 본문 내용 시작 { -->
<div id="bo_v_con">
<?php
// 파일 출력
$v_img_count = count($view['file']);
if($v_img_count) {
echo "<div id=\"bo_v_img\">\n";
foreach($view['file'] as $view_file) {
echo get_file_thumbnail($view_file);
}
echo "</div>\n";
}
?>
<?php
// 링크1과 링크2 유튜브 또는 비메오 링크 일 경우 본문에 영상 자동재생 되도록 출력
$link1_embed = $view['link'][1] ? embed_video_with_autoplay($view['link'][1]) : '';
$link2_embed = $view['link'][2] ? embed_video_with_autoplay($view['link'][2]) : '';
// 출력
echo $link1_embed;
echo $link2_embed;
?>
<?php echo get_view_thumbnail($view['content']); ?>
</div>