Last active
September 23, 2019 16:18
-
-
Save siddartha/5270083 to your computer and use it in GitHub Desktop.
Function to show and/or write thumbnails generated by Mshots from WP.org
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Display thumbs generate by Wordpress' service Mshot and write | |
* locally the thumb after generation to have better loading web performance. | |
* | |
* Thx @jd_ma for help :) | |
*/ | |
function show_and_write_thumbnail ($url, $width=200, $height=150) { | |
// Root path of the thumbnails (should be create before use) | |
$img_dir ='screenshots/'; | |
// get clean hostname of the website without http(s):// and / in the end of url | |
$host = parse_url($url); | |
$img_name = $host['host']; | |
// Full path of the local copy of the thumbshot | |
$img_path = $img_dir.$img_name.'.png'; | |
// Time during we let the call to wordpress.com before call local copy | |
$expire= time() - 600; // 10mn delay (need 30s max to mshots or apercite, but can be overloaded sometimes) | |
// Verify if file exists otherwise call the mshots and make a local copy | |
if (file_exists ($img_path) && filemtime($img_path) > $expire) { | |
$thumbnail = '<img src="'.$img_path.'" alt="screenshot '.$img_name.'" />'; | |
} else { | |
$thumb= 'http://s.wordpress.com/mshots/v1/'.urlencode($url).'?w='.$width.'&h='.$height; | |
$get= file_get_contents($thumb); | |
file_put_contents($img_path, $get); | |
$thumbnail = '<img src="http://s.wordpress.com/mshots/v1/'.urlencode($url).'?w='.$width.'&h='.$height.'" alt="screenshot '.$img_name.'"/>'; | |
} | |
//return thumb | |
print $thumbnail; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment