youtube-thumbnail-resize-and-save
Gesponsorde koppelingen
PHP script bestanden
Code (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
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
<?php
// Author: Ruben Vandenbussche
// Website: http://www.RVandenbussche.nl
// Contact: info (at) RVandenbussche (dot) nl
// Date: 14 Nov 2008
//*******************************************
// Script: Youtube Thumbnail Resize v1.0
//It does:
// 1. Gets the youtube id.
// 2. Gets the youtube thumbnail.
// 3. Resizes the youtube thumbnail.
// 4. Keep ratio.
// 5. Make smaller source images bigger.
// 6. Save the image to the given destination path.
//*
//It doesn't:
// 1. Make directories.
// 2. Crop.
//*******************************************
// Summary:
// This script is made to get the youtube thumbnail from the embed($src_embed so you
// can use it yourself. The image is saved with the width ($dst_w) and height ($dst_h)
// given by the user to the destination path ($dst_path) with the user provided name ($dst_img).
//Script start here.
function getYoutubeThumb($src_embed, $dst_img, $dst_path, $dst_w, $dst_h, $dst_quality){
//Get variables for the function.
//Get youtube id.
preg_match('~http://[^/]+/v/([^&/]+)~', $src_embed, $match);
$youtube_id = $match[1];
//Complete url of the youtube thumbnail.
$src_cpl = 'http://s2.ytimg.com/vi/' . $youtube_id . '/default.jpg';
//complete path of the destination image.
$dst_cpl = $dst_path . basename($dst_img);
//width and height sizes of the youtube thumbnail.
list($src_w, $src_h) = getimagesize($src_cpl);
//Create image from the thumbnail.
$src_img = imagecreatefromjpeg($src_cpl);
//Get heights and width so the image keeps its ratio.
$x_ratio = $dst_w / $src_w;
$y_ratio = $dst_h / $src_h;
if( (($x_ratio > 1) || ($y_ratio > 1)) && ($x_ratio > $y_ratio) ){
//If one of the sizes of the image is smaller than the destination (normal: more height than width).
$dst_w = ceil($y_ratio * $src_w);
$dst_h = $dst_h;
}elseif( (($x_ratio > 1) || ($y_ratio > 1)) && ($y_ratio > $x_ratio) ){
//If one of the sizes of the image is smaller than the destination (landscape: more width than height).
$dst_w = $dst_w;
$dst_h = ceil($x_ratio * $src_h);
}elseif (($x_ratio * $src_h) < $dst_h){
//if the image is landscape (more width than height).
$dst_h = ceil($x_ratio * $src_h);
$dst_w = $dst_w;
}elseif (($x_ratio * $src_h) > $dst_h){
//if the image is normal (more height than width).
$dst_h = ceil($x_ratio * $src_h);
$dst_w = $dst_w;
}else{
//if the image is normal (more height than width).
$dst_w = ceil($y_ratio * $src_w);
$dst_h = $dst_h;
}
// Creating the resized image.
$dst_img = imagecreatetruecolor($dst_w,$dst_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$dst_w, $dst_h,$src_w,$src_h);
// Saving the resized image.
imagejpeg($dst_img,$dst_cpl,$dst_quality);
// Cleaning the memory.
imagedestroy($src_img);
imagedestroy($dst_img);
}
//information needed for the function
$src_embed = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/UqZnB-1SdGM&hl=en&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/UqZnB-1SdGM&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>'; //Youtube embed source
$dst_img = 'test_thumb2.jpg'; // This name will be given to the resized image.
$dst_path = '../directory/thumbs/'; // In this path the resized image will be saved
$dst_w= '150'; // The width of the resized image
$dst_h = '150'; // The height of the resized image
$dst_quality = '100'; // Quality of the resized image (best quality = 100)
getYoutubeThumb($src_embed, $dst_img, $dst_path, $dst_w, $dst_h, $dst_quality); // The function.
?>
// Author: Ruben Vandenbussche
// Website: http://www.RVandenbussche.nl
// Contact: info (at) RVandenbussche (dot) nl
// Date: 14 Nov 2008
//*******************************************
// Script: Youtube Thumbnail Resize v1.0
//It does:
// 1. Gets the youtube id.
// 2. Gets the youtube thumbnail.
// 3. Resizes the youtube thumbnail.
// 4. Keep ratio.
// 5. Make smaller source images bigger.
// 6. Save the image to the given destination path.
//*
//It doesn't:
// 1. Make directories.
// 2. Crop.
//*******************************************
// Summary:
// This script is made to get the youtube thumbnail from the embed($src_embed so you
// can use it yourself. The image is saved with the width ($dst_w) and height ($dst_h)
// given by the user to the destination path ($dst_path) with the user provided name ($dst_img).
//Script start here.
function getYoutubeThumb($src_embed, $dst_img, $dst_path, $dst_w, $dst_h, $dst_quality){
//Get variables for the function.
//Get youtube id.
preg_match('~http://[^/]+/v/([^&/]+)~', $src_embed, $match);
$youtube_id = $match[1];
//Complete url of the youtube thumbnail.
$src_cpl = 'http://s2.ytimg.com/vi/' . $youtube_id . '/default.jpg';
//complete path of the destination image.
$dst_cpl = $dst_path . basename($dst_img);
//width and height sizes of the youtube thumbnail.
list($src_w, $src_h) = getimagesize($src_cpl);
//Create image from the thumbnail.
$src_img = imagecreatefromjpeg($src_cpl);
//Get heights and width so the image keeps its ratio.
$x_ratio = $dst_w / $src_w;
$y_ratio = $dst_h / $src_h;
if( (($x_ratio > 1) || ($y_ratio > 1)) && ($x_ratio > $y_ratio) ){
//If one of the sizes of the image is smaller than the destination (normal: more height than width).
$dst_w = ceil($y_ratio * $src_w);
$dst_h = $dst_h;
}elseif( (($x_ratio > 1) || ($y_ratio > 1)) && ($y_ratio > $x_ratio) ){
//If one of the sizes of the image is smaller than the destination (landscape: more width than height).
$dst_w = $dst_w;
$dst_h = ceil($x_ratio * $src_h);
}elseif (($x_ratio * $src_h) < $dst_h){
//if the image is landscape (more width than height).
$dst_h = ceil($x_ratio * $src_h);
$dst_w = $dst_w;
}elseif (($x_ratio * $src_h) > $dst_h){
//if the image is normal (more height than width).
$dst_h = ceil($x_ratio * $src_h);
$dst_w = $dst_w;
}else{
//if the image is normal (more height than width).
$dst_w = ceil($y_ratio * $src_w);
$dst_h = $dst_h;
}
// Creating the resized image.
$dst_img = imagecreatetruecolor($dst_w,$dst_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$dst_w, $dst_h,$src_w,$src_h);
// Saving the resized image.
imagejpeg($dst_img,$dst_cpl,$dst_quality);
// Cleaning the memory.
imagedestroy($src_img);
imagedestroy($dst_img);
}
//information needed for the function
$src_embed = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/UqZnB-1SdGM&hl=en&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/UqZnB-1SdGM&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>'; //Youtube embed source
$dst_img = 'test_thumb2.jpg'; // This name will be given to the resized image.
$dst_path = '../directory/thumbs/'; // In this path the resized image will be saved
$dst_w= '150'; // The width of the resized image
$dst_h = '150'; // The height of the resized image
$dst_quality = '100'; // Quality of the resized image (best quality = 100)
getYoutubeThumb($src_embed, $dst_img, $dst_path, $dst_w, $dst_h, $dst_quality); // The function.
?>