[PHP][メモ]class.upload.phpに関するメモ(docs)

画像アップロードライブラリの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. こんな設定もあるのか! []
スポンサーリンク
336x280_1
336x280_1

シェアする

  • このエントリーをはてなブックマークに追加

フォローする

スポンサーリンク
336x280_1