8月
26
2009
0

[PHP][メモ]class.upload.phpに関するメモ(docs)
このエントリをはてなブックマークに追加このエントリをdel.icio.usに追加このエントリをLivedoor Clipに追加このエントリをYahoo!ブックマークに追加このエントリをFC2ブックマークに追加このエントリをNifty Clipに追加このエントリをPOOKMARK. Airlinesに追加このエントリをBuzzurl(バザール)に追加このエントリをChoixに追加このエントリをnewsingに追加

画像アップロードライブラリのclass.upload.phpのドキュメントを抜粋して(機械)翻訳しました。
英語が読める方は以下のリンクをご利用ください。

docs
faq

下記はご参考程度にどうぞ。
間違いもたくさんあると思いますので、ご指摘大歓迎です。
コメントでもスカイプでもTwitterでも何でも結構ですので突っ込んでいただけると助かります。

パラメータ

ファイル名の設定

file_new_name_body replaces the name body (default: ”)
$handle->file_new_name_body = 'new name';

設定済みファイル名の後ろに文字列を追加?

file_name_body_add appends to the name body (default: ”)
$handle->file_name_body_add = '_uploaded';

設定済みファイル名の前に文字列を追加

file_name_body_pre prepends to the name body (default: ”)
$handle->file_name_body_pre = ‘thumb_’;

デフォルトで出力するファイルの拡張子を変更する

file_new_name_ext replaces the file extension (default: ”)
$handle->file_new_name_ext = 'txt';

ファイル名のスペースを_(アンダーバー)に変更する?

file_safe_name formats the filename (spaces changed to _) (default: true)
$handle->file_safe_name = true;

出力先に同名のファイルがある場合に上書きする

file_overwrite sets behaviour if file already exists (default: false)
$handle->file_overwrite = true;

出力先に同名のファイルがある場合、自動的に名前を変更する

file_auto_rename automatically renames file if it already exists (default: true)
$handle->file_auto_rename = true;

出力先ディレクトリが存在し無い場合、自動的に作成する

auto_create_dir automatically creates destination directory if missing (default: true)
$handle->auto_create_dir = true;

出力先に書き込み権限が無い場合、書き込み権限をセット(chmod)を試みる?

dir_auto_chmod automatically attempts to chmod the destination directory if not writeable (default: true)
$handle->dir_auto_chmod = true;

dir_auto_chmodで出力先の書き込み権限セット(chmod)するときの権限?

dir_chmod chmod used when creating directory or if directory not writeable (default: 0777)
$handle->dir_chmod = 0777;

最大アップロードサイズ

file_max_size sets maximum upload size (default: upload_max_filesize from php.ini)
$handle->file_max_size = '1024'; // 1KB

後述のallowedしたMIMEタイプリストに対するチェック

mime_check sets if the class check the MIME against the allowed list (default: true)
$handle->mime_check = true;

???

mime_fileinfo activates MIME detection with Fileinfo PECL extension if true (or a string to set the path to the magic database file); false to deactivate (default: true)
$handle->mime_fileinfo = '/usr/share/file/magic';

???

mime_file activates MIME detection with UNIX file() command if true; false to deactivate (default: true)
$handle->mime_file = false;

???

mime_magic activates MIME detection with mime_magic (mime_content_type()) if true; false to deactivate (default: true)
$handle->mime_magic = false;

???

mime_getimagesize activates MIME detection with getimagesize() if true; false to deactivate (default: true)
$handle->mime_getimagesize = false;

???

no_script sets if the class turns scripts into text files (default: true)
$handle->no_script = false;

アップロードを許可するMIMEタイプの設定(*でワイルドカード指定可能)

allowed array of allowed mime-types. wildcard accepted, as in image/* (default: check Init)
$handle->allowed = array('application/pdf','application/msword', 'image/*')

アップロードを禁止するMIMEタイプの設定(*でワイルドカード指定可能)

forbidden array of forbidden mime-types. wildcard accepted, as in image/* (default: check Init)
$handle->forbidden = array('application/*');

出力する画像のタイプ(png,jpeg,gif,bmp,default)を指定する

image_convert if set, image will be converted (possible values : ”|’png’|'jpeg’|'gif’|'bmp’; default: ”)
$handle->image_convert = 'jpg';

??指定した色を透明化する?

image_background_color if set, will forcibly fill transparent areas with the color, in hexadecimal (default: null)
$handle->image_background_color = '#FF00FF';

???

image_default_color fallback color background color for non alpha-transparent output formats, such as JPEG or BMP, in hexadecimal (default: #FFFFFF)
$handle->image_default_color = '#FF00FF';

jpeg画像の品質

jpeg_quality sets the compression quality for JPEG images (default: 85)
$handle->jpeg_quality = 50;

jpeg画像のファイルサイズ指定

jpeg_size if set to a size in bytes, will approximate jpeg_quality so the output image fits within the size (default: null)
$handle->jpeg_size = 3072;

アップロードを制限する指定

以下の8項目のパラメータはアップロードを制限するのに利用できます。

画像の最大幅を制限

image_max_width if set to a dimension in pixels, the upload will be invalid if the image width is greater (default: null)
$handle->image_max_width = 200;

画像の最大高を制限

image_max_height if set to a dimension in pixels, the upload will be invalid if the image height is greater (default: null)
$handle->image_max_height = 100;

画像の最大総ピクセル数を制限?

image_max_pixels if set to a number of pixels, the upload will be invalid if the image number of pixels is greater (default: null)
$handle->image_max_pixels = 50000;

画像の最大アスペクト比(縦横比)を制限*1

image_max_ratio if set to a aspect ratio (width/height), the upload will be invalid if the image apect ratio is greater (default: null)
$handle->image_max_ratio = 1.5;

画像の最小幅を制限

image_min_width if set to a dimension in pixels, the upload will be invalid if the image width is lower (default: null)
$handle->image_min_width = 100;

画像の最小高を制限

image_min_height if set to a dimension in pixels, the upload will be invalid if the image height is lower (default: null)
$handle->image_min_height = 500;

画像の最小ピクセル数を制限

image_min_pixels if set to a number of pixels, the upload will be invalid if the image number of pixels is lower (default: null)
$handle->image_min_pixels = 20000;

画像の最小アスペクト比(縦横比)を制限

image_min_ratio if set to a aspect ratio (width/height), the upload will be invalid if the image apect ratio is lower (default: null)
$handle->image_min_ratio = 0.5;

画像のサイズ変更

画像のサイズを変更するか

image_resize determines is an image will be resized (default: false)
$handle->image_resize = true;

サイズ変更後の画像幅

image_x destination image width (default: 150)
$handle->image_x = 100

サイズ変更後の画像高

image_y destination image height (default: 150)
$handle->image_y = 200;

画像のアスペクト比(縦横比)を維持する?

image_ratio if true, resize image conserving the original sizes ratio, using image_x AND image_y as max sizes if true (default: false)
$handle->image_ratio = true;

image_xとimage_yを優先してサイズ変更しあまった部分をトリミングする???

image_ratio_crop if true, resize image conserving the original sizes ratio, using image_x AND image_y as max sizes, and cropping excedent to fill the space. setting can also be a string, with one or more from ‘TBLR’, indicating which side of the image will be kept while cropping (default: false)
$handle->image_ratio_crop = true;

???

image_ratio_fill if true, resize image conserving the original sizes ratio, using image_x AND image_y as max sizes, fitting the image in the space and coloring the remaining space. setting can also be a string, with one or more from ‘TBLR’, indicating which side of the space the image will be in (default: false)
$handle->image_ratio_fill = true;

???

image_ratio_no_zoom_out same as image_ratio, but won’t resize if the source image is bigger than image_x x image_y (default: false)
$handle->image_ratio_no_zoom_out = true;

???

image_ratio_x if true, resize image, calculating image_x from image_y and conserving the original sizes ratio (default: false)
$handle->image_ratio_x = true;

???

image_ratio_y if true, resize image, calculating image_y from image_x and conserving the original sizes ratio (default: false)
$handle->image_ratio_y = true;

???

image_ratio_pixels if set to a long integer, resize image, calculating image_y and image_x to match a the number of pixels (default: false)
$handle->image_ratio_pixels = 25000;

画像の装飾など?

情熱が潰えたので割愛。

TIPS

ローカルファイルを処理する場合は、第1引数にローカルファイルのパス指定してインスタンスを作る。

$upload = new upload('/home/user/myfile.jpg');

言語をセットするには、第2引数に言語コードを指定してインスタンスを作る。

$handle = new upload($_FILES['image_field'], 'fr_FR');
$handle = new upload('/home/user/myfile.jpg', 'fr_FR');

言語ファイルには「class.upload.xx_XX.php」というファイルが含まれており、このファイルを編集することで簡単に自作できる。

アップロードファイルを直接表示するには

$handle = new upload($_FILES['image_field']);
header('Content-type: ' . $handle->file_src_mim
echo $handle->Process();
die();

アップロードしたファイルの保存名を取得

$handle->file_dst_name;

ダウンロードを強制するには、

$handle = new upload($_FILES['image_field']);
header('Content-type: ' . $handle->file_src_mime);
header("Content-Disposition: attachment; filename=".rawurlencode($handle->file_src_name).";");
echo $handle->Process();
die();

デバッグ

以下のコードでログを表示可能
echo $foo->log;

※後日CakePHPでclass.upload.phpを使うメモを書こうと思っていたりいなかったり。

  1. こんな設定もあるのか! []
Written by suzukenn in: メモ, リファレンス | タグ: ,
12月
24
2008
2

[CakePHP]OpenFlashChartを組み込む
このエントリをはてなブックマークに追加このエントリをdel.icio.usに追加このエントリをLivedoor Clipに追加このエントリをYahoo!ブックマークに追加このエントリをFC2ブックマークに追加このエントリをNifty Clipに追加このエントリをPOOKMARK. Airlinesに追加このエントリをBuzzurl(バザール)に追加このエントリをChoixに追加このエントリをnewsingに追加

概要

社内システムの売上管理でグラフを表示させたいと思って、PHPで使えるライブラリを探していたところ、

以上の2つが候補にあがりました。

どちらかといえばpChartの方が扱いが簡単そうだったので第一候補にあげていたのですが、どうやらPHP4専用のようで…orz
結局第2候補のOpenFlashChart(以下OFC)を使うことにしました。
結論から言うと、OFCにして大正解

よくよく調べるとOFCにはCakePHPのヘルパーがあったり、そのおかげで扱いも非常に簡単だったり、思った以上にスタイリッシュでかっこよかったり*1 と、pChartがPHP4専用でよかったです。ほんと。

導入に際しては結構手間取った部分もありましたのでここにメモとして残しておきたいと思います*2

導入方法

ファイルを設置

まずは本体をこちらからダウンロード
ここで注意して欲しいのは最新版*3 の1.9.7は「open-flash-chart.php」が後述するFlashChartヘルパーの形式に合いませんのでそのままでは使えません。*4 ですので、オススメは1.9.6を使うこと。
1.9.6のopen-flash-chart.phpならそのまま使えます。
それでも最新版を使いたいという方はご自分で研究してください。ここらへんに情報があります*5

ダウンロードしたファイルを解凍したら、
open-flash-chart.swfを/app/webrootへコピー。
/php-ofc-library/open-flash-chart.phpを/app/vendors*6 コピー。
で、本家の記載には無いのですがコメント欄に書かれているjsファイルもコピーする必要があるので、
/js/swfobject.jsを/app/webroot/jsにコピーしてください。

次にこちらからヘルパーをダウンロードしましょう。
でダウンロードした「flash_chart.php」を/app/views/helpersへ設置。

これでファイルの設置は完了です。

ヘルパーをちょっと手直し

このまま後述するソースで表示できそうなもんですが、実はうまくいきません。
というのも、ヘルパーからgraphクラスのrenderメソッドを呼ぶときに$output_typeを指定していないので、swfobject.jsが呼ばれずに渡すはずのパラメータが表示されてしまうようなんですね。ちゃんと調べたわけではないのでテキトーに言ってるんですがね。

$output_typeに”js”を指定してあげれば良いだけなので、ヘルパーのrenderメソッドの以下を修正してください。

return $this->graph->render();
// を下記に修正↓
return $this->graph->render("js");

これでちゃんとスクリプトが呼ばれるようになりますよと。

実際に使ってみる

こちらのサンプルソースを参考に書くだけです。
説明の必要も無いとは思いますが、

  1. pagesコントローラで
    var $helpers = array("FlashChart");
    

    の記述を追加。
    ※pagesコントローラなんてねーよって人は、自分の勉強不足を猛省しつつappControllerにでも書いてください。これもわからないという方はCakePHPを初めから勉強しなおしてください。PHPも。あとオブジェクト指向とか。

  2. /app/views/pages/charts.ctpを作成してサンプルソースをコピペ。
    <?php
    // Sets height and width
    $flashChart->begin(400, 250);
    // Title
    $flashChart->title('Example 1 - Bars: Hits per Day');
    // Configure Grid style and legends
    $flashChart->configureGrid(
    array(
    'x_axis' => array(
    'step' => 1,
    'legend' => 'Day'
    ),
    'y_axis' => array(
    'legend' => '#Hits',
    )
    )
    );
    // Prepare some random data (10 points)
    $random_hits = array();
    for ($i=0; $i < 10; $i++) {
    $random_hits[] = rand(10,100);
    }
    // Register each data set with its information.
    $data = array(
    'Hits' => array(
    'color' => '#afe342',
    'font_size' => 11,
    'data' => $random_hits,
    'graph_style' => 'bar',
    )
    );
    $flashChart->setData($data);
    
    // Set Ranges in the chart
    $flashChart->setRange('y', 0, 100);
    $flashChart->setRange('x', 0, 10);
    
    // Show the graph
    echo $flashChart->render();
    ?>
    
  3. http://%url%/pages/charts.ctpにアクセスしてグラフが表示されてるはず。

どうですか?できました?

あとは普段どおりにコントローラからグラフに使うデータを配列で渡してあげて、それをグラフにセットしてやればデータベースのデータなどを簡単にグラフにできますよ。
あと、豆知識ですが、

// Register each data set with its information.
$data = array(
'Hits' => array(
'color' => '#afe342',
'font_size' => 11,
'data' => $random_hits,
'graph_style' => 'bar',
)
);

この部分の$data['Hits']を$data['ヒット数']とすると上の項目ラベルを日本語で表示できますよ。
もちろん配列の添え字*7 に日本語を使うわけなんで環境によってはやばいことがあるかもしれませんが、僕知らねーと。とりあえずうちの環境じゃバッチリ使えちゃってます。

参考

学生起業~池袋で働く学生社長のBlog~:CakePHPでopen flash chart – livedoor Blog(ブログ)
ヘルパーの存在を知ることができ、とっても助かりました。ありがとうございます!

teethgrinder
Thank you for Mr. John Glazebrook. It helped very much thanks to your work!

  1. これ重要 []
  2. 恐らくこの記事、このブログではかなり良記事になる予感 :-) []
  3. 20081224時点 []
  4. ヘルパーの形式に合わないというよりは、ファイルが細かく分かれててそれをopen-flash-chart.phpからincludeして使ってるので色々いじらないとそのまま使えないのですよ。 []
  5. 僕って親切 :-) []
  6. ルートのvendorsでもいいですが []
  7. 添え字っていうんでしたよね?忘れちゃったよもう。 []
Written by suzukenn in: 記事 | タグ: , ,
11月
20
2008
0

[jsライブラリ]BeautifulJapaneseKitを入れてみた
このエントリをはてなブックマークに追加このエントリをdel.icio.usに追加このエントリをLivedoor Clipに追加このエントリをYahoo!ブックマークに追加このエントリをFC2ブックマークに追加このエントリをNifty Clipに追加このエントリをPOOKMARK. Airlinesに追加このエントリをBuzzurl(バザール)に追加このエントリをChoixに追加このエントリをnewsingに追加

概要

仕事でECサイト構築の真っ最中で、いろんなところにアンテナ張って使える情報を探してるんですが、すごいのみつけました。

配布元:アイデアマンズ株式会社
BeautifulJapaneseKit

フラッシュのテキストボックスを使って指定したフォントを閲覧者の環境に左右されずに表示できるって代物です。

(続きを読む…)

Written by suzukenn in: 記事 | タグ: ,

| I have been indebted to Aeros Theme. | Background image owner is NASA.