カテゴリ:アプリ開発
各種phpスクリプトを http://etoh.minidns.net/php においています。
url2bmpとphpを使って同様のサーバを作ってみました。の第二弾です。imagecopyresampledを使ってリサイズイメージを高画質化するとともにエラー系の強化を図ってみました。 これで、http://hogehoge.jp/w2p.php?bx=400&by=400&url=http://www.uneune.jp とかすると、pngのイメージが表示されます。 以下ソース <?php // // GDライブラリの有効化と // http://www.pixel-technology.com/freeware/url2bmp/ が必要です。 // Copyright 2007 ETOH Hiroki.(http://etoh.minidns.net/php/) // // Usage: // http://hogehoge.jp/w2p.php?bx=400&by=400&url=http://www.uneune.jp // bx=横幅(pixel数) by=縦幅(pixel数) url=キャプチャしたいurl // 上記例ではwww.uneune.jpのurlを400x400のサイズでキャプチャします。 // // 履歴 // ver.0.2 2007.09.24 imagecopyresampledを使ってリサイズ画像の画質を高画質化 // エラールーチンの強化 // ver.0.1 2007.09.17 初版 // // 初期値(extractで書き換えられることを想定した変数) $bx=256; // bxが指定されなかったときの初期値 $by=256; // byが指定されなかったときの初期値 $url="http://www.msn.com"; // urlが指定されなかったときの初期値 // コマンドライン展開(上記初期値はコマンドラインにあればオーバライト) extract($_REQUEST, EXTR_OVERWRITE); // 初期値その2(extractで書き換えられると困る変数) $basex=1024; // webをキャプチャする元のサイズ $basey=1024; // $imgdir = dirname($_SERVER[PATH_TRANSLATED])."\\img\\"; // キャッシュ格納ディレクトリ $exepath = "C:\\task\\url2bmp.exe"; // 実行ファイルパス // 変なサイズが指定されたときははじく if ($bx >1024 || $by >1024 || $bx<1 || $by<1) { errorpng("Argument bx or by is/are out of range."); exit(0); } // ファイル名をハッシュ生成 $hashvalue = 'w2p_'.md5( $url.'bx'.$bx.'by'.$by ).'.png'; $output= $imgdir.$hashvalue; // ファイルがなければ、url2bmpを使ってurlを生成。 if (!file_exists($output)) { $tmp_output= $imgdir.$hashvalue.'.png'; $commandline = $exepath.' -url '.$url.' -file "'.$tmp_output.'" -format PNG -wx '.$basex.' -wy '.$basey.' -bx '.$basex.' -by '.$basey.' -wait 5 -removesb -notinteractive'; $last_line = system($commandline, $retval); $tmp_img = imagecreatefrompng($tmp_output); if (!$tmp_img) { errorpng("Failed to create webshot."); } $img = imagecreatetruecolor($bx, $by); imagecopyresampled( $img, $tmp_img, 0, 0, 0, 0, $bx, $by, 1024, 1024 ); if (!$img) { errorpng("Failed to resizing."); } unlink($tmp_output); imagedestroy($tmp_img); } else { $img = imagecreatefrompng($output); if (!$img) { errorpng("Failed to reloading image."); } } // GDを使ってリサイズ //画像出力 if ($img) { header("Content-type: image/png"); header("Cache-control: no-cache"); imagepng($img); imagepng($img, $output); // キャッシュ用にファイル出力しておく //画像消去(メモリから) imagedestroy($img); } // @brief エラー表示関数 // // function errorpng($str) { $img = imagecreatetruecolor(400, 30); /* 空の画像を作成 */ $bgc = imagecolorallocate($img, 255, 255, 255); $tc = imagecolorallocate($img, 0, 0, 0); imagefilledrectangle($img, 0, 0, 400, 30, $bgc); /* エラーメッセージを出力 */ imagestring($img, 3, 5, 5, $str, $tc); header("Content-type: image/png"); header("Cache-control: no-cache"); imagepng($img); exit(0); } ?> 上記、url2bmpのweb siteならこんな感じで取ってこれます。 ↓ お気に入りの記事を「いいね!」で応援しよう
Last updated
2007.09.24 17:17:48
[アプリ開発] カテゴリの最新記事
|
|