Youtube Thumbnail Resize and Save
Dit script lijkt erg veel op mijn Image Resize script alleen maakt hij nu een Youtube thumbnail. Wat erg handig kan zijn als je alleen de thumbnail wil laten zien en niet gelijk de hele movie. Hij spreek voor zich. Feedback is zoals altijd welkom. 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). Warning: The thumbnail image is provided by the youtube image server. If youtube decide to change server or to change the name of the thumbnails. This script will not work anymore. Howevery you only have to change the line: $src_cpl = 'http://s2.ytimg.com/vi/' . $youtube_id . '/default.jpg';
<?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.
?>
Reacties
0