TAN'S Blog

if you obey all the rules you miss all the fun…


Leave a comment

Get Images in 16 : 9 Format

After lots of searching in Goggle, Stack exchange finally I am able to generate images in 16:9 format for youtube, dailymotion and vimeo. So lets have a look how can we simply get the thumb images from dailymotion, vimeo and youtube API.

Dailymotion

Step 1:
From the http://www.dailymotion.com/us if you will click on any video you will get the url in this format for that video:

http://www.dailymotion.com/video/<video_id>

Ex: Here is my url http://www.dailymotion.com/video/x267cnn_pree-two-feet-shy-indmusic-in-the-great-outdoors_music

Step 2:
To get the thumb image in 16:9 format, request dailymotion API in this format:

https://api.dailymotion.com/video/<video_id>?fields=thumbnail_360_url&thumbnail_ratio=widescreen

Ex: https://api.dailymotion.com/video/x267cnn_pree-two-feet-shy-indmusic-in-the-great-outdoors_music?fields=thumbnail_360_url&thumbnail_ratio=widescreen

Step 3:
In Step 2 you will get response like this: {“thumbnail_360_url”:”http:\/\/s2.dmcdn.net\/HphZA\/640×360-E1L.jpg”}

Reconstruct the thumbnail_360_url:
EX: http://s2.dmcdn.net/HPhZA/640×360-E1L.jpg

Image:

640x360-E1L

  640×360(16:9)



Vimeo

Step 1:
Get the vimeo video URL. Ex: http://vimeo.com/831365

Step 2:
To get data about a specific video, use the following url:

http://vimeo.com/api/v2/video/video_id.output

video_id : The ID of the video you want information for. output Specify the output type. VIMEO currently offer JSON, PHP, and XML formats.

So getting this URL http://vimeo.com/api/v2/video/831365.xml:


<videos>
  <video>
    [skipped]
    <thumbnail_small>http://i.vimeocdn.com/video/52914011_100x75.jpg</thumbnail_small>
    <thumbnail_medium>http://i.vimeocdn.com/video/52914011_200x150.jpg</thumbnail_medium>
    <thumbnail_large>http://i.vimeocdn.com/video/52914011_640.jpg</thumbnail_large>
   [skipped]
  </video>
</videos>

Parse this for every video to get the thumbnail.

Here’s approximate code in PHP:


<?php
  $videoid = 831365;

  $hash = unserialize(file_get_contents("http://vimeo.com/api/v2/video/$videoid.php"));

  $hash = $hash[0]['thumbnail_large'];

  $pieces = explode("_", $hash);

  echo "Your Image is here: " . $pieces[0] . "_640x360.jpg";
?>

O/P:
Your Image is here: http://i.vimeocdn.com/video/52914011_640x360.jpg

Image:-

52914011_640x360

     640×360(16:9)


Youtube

Step 1:
Get the youtube video url. Ex: http://www.youtube.com/watch?v=10OTg6lD4Jc

Step 2:
Request youtube API for 16:9 format:

http://img.youtube.com/vi/<video_id>/mqdefault.jpg or http://img.youtube.com/vi/<video_id>/maxresdefault.jpg

Ex(Medium quality image):
http://img.youtube.com/vi/10OTg6lD4Jc/mqdefault.jpg

Image:

mqdefault

320×180(16:9)

Ex(High quality image):
http://img.youtube.com/vi/10OTg6lD4Jc/maxresdefault.jpg

7788_91393518121913073

 1280×720(16:9)

References:
http://stackoverflow.com/questions/25740034/show-dailymotion-video-and-thumbnail-in-correct-169-format
http://stackoverflow.com/questions/2068344/how-do-i-get-a-youtube-video-thumbnail-from-the-youtube-api
http://stackoverflow.com/questions/1361149/get-img-thumbnails-from-vimeo

/* Hope You will love it */


Leave a comment

CACHING

What is caching?

Caching is an area of a computer’s memory devoted to temporarily storing recently used information. The content, which includes HTML pages, images, files and Web objects, is stored on the local hard drive in order to make it faster for the user to access it, which helps improve the efficiency of the computer and its overall performance.

Most caching occurs without the user knowing about it. For example, when a user returns to a Web page they have recently accessed, the browser can pull those files from the cache instead of the original server because it has stored the user’s activity. The storing of that information saves the user time by getting to it faster, and lessens the traffic on the network.

Caching Mechanism:

Drawing21

Very Simple Example to learn caching in PHP:

Step One: Create the top-cache.php file:

We need to create two files. Here’s the first one: Create a new file named top-cache.php and paste the code below in it.

<?php
  $url = $_SERVER["SCRIPT_NAME"];
  $break = Explode('/', $url);
  $file = $break[count($break) - 1];
  $cachefile = 'cached-'.substr_replace($file ,"",-4).'.html';
  $cachetime = 18000; //in seconds

  // Serve from the cache if it is younger than $cachetime
  if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
      echo "<!-- Cached copy, generated ".date('H:i', filemtime($cachefile))." -->\n";
      include($cachefile);
      exit;
  }
  ob_start(); // Start the output buffer
?>

So, what this code does? The first 5 lines create the cached file name according to the current php file. So, if you’re using a file named index.php, the cached file will be named cached-index.html.

Line 6 creates a $cachetime(in seconds) variable which determines the life of the cache. You can experiment by changing the time.

Lines 9 to 13 are a conditional statement which look for a file named $cachefile. If the file is found, a comment is inserted (line 10) and the $cachefile file is included. Then, the exit statement stops the execution of the script and the file is sent to the client brother. Which means that if a static file is found, no php code is interpreted by the server.

Line 14 creates a buffer, if the $cachefile file isn’t found. That’s all for the top-cache.php file.

Step two: Create the bottom-cache.php file:

Now, create a second php file, named bottom-cache.php and paste the code below in it.

 <?php
  // Cache the contents to a file
  $cached = fopen($cachefile, 'w');
  fwrite($cached, ob_get_contents());
  fclose($cached);
  ob_end_flush(); // Send the output to the browser
?>

If a file named $cachefile isn’t found on your server, this code is executed and create the file, so next time the page will be called, the $cachefile static file will be served to the client browser instead of executing the whole PHP file.

Step three: Include cache files on your page
Now that you have created the two necessary files, you simply have to include them on the php page you wish to cache. As you probably guessed, the top-cache.php file must be included in the beginning of your php page and the bottom-cache.php at the end, as shown below:

You can save this page as index.php.

<?php
  include('top-cache.php');

  // Your regular PHP code goes here.
  //You can uncomment the code and test it.
  /*
  for($i=1;$i<=100000;$i++) {
    echo "Hello world";
  }
  */

  include('bottom-cache.php');
?>

Now if you test the cache on a slow page, you’ll be amazed by how faster the page is.

Note: Give full read and write permission to the directory where you will put the above 3 files. For Windows OS no need to give file permission.
Ex: If this is my folder structure in ubuntu():

cac

Then you should give the read and write permission to caching folder.

When you will browse the page after uncommenting the code in step three “http://localhost/caching/index.php” for very 1st time you will see it will take time around 50-70ms. Next time you will see it will show the result in 17 to 25 ms. Its amazing.

Before caching(time taken 63 ms):

before

After Caching(time taken 18 ms):

after

Reference:

http://en.wikipedia.org/wiki/Cache_(computing)

http://www.catswhocode.com/blog/how-to-create-a-simple-and-efficient-php-cache

http://www.citrix.com/glossary/caching.html

/*Hope you will love it*/