-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathyt-thumb.php
159 lines (119 loc) · 4.13 KB
/
yt-thumb.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/*
* YouTube Thumbnail Enchancer by Hal Gatewood
* License: The just-use-it license. Have fun!
*
* Dependances:
* curl
* GD Library
* coffee
*
* Parameters:
* inpt = YouTube URL or YouTube ID
* quality = hq or mq
* refresh = skips the cache to grab a fresh one
* play = show play button in middle
*
* Usage:
* http://example.com/yt-thumb.php?quality=hq&inpt=http://www.youtube.com/watch?v=XZ4X1wcZ1GE
* http://example.com/yt-thumb.php?quality=mq&inpt=http://www.youtube.com/watch?v=XZ4X1wcZ1GE
* http://example.com/yt-thumb.php?quality=hq&inpt=XZ4X1wcZ1GE
* http://example.com/yt-thumb.php?quality=mq&inpt=XZ4X1wcZ1GE
* http://example.com/yt-thumb.php?quality=hq&inpt=XZ4X1wcZ1GE&play
* http://example.com/yt-thumb.php?quality=hq&inpt=XZ4X1wcZ1GE&play&refresh
*
*/
// GET YOUTUBE ID FROM THE SLEW OF YOUTUBE URLS
// (FOUND ON STACKEXCHANGE SOMEWHERE)
function getYouTubeIdFromURL($url)
{
$pattern = '/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/i';
preg_match($pattern, $url, $matches);
return isset($matches[1]) ? $matches[1] : false;
}
// PARAMETERS
$is_url = false;
$quality = $_REQUEST['quality'];
$quality = ($quality == "hq") ? "hq" : "mq";
$inpt = trim($_REQUEST['inpt']);
$show_play_icon = isset($_REQUEST['play']) ? true : false;
$play_btn_file_name = ($show_play_icon) ? "-play" : "";
// ADD HTTP
if(substr($inpt, 0, 4) == "www."){ $inpt = "http://" . $inpt; $is_url = true; }
if(substr($inpt, 0, 8) == "youtube."){ $inpt = "http://" . $inpt; $is_url = true; }
if(substr($inpt, 0, 8) == "youtu.be"){ $inpt = "http://" . $inpt; $is_url = true; }
// IF URL GET ID
if(substr($inpt, 0, 7) == "http://" OR substr($inpt, 0, 8) == "https://")
{
$is_url = true;
$id = getYouTubeIdFromURL($inpt);
}
// IF NOT URL TRY ID AS INPUT
if(!$is_url) { $id = $inpt; }
// FILENAME
$filename = ($quality == "mq") ? $id . "-mq": $id;
$filename .= $play_btn_file_name;
// IF EXISTS, GO
if(file_exists("i/" . $filename . ".jpg") AND !isset($_GET['refresh']))
{
header("Location: i/" . $filename . ".jpg");
die;
}
// CHECK IF YOUTUBE VIDEO
$handle = curl_init("https://www.youtube.com/watch/?v=" . $id);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($handle);
// CHECK FOR 404 OR NO RESPONSE
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($httpCode == 404 OR !$response)
{
header("Status: 404 Not Found");
die("No YouTube video found or YouTube timed out. Try again soon.");
}
curl_close($handle);
// IF NOT ID THROW AN ERROR
if(!$id)
{
header("Status: 404 Not Found");
die("YouTube ID not found");
}
// CREATE IMAGE FROM YOUTUBE THUMB
$image = imagecreatefromjpeg( "http://img.youtube.com/vi/" . $id . "/" . $quality . "default.jpg" );
// IF HIGH QUALITY WE CREATE A NEW CANVAS WITHOUT THE BLACK BARS
if($quality == "hq")
{
$cleft = 0;
$ctop = 45;
$canvas = imagecreatetruecolor(480, 270);
imagecopy($canvas, $image, 0, 0, $cleft, $ctop, 480, 360);
$image = $canvas;
}
$imageWidth = imagesx($image);
$imageHeight = imagesy($image);
// ADD THE PLAY ICON
$play_icon = $show_play_icon ? "play-" : "noplay-";
$play_icon .= $quality . ".png";
$logoImage = imagecreatefrompng( $play_icon );
imagealphablending($logoImage, true);
$logoWidth = imagesx($logoImage);
$logoHeight = imagesy($logoImage);
// CENTER PLAY ICON
$left = round($imageWidth / 2) - round($logoWidth / 2);
$top = round($imageHeight / 2) - round($logoHeight / 2);
// CONVERT TO PNG SO WE CAN GET THAT PLAY BUTTON ON THERE
imagecopy( $image, $logoImage, $left, $top, 0, 0, $logoWidth, $logoHeight);
imagepng( $image, $filename .".png", 9);
// MASHUP FINAL IMAGE AS A JPEG
$input = imagecreatefrompng($filename .".png");
$output = imagecreatetruecolor($imageWidth, $imageHeight);
$white = imagecolorallocate($output, 255, 255, 255);
imagefilledrectangle($output, 0, 0, $imageWidth, $imageHeight, $white);
imagecopy($output, $input, 0, 0, 0, 0, $imageWidth, $imageHeight);
// OUTPUT TO 'i' FOLDER
imagejpeg($output, "i/" . $filename . ".jpg", 95);
// UNLINK PNG VERSION
@unlink($filename .".png");
// REDIRECT TO NEW IMAGE
header("Location: i/" . $filename . ".jpg");
die;
?>