2010年3月3日

画像のオリジナルサイズをJavaScriptで取得する

カテゴリー: JavaScript — admin @ 12:57 AM

getNaturalSizeにimageオブジェクトを投げてあげるとオブジェクトが返る。

var cache = [];
var getNaturalSize = (function(){
    if(Image.naturalWidth || Image.naturalHeight){
        return function(image){
            return {
                "width"  : image.naturalWidth,
                "height" : image.naturalHeight
            };
        }
    }
    else if(window.opera){
        return function(image){
            if(!cache[image.src]){
                var mem = {
                    "w": image.width,
                    "h": image.height
                };
                image.removeAttribute("width");
                image.removeAttribute("height");
                w = image.width;
                h = image.height;
                image.width = mem.w;
                image.height = mem.h;
                cache[image.src] = {
                    "width"  : w,
                    "height" : h
                };
            }
            return cache[image.src];
        };
    }
    else if(window.attachEvent){
        return function(image){
            if (image[key] && image[key].src === image.src) {
                return image[key];
            }
            run = image.runtimeStyle;
            mem = {
                "w" : run.width,
                "h" : run.height
            }; // keep runtimeStyle
            run.width  = "auto"; // override
            run.height = "auto";
            w = image.width;
            h = image.height;
            run.width  = mem.w; // restore
            run.height = mem.h;
            image[key] = {
                "width"  : w,
                "height" : h,
                "src"    : image.src
            };
            return image[key]; // bond
        }
    }
    else{
        return function(image){
            return {
                "width"  : 100,
                "height" : 100
            };
        }
    }
})();

Firefox、IE6~8、Chrome、Operaで動作する。Safari未検証。

2010年2月22日

スクロール位置を取得する

カテゴリー: JavaScript — admin @ 3:19 AM

var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;// ex) 100(px)

2010年2月22日

Zend_Controller_Router_Route_Regexで正規表現によるルーティング設定を行う

カテゴリー: ZendFramework — admin @ 1:35 AM

以下のように、Zend_Controller_Router_Route_Regexを使用する。

$front->getRouter()->addRoute(
	'page',
	new Zend_Controller_Router_Route_Regex(
		'page_(\d+)\.html',
		array(
			'controller' => 'Index',
			'action'     => 'page'
		),
		array(
			1 => 'pageid'
		)
	)
);

Actionからは以下のコードでマッチした部分を参照できる。

$this->_getParam('pageid');

2010年2月22日

PHPからドメインを取得する

カテゴリー: PHP — admin @ 12:18 AM

var_dump($_SERVER['SERVER_NAME']);
//string(16) "sample.org"

マルチドメイン、シングルホスティングの時には使いそうだ。

2010年2月14日

ZendFrameworkでコマンドラインからアクションを実行する

カテゴリー: PHP, ZendFramework — admin @ 11:56 PM

コマンドラインにおけるオプションの設定・取得。|の後がエイリアス名、=の後の文字列で型を指定できる。

try {
    $options = new Zend_Console_Getopt(
        array(
            'help|h'        => 'help.',
            'zfm|m=s'       => 'module',
            'zfc|c=s'       => 'controller',
            'zfa|a=s'       => 'action'
        )
    );
    $options->parse();
}
catch(Zend_Console_Getopt_Exception $e){
    die($e->getMessage() . ' : ' . $e->getUsageMessage());
}

Zend_Controller_Request_Simpleがポイント。アクション、コントローラ、モジュールを引数に指定してリクエストオブジェクトを取得する。

if(isset($options->zfa) && isset($options->zfc) && isset($options->zfm)){
    $request = new Zend_Controller_Request_Simple(
        $options->zfa,
        $options->zfc,
        $options->zfm
    );
    $front = Zend_Controller_Front::getInstance();
    $front->setRequest($request);
    $front->setRouter(new Custom_Controller_Router_Cli());
    $front->setResponse(new Zend_Controller_Response_Cli());
    $front->throwExceptions(true);
    $front->addModuleDirectory(dirname(__FILE__) . '/application/modules');
    $front->dispatch();
}

Custom_Controller_Router_Cliはこんな感じ。

<?php
require_once 'Zend/Controller/Router/Interface.php';
require_once 'Zend/Controller/Router/Abstract.php';

class Custom_Controller_Router_Cli extends Zend_Controller_Router_Abstract implements Zend_Controller_Router_Interface
{
    public function assemble($userParams, $name = null, $reset = false, $encode = true) {}
    public function route(Zend_Controller_Request_Abstract $dispatcher) {}
}

基本的には以上で実行できるが

■アクセスコントロールの設定

アクセスコントロールを行っている場合は上述のコードよりも先にアクセスできるようにしなければならない。以下は一例。

$sesion = new Zend_Session_Namespace('global');
$sesion->userLevel = 'admin';
$acl = new Zend_Acl();
$acl->addRole(new Zend_Acl_Role('guest'));
$acl->addRole(new Zend_Acl_Role('admin'), 'guest');
$acl->add(new Zend_Acl_Resource('guestPage'));
$acl->add(new Zend_Acl_Resource('adminPage'));
$acl->allow('guest');
$acl->allow('admin');
$acl->deny('guest', 'adminPage');
$acl->allow('admin', 'adminPage');
Zend_Registry::set('acl', $acl);

基本的には一般ユーザが閲覧(実行)できる場所に、このファイルを配置するのは良くない。

2010年2月14日

PHPの定数とOS

カテゴリー: PHP — admin @ 10:45 PM

■OS

OS名

echo PHP_OS;

■path

パスを区切る文字

echo PATH_SEPARATOR;// linux => ';', win => ':'

■directory

ディレクトリを区切る文字

echo DIRECTORY_SEPARATOR;// linux => '/', win => '\'

2010年2月14日

createElementでtableを挿入するときのIEの挙動

カテゴリー: JavaScript — admin @ 3:48 AM

以下のコードはIE以外では正常に動作する。

var table = document.createElement('table');
var tr = document.createElement('tr');
var td = document.createElement('td');
var img = document.createElement('img');
document.getElementById('container').appendChild(table).appendChild(tr).appendChild(td).appendChild(img);

以下のコードはIEでも動作する。違いはtbodyを明示してあげる事。

var table = document.createElement('table');
var tbody = document.createElement('tbody');
var tr = document.createElement('tr');
var td = document.createElement('td');
var img = document.createElement('img');
document.getElementById('container').appendChild(table).appendChild(tbody).appendChild(tr).appendChild(td).appendChild(img);

html 4.01

11.2.3 行グループ: THEAD、 TFOOT、及びTBODY要素

TBODY開始タグは、表が本体をただ1つだけ含んでいてヘッダもフッタも含まないという場合を除き、常に必要である。

へー

html 5

table 要素

0 個以上の tbody 要素

ふむふむ

参考

http://w3g.jp/xhtml/dic/tbody

2010年2月10日

linuxで同時に開けるソケット数を確認する

カテゴリー: Linux, PHP — admin @ 12:37 AM

以下のコマンドで確認する。

/sbin/sysctl -a|grep "somax"

PHPでは以下の定数で定義される。

SOMAXCONN

2010年1月29日

IEはradioボタンをappendChildしてもつかえない

カテゴリー: JavaScript, jQuery — admin @ 2:03 AM

IE6~7でラジオボタンをappendChildした場合、そのラジオボタンはクリックできない。ラジオボタンとして致命的なバグである。

■失敗例

以下のようにDOM要素を操作する。

var input = document.createElement('input');
var p     = document.createElement('p');
p.appendChild(input);

ちなみにjQueryを使うと以下のようになる。

var input = document.createElement('input');
var p     = document.createElement('p');
$(p).append(input);

■解決策

IEだけinnerHTMLを使う。

var input = document.createElement('input');
var p     = document.createElement('p');
if(!!(!window.opera && window.attachEvent)){
    p.innerHTML = '<input type="radio" name="id" />';
}
else{
    p.appendChild(input);
}

ちなみにjQueryを使うと以下のようになる。

var input = document.createElement('input');
var p     = document.createElement('p');
if($.browser.msie){
    $(p).append('<input type="radio" name="id" />');
}
else{
    $(p).append(input);
}

2010年1月29日

mb_ereg系関数が使えない

カテゴリー: Linux, PHP — admin @ 1:46 AM

EC-CUBEが使えなかったりする。

yum install php-mbstring

コンパイルは面倒だからyumする。

次ページへ »