갤러리 리스트 썸네일에서 하나를 추가하고 싶습니다.

갤러리 리스트 썸네일에서 하나를 추가하고 싶습니다.

QA

갤러리 리스트 썸네일에서 하나를 추가하고 싶습니다.

답변 1

본문

안녕하세요.
제가 아래와 같은 방법으로 갤러리 게시판 리스트에서 썸네일을 불러오고 있습니다.

 


                <?php
                if ($list[$i]['is_notice']) { // 공지사항  ?>
                    <img src="<?php echo $board_skin_url ?>/img/notice.gif"  class="img-fluid">
                <?php }else if($list[$i]['icon_secret']) { ?>
                    <img src="<?php echo $board_skin_url ?>/img/secret.gif"  class="img-fluid">
              
                <?php } else {
                    $thumb = get_list_thumbnail($board['bo_table'], $list[$i]['wr_id'], $board['bo_gallery_width'], $board['bo_gallery_height'], false, true);
   
                    $img_content = '';
                    if($thumb['src']) {
                        $img_content = '<img src="'.$thumb['src'].'" class="img-fluid">';
                    } else if($list[$i]['wr_4']) {
                        $img_content = '<img src="'.$list[$i]['wr_4'].'" class="img-fluid1">';
                    } else {
                        $thumbnail_file = substr($list[$i]['wr_9'], 0, 4) == "http" ? $list[$i]['wr_9'] : substr(str_replace("/", "", str_replace("?v=", "", substr(explode("https://www.youtube.com/watch", $list[$i]['wr_content'])[1], 0, 21))), 0, 11);
                        if (empty($thumbnail_file)) {
                            $thumbnail_extension = [".jpg", ".png", ".gif", "jpeg", ".mp4"];
                            foreach ($list[$i]['file'] as $thumbnail) {
                                if (in_array(strtolower(substr($thumbnail['file'], -4)), $thumbnail_extension)) {
                                    $thumbnail_file = $thumbnail['path']."/".$thumbnail['file'];
                                    break;
                                }
                                else $thumbnail_file = $board_skin_url."/img/no_image.gif"; 
                            }
                        }
                        $thumbnail_file = substr($thumbnail_file, 0, 4) == "http" ? $thumbnail_file : "https://img.youtube.com/vi/".$thumbnail_file."/maxresdefault.jpg";
                        $img_content = strtolower(substr($thumbnail_file, -4)) == ".mp4" ? "<video class='img-fluid2' src='".$thumbnail_file."' loop muted></video>" : "<img class='img-fluid' src='".$thumbnail_file."'>";
                    }
                    echo run_replace('thumb_image_tag', $img_content, $thumb);
                }
                ?>

 

여기에 유튜브 쇼츠 썸네일을 더 추가하고 싶은데 방법을 모르겠습니다.


substr(str_replace("/", "", str_replace("?v=", "", substr(explode("https://www.youtube.com/watch", $list[$i]['wr_content'])[1], 0, 21))), 0, 11);

이 부분을


substr(str_replace("/", "", str_replace("?v=", "", substr(explode("https://www.youtube.com/shorts", $list[$i]['wr_content'])[1], 0, 21))), 0, 11);

로 바꾸면 쇼츠로 올린 영상의 썸네일이 불러와지기는 하지만 이렇게 변경하면 유튜브 영상의 썸네일은 불러와 지지 않습니다.

 

이 두 개를 같이 사용할 수 있는 방법을 아시는 분 계시면 좀 알려주세요.
감사합니다.

이 질문에 댓글 쓰기 :

답변 1

/lib/common.lib.php에 아래 함수 추가하시고



function get_youtube_id($url)
{ 
    $regex = '#^(?:https?://|//)?(?:www\.|m\.|.+\.)?(?:youtu\.be/|youtube\.com/(?:embed/|v/|shorts/|feeds/api/videos/|watch\?v=|watch\?.+&v=))([\w-]{11})(?![\w-])#';
    preg_match($regex, $url, $matches);
    return isset($matches[1]) ? $matches[1] : null;
}

아래코드는 리스트에 넣어서 응용하세요


$thumbnail_file = "https://img.youtube.com/vi/".get_youtube_id($list[$i]['wr_9])."/maxresdefault.jpg";

 

 

친절하신 답변 감사합니다.

그런데, 알려주신대로 하면
Parse error: syntax error, unexpected 'img' (T_STRING), expecting ']' 에러가 나오네요.

그리고 제가 잘 몰라서 그러는데,
$thumbnail_file = "https://img.youtube.com/vi/".get_youtube_id($list[$i]['wr_9])."/maxresdefault.jpg";
가 wr_9에 입력한 유튜브나 쇼츠 주소에서 썸네일을 뽑느다는 의미인가요?

지금 썸네일 불러오는 소스가 유튜브 주소를 wr_9에 입력하는게 아니라 그냥 본문에 주소만 입력했을 때 뽑느거라서요.

혹시나해서 wr_9를 wr_content로 바꿔봤지만 동일하게 에러나오고요.

어떤식으로 되어있는지 몰라서 함수만 다시 수정했습니다.
get_youtube_id()에 주소든 html코드든 유튜브주소형태가있으면 아이디만 뽑아서 배열로 리턴합니다. 무조건 배열로 리턴하니까 if($youtube_id_array[0]) 이런식으로 체크해서 사용해보세요


function get_youtube_id($url)
{
    $regex = '#(?:https?://|//)?(?:www\.|m\.|.+\.)?(?:youtu\.be/|youtube\.com/(?:embed/|v/|shorts/|feeds/api/videos/|watch\?v=|watch\?.+&v=))([\w-]{11})(?![\w-])#';
    preg_match_all($regex, $url, $matches);
    return isset($matches[1]) ? $matches[1] : array();
}

$text = '<div>
<a href="https://youtube.com/shorts/grPpo2JIF8o?si=UC8MaNjn1rNbJTuK"></a>
<iframe width="560" height="315" src="https://www.youtube.com/embed/A5KwgSVxgdg?si=WZW9sKVmhd2ZhFV-" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>
<iframe width="560" height="315" src="https://www.youtube.com/embed/eNngaRXaLpw?si=hsROu7vtX5kpMoUk" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>
</div>';

$youtube_id_array = get_youtube_id($text);
print_r($youtube_id_array);

계속 시간내주셔서 정말 감사합니다.
그런데 알려주신 내용을 어떻게 넣어야 할지 잘 모르겠습니다.
이리저리 넣어봤지만 에러가 나네요.

그런데, 무식한(?) 방법으로 원하는걸 구현하긴 했습니다.
나중에 또 어떤 에러가 나올지 모르겠지만 일단 원하는대로 되긴하네요.
/lib/common.lib.php는 건들지 않고,

기존 소스 아래


                        $thumbnail_file1 = substr($list[$i]['wr_9'], 0, 4) == "http" ? $list[$i]['wr_9'] : substr(str_replace("/", "", str_replace("?v=", "", substr(explode("https://www.youtube.com/shorts", $list[$i]['wr_content'])[1], 0, 21))), 0, 11);
                        if (empty($thumbnail_file1)) {
                            $thumbnail_extension = [".jpg", ".png", ".gif", "jpeg", ".mp4"];
                            foreach ($list[$i]['file'] as $thumbnail) {
                                if (in_array(strtolower(substr($thumbnail['file'], -4)), $thumbnail_extension)) {
                                    $thumbnail_file1 = $thumbnail['path']."/".$thumbnail['file'];
                                    break;
                                }
                                else $thumbnail_file1 = $board_skin_url."/img/no_image.gif"; 
                            }
                        }
                        $thumbnail_file1 = substr($thumbnail_file1, 0, 4) == "http" ? $thumbnail_file : "https://img.youtube.com/vi/".$thumbnail_file1."/maxresdefault.jpg";
                        $img_content = strtolower(substr($thumbnail_file1, -4)) == ".mp4" ? "<video class='img-fluid2' src='".$thumbnail_file1."' loop muted></video>" : "<img class='img-fluid' src='".$thumbnail_file1."'>";

이런식으로 추가하니 일단 에러도 안나고
유튜브, 쇼츠 썸네일도 정상적으로 나오네요.

답변을 작성하시기 전에 로그인 해주세요.
QA 내용 검색
질문등록
전체 0
© SIRSOFT
현재 페이지 제일 처음으로