source: extensions/gvideo/include/functions.inc.php @ 17311

Last change on this file since 17311 was 17311, checked in by mistic100, 12 years ago

missing jpeg quality in add_film_frame()

File size: 11.1 KB
Line 
1<?php
2if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
3
4function parse_video_url($source_url)
5{
6  $source_url = 'http://'.preg_replace('#^http(s?)://#', null, $source_url);
7 
8  $url = parse_url($source_url);
9  $url['host'] = str_replace('www.', null, $url['host']);
10  $url['host'] = explode('.', $url['host']);
11 
12  $video = array();
13  switch ($url['host'][0])
14  {
15    /* youtube */
16    case 'youtube':
17    {
18      $video['type'] = 'youtube';
19
20      parse_str($url['query'], $url['query']);
21      if (empty($url['query']['v'])) return false;
22     
23      $video['id'] = $url['query']['v'];
24    }
25   
26    case 'youtu': // youtu.be (short-url service)
27    {
28      if (empty($video))
29      {
30        $video['type'] = 'youtube';
31       
32        $url['path'] = explode('/', $url['path']);
33        $video['id'] = $url['path'][1];
34      }
35     
36      $api_url = 'http://gdata.youtube.com/feeds/api/videos/'.$video['id'].'?v=2&alt=json';
37      $json = download_remote_file($api_url, true);
38      if ($json === false or $json == 'file_error') return false;
39     
40      $json = json_decode($json, true);
41      $video = array_merge($video, array(
42        'title' => $json['entry']['title']['$t'],
43        'description' => $json['entry']['media$group']['media$description']['$t'],
44        'thumbnail' => $json['entry']['media$group']['media$thumbnail'][2]['url'],
45        'author' => $json['entry']['author'][0]['name']['$t'],
46        ));
47      break;
48    }
49     
50    /* vimeo */
51    case 'vimeo':
52    {
53      $video['type'] = 'vimeo';
54     
55      $url['path'] = explode('/', $url['path']);
56      $video['id'] = $url['path'][1];
57     
58      $api_url = 'http://vimeo.com/api/v2/video/'.$video['id'].'.json';
59      $json = download_remote_file($api_url, true);
60      if ($json === false or $json == 'file_error') return false;
61     
62      $json = json_decode($json, true);
63      $video = array_merge($video, array(
64        'title' => $json[0]['title'],
65        'description' => $json[0]['description'],
66        'thumbnail' => $json[0]['thumbnail_large'],
67        'author' => $json[0]['user_name'],
68        ));
69      break;
70    }
71     
72    /* dailymotion */
73    case 'dailymotion':
74    {
75      $video['type'] = 'dailymotion';
76     
77      $url['path'] = explode('/', $url['path']);
78      if ($url['path'][1] != 'video') return false;
79      $video['id'] = $url['path'][2];
80     
81      $api_url = 'https://api.dailymotion.com/video/'.$video['id'].'?fields=description,id,thumbnail_large_url,title,owner.username'; // DM doesn't accept non secure connection
82      $json = download_remote_file($api_url, true);
83      if ($json === false or $json == 'file_error') return false;
84     
85      $json = json_decode($json, true);
86      $json['thumbnail_large_url'] = preg_replace('#\?([0-9]+)$#', null, $json['thumbnail_large_url']);
87     
88      $video = array_merge($video, array(
89        'id' => $json['id'],
90        'title' => $json['title'],
91        'description' => $json['description'],
92        'thumbnail' => $json['thumbnail_large_url'],
93        'author' => $json['owner.username'],
94        ));
95      break;
96    }
97     
98    /* wat */
99    case 'wat':
100    {
101      $video['type'] = 'wat';
102     
103      $html = download_remote_file($source_url, true);
104      if ($html === false or $html == 'file_error') return false;
105     
106      preg_match('#<meta property="og:video" content="http://www.wat.tv/swf2/([^"/>]+)" />#', $html, $matches);
107      if (empty($matches[1])) return false;
108      $video['id'] = $matches[1];
109     
110      preg_match('#<meta name="name" content="([^">]*)" />#', $html, $matches);
111      $video['title'] = $matches[1];
112     
113      preg_match('#<p class="description"([^>]*)>(.*?)</p>#s', $html, $matches);
114      $video['description'] = $matches[2];
115     
116      preg_match('#<meta property="og:image" content="([^">]+)" />#', $html, $matches);
117      $video['thumbnail'] = $matches[1];
118     
119      $video['author'] = null;
120      break;
121    }
122     
123    /* wideo */
124    case 'wideo':
125    {
126      $video['type'] = 'wideo';
127     
128      $url['path'] = explode('/', $url['path']);
129      $video['id'] = rtrim($url['path'][2], '.html');
130     
131      $html = download_remote_file($source_url, true);
132      if ($html === false or $html == 'file_error') return false;
133           
134      preg_match('#<meta property="og:title" content="([^">]*)" />#', $html, $matches);
135      $video['title'] = $matches[1];
136     
137      preg_match('#<meta property="og:description" content="([^">]*)" />#', $html, $matches);
138      $video['description'] = $matches[1];
139     
140      preg_match('#<meta property="og:image" content="([^">]+)" />#', $html, $matches);
141      $video['thumbnail'] = $matches[1];
142     
143      preg_match('#<li id="li_author">Auteur :  <a href="\#"([^>]*)><span>(.*?)</span></a>#', $html, $matches);
144      $video['author'] = $matches[2];
145      break;
146    }
147     
148    /* videobb */
149    case 'videobb':
150    {
151      $video['type'] = 'videobb';
152     
153      if (!empty($url['query']))
154      {
155        parse_str($url['query'], $url['query']);
156        if (empty($url['query']['v'])) return false;
157        $video['id'] = $url['query']['v'];
158      }
159      else
160      {
161        $url['path'] = explode('/', $url['path']);
162        if ($url['path'][1] != 'video') return false;
163        $video['id'] = $url['path'][2];
164      }
165     
166      $html = download_remote_file($source_url, true);
167      if ($html === false or $html == 'file_error') return false;
168           
169      preg_match('#<meta content="videobb - ([^">]*)"  name="title" property="" />#', $html, $matches);
170      $video['title'] = $matches[1];
171     
172      $video['description'] = null;
173     
174      preg_match('#<link rel="image_src" href="([^">]+)" type="image/jpeg" />#', $html, $matches);
175      $video['thumbnail'] = $matches[1];
176     
177      $video['author'] = null;
178      break;
179    }
180     
181    default:
182      return false;   
183  }
184 
185  return $video;
186}
187
188/**
189 * download a remote file
190 *  - needs cURL or allow_url_fopen
191 *  - take care of SSL urls
192 *
193 * @param: string source url
194 * @param: mixed destination file (if true, file content is returned)
195 */
196if (!function_exists('download_remote_file'))
197{
198  function download_remote_file($src, $dest)
199  {
200    if (empty($src))
201    {
202      return false;
203    }
204   
205    $return = ($dest === true) ? true : false;
206   
207    /* curl */
208    if (function_exists('curl_init'))
209    {
210      if (!$return)
211      {
212        $newf = fopen($dest, "wb");
213      }
214      $ch = curl_init();
215     
216      curl_setopt($ch, CURLOPT_URL, $src);
217      curl_setopt($ch, CURLOPT_HEADER, 0);
218      curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept-language: en"));
219      curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
220      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
221      curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
222      if (strpos($src, 'https://') !== false)
223      {
224        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
225        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
226      }
227      if (!$return)
228      {
229        curl_setopt($ch, CURLOPT_FILE, $newf);
230      }
231      else
232      {
233        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
234      }
235     
236      if (($out = curl_exec($ch)) === false)
237      {
238        return 'file_error';
239      }
240     
241      curl_close($ch);
242     
243      if (!$return)
244      {
245        fclose($newf);
246        return true;
247      }
248      else
249      {
250        return $out;
251      }
252    }
253    /* file get content */
254    else if (ini_get('allow_url_fopen'))
255    {
256      if (strpos($src, 'https://') !== false and !extension_loaded('openssl'))
257      {
258        return false;
259      }
260     
261      $opts = array(
262        'http' => array(
263          'method' => "GET",
264          'user_agent' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
265          'header' => "Accept-language: en",
266        )
267      );
268
269      $context = stream_context_create($opts);
270     
271      if (($file = file_get_contents($src, false, $context)) === false)
272      {
273        return 'file_error';
274      }
275     
276      if (!$return)
277      {
278        file_put_contents($dest, $file);
279        return true;
280      }
281      else
282      {
283        return $file;
284      }
285    }
286   
287    return false;
288  }
289}
290
291/**
292 * and film frame to an image (need GD library)
293 * @param: string source
294 * @param: string destination (if null, the source si modified)
295 * @return: void
296 */
297function add_film_frame($src, $dest=null)
298{
299  if (empty($dest))
300  {
301    $dest = $src;
302  }
303 
304  // we need gd library
305  if (!function_exists('imagecreatetruecolor'))
306  {
307    if ($dest != $src) copy($src, $dest);
308    return;
309  }
310 
311  // open source image
312  switch (strtolower(get_extension($src)))
313  {
314    case 'jpg':
315    case 'jpeg':
316      $srcImage = imagecreatefromjpeg($src);
317      break;
318    case 'png':
319      $srcImage = imagecreatefrompng($src);
320      break;
321    case 'gif':
322      $srcImage = imagecreatefromgif($src);
323      break;
324    default:
325      if ($dest != $src) copy($src, $dest);
326      return;
327  }
328 
329  // source properties
330  $srcWidth = imagesx($srcImage);
331  $srcHeight = imagesy($srcImage);
332  $const = intval($srcWidth * 0.04);
333  $bandRadius = floor($const/8);
334
335  // band properties
336  $imgBand = imagecreatetruecolor($srcWidth + 6*$const, $srcHeight + 3*$const);
337 
338  $black = imagecolorallocate($imgBand, 0, 0, 0);
339  $white = imagecolorallocate($imgBand, 245, 245, 245);
340 
341  // and dots
342  $y_start = intval(($srcHeight + 3*$const) / 2);
343  $aug = intval($y_start / 5) + 1;
344  $i = 0;
345
346  while ($y_start + $i*$aug < $srcHeight + 3*$const)
347  {
348    imagefilledroundrectangle($imgBand, (3/4)*$const, $y_start + $i*$aug - $const/2, (9/4)*$const - 1, $y_start + $i*$aug + $const/2 - 1, $white, $bandRadius);
349    imagefilledroundrectangle($imgBand, (3/4)*$const, $y_start - $i*$aug - $const/2, (9/4)*$const - 1, $y_start - $i*$aug + $const/2 - 1, $white, $bandRadius);
350
351    imagefilledroundrectangle($imgBand, $srcWidth + (15/4)*$const, $y_start + $i*$aug - $const/2, $srcWidth + (21/4)*$const - 1, $y_start + $i*$aug + $const/2 - 1, $white, $bandRadius);
352    imagefilledroundrectangle($imgBand, $srcWidth + (15/4)*$const, $y_start - $i*$aug - $const/2, $srcWidth + (21/4)*$const - 1, $y_start - $i*$aug + $const/2 - 1, $white, $bandRadius);
353
354    ++$i;
355  }
356
357  // add source to band
358  imagecopy($imgBand, $srcImage, 3*$const, (3/2)*$const, 0, 0, $srcWidth, $srcHeight);
359 
360  // save image
361  switch (strtolower(get_extension($dest)))
362  {
363    case 'jpg':
364    case 'jpeg':
365      imagejpeg($imgBand, $dest, 85);
366      break;
367    case 'png':
368      imagepng($imgBand, $dest);
369      break;
370    case 'gif':
371      imagegif($imgBand, $dest);
372      break;
373  }
374}
375
376/**
377 * create a rectangle with round corners
378 * http://www.php.net/manual/fr/function.imagefilledrectangle.php#42815
379 */
380function imagefilledroundrectangle(&$img, $x1, $y1, $x2, $y2, $color, $radius)
381{
382  imagefilledrectangle($img, $x1+$radius, $y1, $x2-$radius, $y2, $color);
383 
384  if ($radius > 0)
385  {
386    imagefilledrectangle($img, $x1, $y1+$radius, $x2, $y2-$radius, $color);
387    imagefilledellipse($img, $x1+$radius, $y1+$radius, $radius*2, $radius*2, $color);
388    imagefilledellipse($img, $x2-$radius, $y1+$radius, $radius*2, $radius*2, $color);
389    imagefilledellipse($img, $x1+$radius, $y2-$radius, $radius*2, $radius*2, $color);
390    imagefilledellipse($img, $x2-$radius, $y2-$radius, $radius*2, $radius*2, $color);
391  }
392}
393
394?>
Note: See TracBrowser for help on using the repository browser.