치즈버거

유투브 썸네일, 공유소스 가져오기

· 2017-01-07 (토) 19:23:44 · 4973 · 1

[code]
<?
function yt_thumb($url) {
    $a = parse_url($url);
    parse_str($a[query], $a);
    return "http://img.youtube.com/vi/$a[v]/0.jpg";
}

function yt_iframe($url) {
    $a = parse_url($url);
    parse_str($a[query], $a);
    $is_mobile =
        preg_match('/phone|samsung|lgtel|mobile|[^A]skt|nokia|blackberry|android|sony/i',
        $_SERVER['HTTP_USER_AGENT']);
    if ($is_mobile) {
        $width = 360;
        $height = 240;
    }
    else {
        $width = 560;
        $height = 315;
    }
    return "<iframe width='$width' height='$height' src='https://www.youtube.com/embed/$a[v]' frameborder='0' allowfullscreen></iframe>";
}
?>
[/code]

 

<? // 사용 예
$url = "https://www.youtube.com/watch?v=QnsfHfgIRJM";
$src = yt_image($url);
$iframe = yt_iframe($url);
?>
<img src="<?=$src?>">
<?=$iframe?>

댓글 1개

[code]
<?php
function YouTube($url='', $width='', $height='') {
if(!$url) return array();
$OutPut = array();
$url = end(explode('=', parse_url($url, PHP_URL_QUERY)));
if(!$url) return array();
$is_mobile = preg_match('/phone|samsung|lgtel|mobile|[^A]skt|nokia|blackberry|android|sony/i', $_SERVER['HTTP_USER_AGENT']);
if(!$width || !$height) {
if($is_mobile) { $width = 360; $height = 240; }
else { $width = 560; $height = 315; }
}
$OutPut['thumb'] = 'http://img.youtube.com/vi/'.$url.'/0.jpg';
$OutPut['iframe'] = '<iframe width="'.$width.'" height="'.$height.'" src="https://www.youtube.com/embed/'.$url.'" frameborder="0" allowfullscreen></iframe> ';
return $OutPut;
}
$YouTube = YouTube('https://www.youtube.com/watch?v=QnsfHfgIRJM');
?>
<img src="<?php echo $YouTube['thumb']; ?>" alt="">
<?php echo $YouTube['iframe']; ?>
[/code]
이렇게 쓰는것도 좋을것 같습니다.
댓글을 작성하시려면 로그인이 필요합니다.

개발자팁

5,404건

개발과 관련된 유용한 정보를 공유하세요. 질문은 QA에서 해주시기 바랍니다.

+
분류 제목 글쓴이 날짜 조회
PHP 17-01-13 조회 3,135
PHP 17-01-13 조회 2,925
PHP 17-01-12 조회 2,459
PHP 17-01-12 조회 2,709
PHP 17-01-12 조회 2,619
PHP 17-01-11 조회 2,926
PHP 17-01-11 조회 2,663
PHP 17-01-11 조회 2,540
PHP 17-01-10 조회 2,583
PHP 17-01-10 조회 2,331
PHP 17-01-10 조회 2,456
PHP 17-01-10 조회 4,103
PHP 17-01-09 조회 4,692
PHP 17-01-09 조회 3,395
PHP 17-01-09 조회 2,776
PHP 17-01-08 조회 4,659
PHP 17-01-08 조회 2,765
PHP 17-01-08 조회 2,602
PHP 17-01-07 조회 4,974
PHP 17-01-07 조회 3,247
PHP 17-01-06 조회 2,566
PHP 17-01-06 조회 3,001
PHP 17-01-06 조회 2,736
PHP 17-01-06 조회 3,115
PHP 17-01-05 조회 2,631
PHP 17-01-05 조회 2,993
PHP 17-01-05 조회 2,685
PHP 17-01-04 조회 2,851
PHP 17-01-04 조회 2,606
PHP 17-01-04 조회 3,022