dari88's diary

これから趣味にするプログラミング/PHP/javascript/kohana/CMS/web design/

kohanaのテスト13・・・ヤマレコビューワーを作ってみた

 ヤマレコは登山愛好家のコミュニティーサイトです。今日はヤマレコに蓄積された膨大な写真を全くランダムに選択して表示するコードです。

 

ヤマレコビューワー

・kohana/application/classes/controller/test13.php

<?php defined('SYSPATH') OR die('No direct access allowed.');

class Controller_Test13 extends Controller {

    public function action_index() {

        if (isset($_GET['getframe'])) {
            for ($i = 1; $i < 10; $i++) {

                mt_srand();
                $rand = mt_rand(50001, 196250);
                $yamareco = 'http://www.yamareco.com/modules/yamareco/detail-' . $rand . '.html';

                $subject = http_parse_message(http_get($yamareco))->body;

                if ($subject == FALSE)
                    continue;

                preg_match_all("#http.+modules/yamareco/include/tmp_imgresize.+'#", $subject, $matches);
                $matches = $matches[0];

                if (!$matches)
                    continue;

                $rand = mt_rand(0, count($matches) - 1);
                $url = $matches[$rand];
                $url = preg_replace("/'/", '', $url);
                $url = preg_replace("/maxsize=800/", 'maxsize=800', $url);

                echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>';
                echo '<body><img src="' . $url . '" alt="yamareco"></body>';
                echo '</html>';
                exit();
            }

            echo 'Time out!';
            
        } else {
            $view = view::factory('test13');
            $this->response->body($view);
        }
    }

}

?>

・kohana/application/views/test13

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>山の写真</title>
        <script type="text/javascript"><!--
            function changeImg(){
                document.getElementById('change_img').click();
            }
            function setTimer(){
                document.getElementById('change_img').click();
                imgTimer = setInterval("changeImg()",10*1000);
            }
            function clearTimer(){
                clearInterval(imgTimer);
            }
            // --></script>
    </head>
    <body bgcolor=Gray>
        <p>ヤマレコの写真</p>
        <iframe name="frame1" src="test13?getframe=1" width="830" height="650" frameborder="0"></iframe>

        <form action="test13" method="GET" target="frame1">
            <input type="submit" value="更新" name="getframe" id="change_img"/>
            <input type="button" value="自動更新" onClick="setTimer()"/>
            <input type="button" value="停止" onClick="clearTimer()"/>
        </form>

    </body>
</html>

 

動作試験

 これを眺めていると、やはり山に行きたくなります・・・(^^;;;