-/**
- * Because some f*cking services like flickr require an extra HTTP request to get the thumbnail URL,
- * I have deported the thumbnail URL code generation here, otherwise this would slow down page generation.
- * The following function takes the URL a link (e.g. a flickr page) and return the proper thumbnail.
- * This function is called by passing the URL:
- * http://mywebsite.com/shaarli/?do=genthumbnail&hmac=[HMAC]&url=[URL]
- * [URL] is the URL of the link (e.g. a flickr page)
- * [HMAC] is the signature for the [URL] (so that these URL cannot be forged).
- * The function below will fetch the image from the webservice and store it in the cache.
- *
- * @param ConfigManager $conf Configuration Manager instance,
- */
-function genThumbnail($conf)
-{
- // Make sure the parameters in the URL were generated by us.
- $sign = hash_hmac('sha256', $_GET['url'], $conf->get('credentials.salt'));
- if ($sign!=$_GET['hmac']) die('Naughty boy!');
-
- $cacheDir = $conf->get('resource.thumbnails_cache', 'cache');
- // Let's see if we don't already have the image for this URL in the cache.
- $thumbname=hash('sha1',$_GET['url']).'.jpg';
- if (is_file($cacheDir .'/'. $thumbname))
- { // We have the thumbnail, just serve it:
- header('Content-Type: image/jpeg');
- echo file_get_contents($cacheDir .'/'. $thumbname);
- return;
- }
- // We may also serve a blank image (if service did not respond)
- $blankname=hash('sha1',$_GET['url']).'.gif';
- if (is_file($cacheDir .'/'. $blankname))
- {
- header('Content-Type: image/gif');
- echo file_get_contents($cacheDir .'/'. $blankname);
- return;
- }
-
- // Otherwise, generate the thumbnail.
- $url = $_GET['url'];
- $domain = parse_url($url,PHP_URL_HOST);
-
- if ($domain=='flickr.com' || endsWith($domain,'.flickr.com'))
- {
- // Crude replacement to handle new flickr domain policy (They prefer www. now)
- $url = str_replace('http://flickr.com/','http://www.flickr.com/',$url);
-
- // Is this a link to an image, or to a flickr page ?
- $imageurl='';
- if (endsWith(parse_url($url, PHP_URL_PATH), '.jpg'))
- { // This is a direct link to an image. e.g. http://farm1.staticflickr.com/5/5921913_ac83ed27bd_o.jpg
- preg_match('!(http://farm\d+\.staticflickr\.com/\d+/\d+_\w+_)\w.jpg!',$url,$matches);
- if (!empty($matches[1])) $imageurl=$matches[1].'m.jpg';
- }
- else // This is a flickr page (html)
- {
- // Get the flickr html page.
- list($headers, $content) = get_http_response($url, 20);
- if (strpos($headers[0], '200 OK') !== false)
- {
- // flickr now nicely provides the URL of the thumbnail in each flickr page.
- preg_match('!<link rel=\"image_src\" href=\"(.+?)\"!', $content, $matches);
- if (!empty($matches[1])) $imageurl=$matches[1];
-
- // In albums (and some other pages), the link rel="image_src" is not provided,
- // but flickr provides:
- // <meta property="og:image" content="http://farm4.staticflickr.com/3398/3239339068_25d13535ff_z.jpg" />
- if ($imageurl=='')
- {
- preg_match('!<meta property=\"og:image\" content=\"(.+?)\"!', $content, $matches);
- if (!empty($matches[1])) $imageurl=$matches[1];
- }
- }
- }
-
- if ($imageurl!='')
- { // Let's download the image.
- // Image is 240x120, so 10 seconds to download should be enough.
- list($headers, $content) = get_http_response($imageurl, 10);
- if (strpos($headers[0], '200 OK') !== false) {
- // Save image to cache.
- file_put_contents($cacheDir .'/'. $thumbname, $content);
- header('Content-Type: image/jpeg');
- echo $content;
- return;
- }
- }
- }
-
- elseif ($domain=='vimeo.com' )
- {
- // This is more complex: we have to perform a HTTP request, then parse the result.
- // Maybe we should deport this to JavaScript ? Example: http://stackoverflow.com/questions/1361149/get-img-thumbnails-from-vimeo/4285098#4285098
- $vid = substr(parse_url($url,PHP_URL_PATH),1);
- list($headers, $content) = get_http_response('https://vimeo.com/api/v2/video/'.escape($vid).'.php', 5);
- if (strpos($headers[0], '200 OK') !== false) {
- $t = unserialize($content);
- $imageurl = $t[0]['thumbnail_medium'];
- // Then we download the image and serve it to our client.
- list($headers, $content) = get_http_response($imageurl, 10);
- if (strpos($headers[0], '200 OK') !== false) {
- // Save image to cache.
- file_put_contents($cacheDir .'/'. $thumbname, $content);
- header('Content-Type: image/jpeg');
- echo $content;
- return;
- }
- }
- }
-
- elseif ($domain=='ted.com' || endsWith($domain,'.ted.com'))
- {
- // The thumbnail for TED talks is located in the <link rel="image_src" [...]> tag on that page
- // http://www.ted.com/talks/mikko_hypponen_fighting_viruses_defending_the_net.html
- // <link rel="image_src" href="http://images.ted.com/images/ted/28bced335898ba54d4441809c5b1112ffaf36781_389x292.jpg" />
- list($headers, $content) = get_http_response($url, 5);
- if (strpos($headers[0], '200 OK') !== false) {
- // Extract the link to the thumbnail
- preg_match('!link rel="image_src" href="(http://images.ted.com/images/ted/.+_\d+x\d+\.jpg)"!', $content, $matches);
- if (!empty($matches[1]))
- { // Let's download the image.
- $imageurl=$matches[1];
- // No control on image size, so wait long enough
- list($headers, $content) = get_http_response($imageurl, 20);
- if (strpos($headers[0], '200 OK') !== false) {
- $filepath = $cacheDir .'/'. $thumbname;
- file_put_contents($filepath, $content); // Save image to cache.
- if (resizeImage($filepath))
- {
- header('Content-Type: image/jpeg');
- echo file_get_contents($filepath);
- return;
- }
- }
- }
- }
- }
-
- elseif ($domain=='xkcd.com' || endsWith($domain,'.xkcd.com'))
- {
- // There is no thumbnail available for xkcd comics, so download the whole image and resize it.
- // http://xkcd.com/327/
- // <img src="http://imgs.xkcd.com/comics/exploits_of_a_mom.png" title="<BLABLA>" alt="<BLABLA>" />
- list($headers, $content) = get_http_response($url, 5);
- if (strpos($headers[0], '200 OK') !== false) {
- // Extract the link to the thumbnail
- preg_match('!<img src="(http://imgs.xkcd.com/comics/.*)" title="[^s]!', $content, $matches);
- if (!empty($matches[1]))
- { // Let's download the image.
- $imageurl=$matches[1];
- // No control on image size, so wait long enough
- list($headers, $content) = get_http_response($imageurl, 20);
- if (strpos($headers[0], '200 OK') !== false) {
- $filepath = $cacheDir.'/'.$thumbname;
- // Save image to cache.
- file_put_contents($filepath, $content);
- if (resizeImage($filepath))
- {
- header('Content-Type: image/jpeg');
- echo file_get_contents($filepath);
- return;
- }
- }
- }
- }
- }
-
- else
- {
- // For all other domains, we try to download the image and make a thumbnail.
- // We allow 30 seconds max to download (and downloads are limited to 4 Mb)
- list($headers, $content) = get_http_response($url, 30);
- if (strpos($headers[0], '200 OK') !== false) {
- $filepath = $cacheDir .'/'.$thumbname;
- // Save image to cache.
- file_put_contents($filepath, $content);
- if (resizeImage($filepath))
- {
- header('Content-Type: image/jpeg');
- echo file_get_contents($filepath);
- return;
- }
- }
- }
-
-
- // Otherwise, return an empty image (8x8 transparent gif)
- $blankgif = base64_decode('R0lGODlhCAAIAIAAAP///////yH5BAEKAAEALAAAAAAIAAgAAAIHjI+py+1dAAA7');
- // Also put something in cache so that this URL is not requested twice.
- file_put_contents($cacheDir .'/'. $blankname, $blankgif);
- header('Content-Type: image/gif');
- echo $blankgif;
-}
-
-// Make a thumbnail of the image (to width: 120 pixels)
-// Returns true if success, false otherwise.
-function resizeImage($filepath)
-{
- if (!function_exists('imagecreatefromjpeg')) return false; // GD not present: no thumbnail possible.
-
- // Trick: some stupid people rename GIF as JPEG... or else.
- // So we really try to open each image type whatever the extension is.
- $header=file_get_contents($filepath,false,NULL,0,256); // Read first 256 bytes and try to sniff file type.
- $im=false;
- $i=strpos($header,'GIF8'); if (($i!==false) && ($i==0)) $im = imagecreatefromgif($filepath); // Well this is crude, but it should be enough.
- $i=strpos($header,'PNG'); if (($i!==false) && ($i==1)) $im = imagecreatefrompng($filepath);
- $i=strpos($header,'JFIF'); if ($i!==false) $im = imagecreatefromjpeg($filepath);
- if (!$im) return false; // Unable to open image (corrupted or not an image)
- $w = imagesx($im);
- $h = imagesy($im);
- $ystart = 0; $yheight=$h;
- if ($h>$w) { $ystart= ($h/2)-($w/2); $yheight=$w/2; }
- $nw = 120; // Desired width
- $nh = min(floor(($h*$nw)/$w),120); // Compute new width/height, but maximum 120 pixels height.
- // Resize image:
- $im2 = imagecreatetruecolor($nw,$nh);
- imagecopyresampled($im2, $im, 0, 0, 0, $ystart, $nw, $nh, $w, $yheight);
- imageinterlace($im2,true); // For progressive JPEG.
- $tempname=$filepath.'_TEMP.jpg';
- imagejpeg($im2, $tempname, 90);
- imagedestroy($im);
- imagedestroy($im2);
- unlink($filepath);
- rename($tempname,$filepath); // Overwrite original picture with thumbnail.
- return true;