Web技術日記
ホームページ制作の小技を紹介します。

imagemagickでピクセル情報取得 2009年6月 2日

convert 画像ファイル名 txt:

出力結果

# ImageMagick pixel enumeration: 1,1,65535,RGBA
0,0: (65535,65535,65535,65535) #FFFFFFFFFFFFFFFF
0,1: (65535,65535,65535,65535) #FFFFFFFFFFFFFFFF
・・・

-crop 0x0+10+10などと併用することで、1ピクセルだけの情報取得も可能

 
 

チャンク形式のHTTP1.1応答 2009年4月 9日

HTTP1.0応答の場合は、問題ないのですが、HTTP1.1応答の場合、ボディ部がチャンク形式というちょっと変わった形式で返ってくるようです。

例えば、ボディ部が
abcdefghijklmnopqrstuvwxyz1234567890
の場合、HTTP1.0だとこれがそのまま返ってきますが、
HTTP1.1だと
1a+改行コード(1aは十進数に直すと26バイト、次のデータの長さ)
abcdefghijklmnopqrstuvwxyz+改行コード
10+改行コード
1234567890+改行コード
0+改行コード(データの終わり)
オプションデータ+改行コード
オプションデータ+改行コード
・・・・
という感じで返ってくるようです。

これを解析する場合のスクリプトは以下のような感じ。

<?
$fp = fsockopen("アドレス", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET HTTP/1.1\r\n";
    $out .= "Accept: */*\r\n";
    $out .= "Accept-Language: ja\r\n";
    $out .= "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)\r\n";
    $out .= "Host: ホスト名\r\n";
    $out .= "Connection: Keep-Alive\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        $headers=fgets($fp, 4096);
        if(trim($headers)==""){
            break;
        }
    }
    $body="";  
    while (!feof($fp)) {
        $tmp = hexdec(fgets($fp));
        if (!$tmp) break;
        $body=fread($fp, $tmp);
        fgets($fp);
        print $body;
    }
    fclose($fp);
}
?>

 
 

Linuxでユーザーのホームディレクトリ変更 2008年9月29日

usermod -d 新しいホームディレクトリ ユーザー名

usermodその他のオプション
-a, --append append the user to the supplemental GROUPS
(use only with -G)
-c, --comment COMMENT new value of the GECOS field
-d, --home HOME_DIR new home directory for the user account
-e, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE
-f, --inactive INACTIVE set password inactive after expiration
to INACTIVE
-g, --gid GROUP force use GROUP as new primary group
-G, --groups GROUPS new list of supplementary GROUPS
-h, --help display this help message and exit
-l, --login NEW_LOGIN new value of the login name
-L, --lock lock the user account
-m, --move-home move contents of the home directory to the new
location (use only with -d)
-o, --non-unique allow using duplicate (non-unique) UID
-p, --password PASSWORD use encrypted password for the new password
-s, --shell SHELL new login shell for the user account
-u, --uid UID new UID for the user account
-U, --unlock unlock the user account
-Z, --selinux-user new selinux user mapping for the user account

 
 

socket通信でPOSTでデータを渡す方法 2008年6月 8日

以下のような感じ。

$tmp = 'a=123&b=456&c=789';
$tmp .= "\r\n\r\n";
$fp = fsockopen('ホスト名', 80, $errno, $errstr, 30);
$out = 'POST スクリプト名 HTTP/1.0'."\r\n";
$out .= 'Date: '.gmdate('D, d M Y H:i:s \G\M\T')."\r\n";
$out .= 'Content-Length: '.strlen($tmp)."\r\n\r\n";
$out .= $tmp;
fwrite($fp, $out);

unset($tmp);
while (!feof($fp)) {
$tmp[] = fgets($fp);
}
fclose($fp);

 
 

mysqlのShift_JISの日本語のinsertエラー(文字化け) 2008年6月 2日

mysqlの文字コードがShift_JISの場合、日本語をsqlで、そのままinsertすると、「予」など一部の文字で、エラー又は、文字化けを起こす。

これはそれらの文字をバイト表記した場合、5C(いわゆる¥マーク)が入るためのようだ。
5cはsqlが実行されるときに、自動削除されるため、文字化け又はエラーの原因になる。

対策としては、1文字づつ置換するのは大変なので、バイナリエディタで、5c → 5c 5c に一括置換してやると良い。

 
 
 
12