CentOS5 で WEBサイトのスクリーンショット
13 6 月 2008
ImageMagick と firefoxを利用してスクリーンショットを作りたいと思います。
ダウンロード&インストール
ScreenShot で必要なものをyum でインストールする。 1 画像処理に必要な ImageMagick を yum でインストール。 # yum -y install ImageMagick 2 Xvfb 仮想フレームバッファ を yum でインストール。 # yum -y install xorg-x11-server-Xvfb 3 サーバー環境であれば firefox を yum でインストール。 # yum -y install firefox 4 仮想フレームを常時起動させるのであれば ※ 手動は 5 を参照 # vi /etc/rc.d/init.d/xvfb -------------------------ここから------------------------- #!/bin/bash # # chkconfig: - 91 35 # description: Starts and stops XVfb. \ # used to provide virtual frame buffer. # Source function library. . /etc/init.d/functions prog=$"Xvfb" # Xvfb program XVFB=/usr/bin/Xvfb start() { echo -n $"Starting $prog: " daemon /usr/bin/Xvfb :1 -screen 0 1024x800x24 & echo } stop() { echo -n $"Shutting down $prog: " killproc Xvfb echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; restart|reload) stop start ;; *) echo $"Usage: $0 {start|stop|restart}" exit 1 esac exit 0 -------------------------ここまで------------------------- ※ 1024x1024x24 は好きなサイズに変更してください。 :1 は仮想ディスプレイ番号 # chmod 755 /etc/rc.d/init.d/xvfb # /etc/rc.d/init.d/xvfb start ※ スタートさせたときにフォント系のエラーが出るかもしれませんが、取り合えず無視です。 # /sbin/chkconfig --add xvfb # /sbin/chkconfig xvfb on # /sbin/chkconfig --list xvfb xvfb 0:off 1:off 2:on 3:on 4:on 5:on 6:off 5 仮想フレームを手動起動させるのであれば 起動するときは # /usr/bin/Xvfb :1 -screen 0 1024x800x24 & 止めるときは # killall Xvfb ※ 1024x800x24 は好きなサイズに変更してください。 :1 は仮想ディスプレイ番号 以上です。 |
これで環境が整いました。
簡単な説明です。
手動でサムネイルをとるとき この色の文字は、スクリーンショットをとるサイトのURL この色の文字は、スクリーンショットをとるサイトの画像名 この色の文字は、編集後の画像名 なので、こんな感じ firefox -createprofile "webshot /tmp/" ← 一回でいい firefox -display :1 -P "webshot" "サイトURL" & import -display :1 -window root 画像名 & convert -crop 994x644+0+95 画像名 画像名 ↑ 元画像を残さないとき convert -crop 994x644+0+95 画像名 画像名 ↑ 元画像を残すとき killall firefox-bin ※ convert はしてもしなくてもどちらでも。 convert しないと、ツールバー、ステータスバー もろとも写ってしまう。 convert すると、サイズによって画面のみが写る状態になる。 994x644+0+95 は各自の環境に合わせる。 一連の流れはこんな感じ 仮想ディスプレイ番号を指定する。 $ export DISPLAY=:1.0 webshot用プロファイルを作成 $ firefox -createprofile "webshot /tmp/" Success: created profile 'webshot /tmp/' URL を指定して firefox を起動 $ firefox -display :1 -P "webshot" "http://server.lunq.net/" & [1] 6120 ※ 起動した後は十分な時間をとる (大体の目安で) じゃないと、表示しきれていないことがある。 スクリーンショットをインポート $ import -display :1 -window root server.png 要らない部分を削除 $ convert -crop 994x644+0+95 server.png t_server.png できているかの確認 $ ls -al 合計 12 ・ -rw-rw-r-- 1 server server 241981 6月 13 15:45 server.png -rw-rw-r-- 1 server server 206985 6月 13 15:46 t_server.png ・ firefox を閉じる $ killall firefox-bin /usr/lib/firefox-1.5.0.12/run-mozilla.sh: line 131: 6147 終了しました "$prog" ${1+"$@"} [1]+ Exit 143 firefox -display :1 -P "webshot" "http://server.lunq.net/" firefox が閉じられているかの確認。 $ ps -al F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 0 S 500 6046 1 0 78 0 - 1888 - pts/1 00:00:00 gconfd-2 0 R 500 6192 29062 0 77 0 - 1282 - pts/1 00:00:00 ps 自動で複数のサイトのスクリーンショットをとるとき まずスクリプトを作成する。 # vi webshot.sh -------------------------ここから------------------------- #!/bin/bash export DISPLAY=:1.0 xhost localhost firefox -createprofile "webshot /tmp/" # カレントディレクトリのファイル名リストを配列に格納 files=(`cat ./weblist`) # インデックスに @ を指定して、全ての要素を for 文の値リストに指定 for file in ${files[@]} do name=`echo $file | sed 's/\///g'` name=`echo $name | sed -e "s/\./_/g"` name=`echo $name | sed -e "s/~/-/g"` name=`echo $name | sed 's/http://g'` img=$name.png thuimg=t_$name.png firefox -display :1 -P "webshot" "$file" & sleep 10 import -display :1 -window root $img & sleep 3 convert -crop 994x644+0+95 $img $thuimg & sleep 3 done echo "The END" exit 0 -------------------------ここまで------------------------- sleep をいれないとエラーで画像が作られないことがある。 長さは各々好きなように (そのぶん時間がかかる) 実行権付与 # chmod 755 webshot.sh スクリーンショットを作成したいURLのリストを作る。 # echo "http://server.lunq.net/" >> weblist # echo "http://www.lunq.net/" >> weblist # echo "http://www.google.com/" >> weblist # echo "http://www.yahoo.co.jp/" >> weblist リスとの確認をする。 # cat weblist http://server.lunq.net/ http://www.lunq.net/ http://www.google.com/ http://www.yahoo.co.jp/ スクリプトを実行する。 # ./webshot.sh localhost being added to access control list Success: created profile 'webshot /tmp/' The END /usr/lib/firefox-1.5.0.12/run-mozilla.sh: line 131: 8231 終了しました "$prog" ${1+"$@"} ※ 上記の例では約1分ちょいかかる 画像を確認してみる。 # ls -al ./ ・ -rw-rw-r-- 1 server server 242079 6月 13 16:28 server_lunq_net.png -rw-rw-r-- 1 server server 207071 6月 13 16:28 t_server_lunq_net.png -rw-rw-r-- 1 server server 102559 6月 13 16:29 t_www_google_com.png -rw-rw-r-- 1 server server 228232 6月 13 16:28 t_www_lunq_net.png -rw-rw-r-- 1 server server 427138 6月 13 16:29 t_www_yahoo_co_jp.png -rw-rw-r-- 1 server server 135738 6月 13 16:29 www_google_com.png -rw-rw-r-- 1 server server 263495 6月 13 16:28 www_lunq_net.png -rw-rw-r-- 1 server server 468638 6月 13 16:29 www_yahoo_co_jp.png ・ OK! 画像名は、上記のスクリプトで 「 http: 」は削除 「 / (スラッシュ) 」は削除 「 . (ドット) 」は「 _ (アンダーバー) 」 「 ~ (チルダ) 」は「 - (ハイフン) 」 に変更している。 |
出来上がった画像はこんな感じ
Comments are closed.








