node.jsをubuntuに入れた

インストールする時は最新情報を確認。自己責任で(当たり前だけどかいとこ)
今回は2011.08.17にでた最新版いれたので備忘録

% mkdir ~/work
% cd ~/work
% git clone --depth 1 git://github.com/joyent/node.git
% cd node
% git checkout v0.4.11
% export JOBS=2
% mkdir ~/local
% ./configure --prefix=$HOME/local/node
% make
% make install
  • 環境パス設定
% vi ~/.zshrc
export NODE_PATH=$HOME/local/node:$HOME/local/node/lib/node_modules
export PATH=$HOME/local/node/bin:$HOME/bin:$PATH
export MANPATH=$HOME/share/man:$MANPATH

% source ~/.zshrc
% cd ~/work
% git clone git://github.com/isaacs/npm.git ./npm
% cd npm
% git checkout origin/0.2
% make dev

node.jsでドラッグしてアップロードを試してみた

atsuya's posterousさんが作成したnode.jsでドラッグしてファイルアップロードできるtensoidを試してみた。


まずはgithubからダウンロード。ライセンスはMIT
https://github.com/atsuya/tensoid


買ったばっかりのMBAなのでnode.jsを入れるところから始める。

% brew install node



後はプログラムを実行するとエラーが出たのでライブラリを追加する
※ 共有してライブラリを利用するならパスを通しておくとよい

% curl http://npmjs.org/install.sh | sh
% npm install express
% npm install socket.io
% npm install log
% npm install ejs



logsディレクトリを作らないとアップロード出来ないので作る

% mkdir logs



起動する

% node app.js



以上。


コードは読み解いてないので不十分な理解ではあるがまとめてみる。

  • ファイルをドラッグしてアップロードするとURLが表示する。URLにアクセスするとダウンロードとなる。
  • 上記のURLに別ブラウザからアクセスするとダウンロードできない。
  • アップロードしたファイルの実体はローカルディレクトリには保存していない。

ということが解った。まだ開発中なのかな。

SSH多段接続の備忘録

SSHの多段接続をしたので備忘録
こういう条件の場合に便利
serverBに繋ぐには、serverAを経由しなければいけない。
serverAとserverBの両方には鍵がかかっている。多段接続を使うとローカルに鍵があれば両方の認証が可能。
つまりserverAに鍵を置く必要がないというわけだ。

% vi ~/.ssh/config

Host serverA
 HostName     192.168.100.10
 User         test
 IdentityFile ~/.ssh/serverA.rsa
Host serverB
 HostName     192.168.100.11
 User         test
 IdentityFile ~/.ssh/serverB.rsa
 ProxyCommand ssh serverA nc %h %p

// 接続
% ssh serverB

縦横比コードの修正

縦横比を固定で指定サイズのキャンバスに埋め込むのコードが拡大した場合に正しく処理出来ないので直してみた。

<?php
$path = 'test.jpg';
$frame_width = 75;
$frame_height = 75;
$info = getimagesize($path);
$src_width = $info[0];
$src_height = $info[1];
$src_image = imagecreatefromjpeg($path);

if ( $src_width > $src_height ){
	$width = round($src_width / $src_height * $frame_width);
	$dst_x = round(($frame_width - $width) / 3);
	$dst_y = 0;
	$src_x = 0;
	$src_y = 0;
	$dst_w = $width + $src_x;
	$dst_h = $frame_height;
} else {
	$height = round($src_height / $src_width * $frame_height);
	$dst_x = 0;
	$dst_y = round(($frame_height - $height) / 3);
	$src_x = 0;
	$src_y = 0;
	$dst_w = $frame_width;
	$dst_h = $height  + $dst_y;
}
$dst_image = imagecreatetruecolor($frame_width, $frame_height);
imagecopyresampled(
   $dst_image, $src_image,
   $dst_x, $dst_y, 
   0, 0,
   $dst_w, $dst_h, 
   $src_width, $src_height
);

header('Content-Type: image/jpg');
echo imagejpeg($dst_image, null, 100);
exit;

オリジナル 240x320 拡大 320x320
縮小 75x75

拡大で横に伸びると太っているように見えるがこんなものだろう。きっと。

上の計算処理をシンプルにするとこうかな。

function calc ($min,$max,$frame){
  $size = round($max / $min * $frame);
  $dst_xy = round(($frame - $size) / ($min / ($max - $min) ));
  $dst_wh = $size  + $dst_xy;
  return array($dst_xy,$dst_wh);
}

if ( $src_width > $src_height ){
  list($dst_x,$dst_w) = calc($src_height,$src_width,$frame_width);
  $dst_y = 0;
  $dst_h = $frame_height;
} else {
  list($dst_y,$dst_h) = calc($src_width,$src_height,$frame_height);
  $dst_x = 0;
  $dst_w = $frame_width;
}

$dst_image = imagecreatetruecolor($frame_width, $frame_height);
imagecopyresampled(
  $dst_image, $src_image,
  $dst_x, $dst_y, 
  0, 0,
  $dst_w, $dst_h, 
  $src_width, $src_height
);

フレームの縦横のサイズは同じでないとこの書式では成り立ってないな。。

画像を縦横比固定で指定サイズのキャンバスに埋め込む

縦横比を固定で指定サイズのキャンバスに埋め込む時、四隅は黒で画像を中央に持ってくるをやってみた。

$path = 'test.jpg';
$frame_width = 75;
$frame_height = 75;
$info = getimagesize($path);
$src_width = $info[0];
$src_height = $info[1];
$src_image = imagecreatefromjpeg($path);
if($src_width > $src_height){
  $width = ($src_width > $frame_width) ? $frame_width : $src_width;
  $height = round($src_height * $frame_width / $src_width);   
}else{
  $height = ($src_height > $frame_height) ? $frame_height : $src_height;
  $width = round($src_width * $frame_height / $src_height);
}
$x = round(($frame_width - $width) / 4);
$y = round(($frame_height - $height) / 4);

$dst_w = $frame_width - $x * 2;
$dst_h = $frame_height - $y * 2;

$dst_image = imagecreatetruecolor($frame_width, $frame_height);
imagecopyresampled(
  $dst_image, $src_image,
  $x, $y, 0, 0,
  $dst_w, $dst_h,
  $src_width, $src_height
);
header('Content-Type: image/jpg');
echo imagejpeg($dst_image, null, 100);
exit;

オリジナル


結果

追記 - 2011.05.21
サイズの小さい方をキャンバスに合わせるには上記コードの分岐を以下に変更すれば実現できる

if($src_width > $src_height){
↓
if($src_width < $src_height){

結果

Snow Leopard環境構築

iPhoneアプリとWEB連携をするのでMacにweb共有の設定を行った。

HomeBrew Install

$ ruby -e "$(curl -fsSLk https://gist.github.com/raw/323731/install_homebrew.rb)"
$ brew install subversion
$ brew install wget

ApachePHPを設定

$ sudo vi /etc/apache2/users/macmini.conf
<VirtualHost *:80>
DocumentRoot /Users/macmini/Sites
ServerName macmini.local

ErrorLog /Users/macmini/logs/error_log
LogLevel warn
customlog /Users/macmini/logs/access_log combined

<Directory "/Users/macmini/Sites/">
    Options Indexes MultiViews FollowSymlinks
    #AllowOverride None
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>
</VirtualHost>

$ sudo vi /etc/apache2/httpd.conf  
#LoadModule php5_module        libexec/apache2/libphp5.so
↓
LoadModule php5_module        libexec/apache2/libphp5.so

$ sudo apachectl restart
/usr/sbin/apachectl: line 82: ulimit: open files: cannot modify limit: Invalid argument

$ sudo vi /usr/sbin/apachectl
ULIMIT_MAX_FILES="ulimit -S -n `ulimit -H -n`"
↓
#ULIMIT_MAX_FILES="ulimit -S -n `ulimit -H -n`"
ULIMIT_MAX_FILES=""

$ sudo cp /etc/php.ini.default /etc/php.ini
$ sudo chmod 0644 /etc/php.ini
$ sudo vi /etc/php.ini 
date.timezone = Asia/Tokyo

$ sudo apachectl restart

MongoDB install

参考サイト:http://d.hatena.ne.jp/babie/20100601/1275420266

// mongo
$ brew install mongodb
$ mkdir -p /data/db
// 自動起動設定
$ sudo vi /Library/LaunchDaemons/org.mongodb.mongod.plist
mongod.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>org.mongodb.mongod</string>
        <key>RunAtLoad</key>
        <true/>
        <key>ProgramArguments</key>
        <array>
                <string>/usr/local/bin/mongod</string>
                <string>--dbpath</string>
                <string>/data/db</string>
                <string>--logpath</string>
                <string>/var/log/mongodb.log</string>
        </array>
</dict>
</plist>
$ sudo launchctl load /Library/LaunchDaemons/org.mongodb.mongod.plist
$ sudo launchctl unload /Library/LaunchDaemons/org.mongodb.mongod.plist

Peclが無いと怒られたのでインストール

参考サイト:http://sudhanshuraheja.com/2011/03/installing-php-pear-on-mac-osx-10-6-with-php-5-3-3/

$ wget http://pear.php.net/go-pear.phar 
$ sudo php -d detect_unicode=0 go-pear.phar

Below is a suggested file layout for your new PEAR installation.  To
change individual locations, type the number in front of the
directory.  Type 'all' to change all of them or simply press Enter to
accept these locations.

 1. Installation base ($prefix)                   : /Users/macmini/pear
 2. Temporary directory for processing            : /tmp/pear/install
 3. Temporary directory for downloads             : /tmp/pear/install
 4. Binaries directory                            : /Users/macmini/pear/bin
 5. PHP code directory ($php_dir)                 : /Users/macmini/pear/share/pear
 6. Documentation directory                       : /Users/macmini/pear/docs
 7. Data directory                                : /Users/macmini/pear/data
 8. User-modifiable configuration files directory : /Users/macmini/pear/cfg
 9. Public Web Files directory                    : /Users/macmini/pear/www
10. Tests directory                               : /Users/macmini/pear/tests
11. Name of configuration file                    : /Users/macmini/.pearrc

1-11, 'all' or Enter to continue: 1
(Use $prefix as a shortcut for '/Users/macmini/pear', etc.)
Installation base ($prefix) [/Users/macmini/pear] : /usr/lib/php

$ sudo ln -s /usr/lib/php/bin/pear /usr/bin
$ sudo ln -s /usr/lib/php/bin/pecl /usr/bin
$ sudo pecl install mongo
$ sudo vi /etc/php.ini
extension_dir = "/usr/lib/php/extensions/no-debug-non-zts-20090626"
extension=mongo.so

appwebが速いそうなのでまずはPHPを動くように設定してみた

appwebのサイト http://appwebserver.org/
appwebの記事 http://www.moongift.jp/2009/05/appweb/

# cd /usr/local/src
# mkdir appweb
# cd appweb
# wget http://appwebserver.org/software/appweb-3.2.3-3-ubuntu-LINUX-i686.tar.tar.gz

// 全て Enter
# ./install

ブラウザで http://localhost:7777/ を叩くと表示。オォッ!

// VirtualHostを試すのでUser追加
# adduser appweb
# su - appweb
$ mkdir public_html
$ vi phpinfo.php
<?php phpinfo();

# cd /etc/appweb/conf/hosts/
# vi appweb.conf
Listen 5556
<VirtualHost *:5556>
  ServerName        appweb.lo
  DocumentRoot    "/home/appweb/public_html"
  AddHandler cgiHandler .php
  Action application/x-appweb-php /usr/local/lib/php4/bin/php
</VirtualHost>

# /etc/init.d/appweb restart
# sudo vi /etc/hosts
127.0.0.1 appweb.lo

ブラウザで http://appweb.lo:5556/phpinfo.php を叩くとPHP4がCGIモードで起動した。
すげー簡単。


一応説明入れるとPHP4は以下のconfigureでCGIモードで入ってる。

./configure --prefix=/usr/local/lib/php4 --exec-prefix=/usr/local/lib/php4 --with-pgsql=/usr/local/pgsql/lib/ --enable-mbstring --enable-mbstr-enc-trans --enable-mbregex 


ベンチは今度試してみよう。