何かとサイトサムネイルで重宝するMozshotですが、
サムネイル表示のために、毎回URLの指定やソースの調整をすることがだんだん苦になってきました。
簡単にサイトサムネルを表示するファンクションのサンプルがあったので自分なりに改造し、
指定URLのタイトル取得とMozshotのサムネイルにリンクを組み合わせてみました。
<?php /*---- 指定したURLのソースを取得 ----*/ function getURL( $pURL ) { $_data = null; if( $_http = @fopen( $pURL, "r" ) ) { while( !feof( $_http ) ) { $_data .= fgets( $_http, 1024 ); } fclose( $_http ); } return( $_data ); } /*---- ソース内にある余計な改行コードを取り除く----*/ function cleanString( $pString ) { $_data = str_replace( array( chr(10), chr(13), chr(9) ), chr(32), $pString ); while( strpos( $_data, str_repeat( chr(32), 2 ), 0 ) != false ) { $_data = str_replace( str_repeat( chr(32), 2 ), chr(32), $_data ); } return( trim( $_data ) ); } /*---- 指定した最初タグ内にある文字列を取得 ----*/ function getElement( $pElement, $pSource ) { //取得できなかった場合の代替テキスト if(empty($pSource)){ $pSource = chr(60).$pElement.chr(62)."Pages".chr(60)."/".$pElement.chr(62); } $_data = null; $pElement = strtolower( $pElement ); $_start = strpos( strtolower( $pSource ), chr(60) . $pElement, 0 ); $_start = strpos( $pSource, chr(62), $_start ) + 1; $_stop = strpos( strtolower( $pSource ), "</" . $pElement . chr(62), $_start ); if( $_start > strlen( $pElement ) && $_stop > $_start ) { $_data = trim( substr( $pSource, $_start, $_stop - $_start ) ); } return( $_data ); } /*---- Mozshot用ファンクション ---*/ function mShot($url,$s){ if ($url) { $sData = getURL( $url ); $sData = cleanString( $sData ); $sData = getElement( "title", $sData ); mb_language( 'Japanese' ); $title = mb_convert_encoding($sData, "UTF-8", "auto"); } $imgSize[0] = '64'; $imgSize[1] = '128'; $imgSize[2] = '256'; $thumbUrl[0] = "/small"; $thumbUrl[1] = ""; $thumbUrl[2] = "/large"; $a = "<div id=¥"mShot¥"><a href=¥"".$url."¥" target=¥"_blank¥">¥n"; $a = $a."<strong class=¥"title¥">".$title."</strong><br />¥n"; $a = $a."<span class=¥"url¥">".$url."</span><br />¥n" ; $a = $a."<img src=¥"http://mozshot.nemui.org/shot".$thumbUrl[$s]."?".$url."¥" width=¥"".$imgSize[$s]."¥" height=¥"".$imgSize[$s]."¥" alt=¥"".$title."¥" />¥n"; $a = $a."</a></div>¥n" ; echo $a; } ?>
上記のfunction.phpをサムネイル表示を使うページindex.phpでインクルードして使う。
<?php include_once $_SERVER['DOCUMENT_ROOT']."/include/function/function.php";?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Mozshotファンクションテスト</title> <style type="text/css"> <!-- #mShot { padding: 0px; margin: 0px; } #mShot .title { font-weight: bold; } #mShot .url { font-size: small; } #mShot img { border: none; } --> </style> </head> <body> <?php mShot("http://qwerty.work",0);?> <?php mShot("http://qwerty.work",1);?> <?php mShot("http://qwerty.work",2);?> </body> </html>
このサイト用のCSSを適用しているので、
色は違いますが、実際に表示すると
<?php mShot("http://qwerty.work",0);?>の場合
<?php mShot("http://qwerty.work",1);?>の場合
<?php mShot("http://qwerty.work",2);?>の場合
となります。
参考ページ
2011/03/01追記
指定したURLのタイトルを取得する簡単な方法が見つかったのメモ。