유투브 썸네일, 공유소스 가져오기 > 개발자팁

개발자팁

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

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

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

본문


<?
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>";
}
?>

 

<? // 사용 예
$url = "";
$src = yt_image($url);
$iframe = yt_iframe($url);
?>
<img src="<?=$src?>">
<?=$iframe?>
추천
0
  • 복사

댓글 1개


<?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']; ?>

이렇게 쓰는것도 좋을것 같습니다.
© SIRSOFT
현재 페이지 제일 처음으로