Cakephp media plugin メモ

plugins/media/models/behaviors/media.php

テーブルのカラム名を変更

//165行目
$result = array(
'checksum' => $File->md5(),
'dirname' => $dirname,
'basename' => $File->name,
);
//変更
$result = array(
'checksum' => $File->md5(),
'photo_dir' => $dirname,
'photo_file' => $File->name,
);


?>



画像作成時にデフォルトはpngになるようなのでそれぞれ指定

bootstrap.php

require APP.'plugins/media/config/core.php';
//読み込んだ後に指定
Configure::write('Media.filter.image', array(
//gif
'xs' => array('convert' => 'image/gif', 'fit' => array(50, 50)),
's' => array('convert' => 'image/gif', 'fit' => array(140, 100)),
'm' => array('convert' => 'image/gif', 'fit' => array(600, 400)),
'l' => array('convert' => 'image/gif', 'fit' => array(960, 720)),
//png
'xs' => array('convert' => 'image/png', 'fit' => array(50, 50)),
's' => array('convert' => 'image/png', 'fit' => array(140, 100)),
'm' => array('convert' => 'image/png', 'fit' => array(600, 400)),
'l' => array('convert' => 'image/png', 'fit' => array(960, 720)),
//jpg
'xs' => array('convert' => 'image/jpeg', 'fit' => array(50, 50)),
's' => array('convert' => 'image/jpeg', 'fit' => array(140, 100)),
'm' => array('convert' => 'image/jpeg', 'fit' => array(600, 400)),
'l' => array('convert' => 'image/jpeg', 'fit' => array(960, 720)),
)
);


作成された画像が、/transfer/img/ディレクトリになるので変更する場合
使用するモデル.php

var $actsAs = array(
'Media.Transfer' => array(
'trustClient' => false,
'destinationFile' => ':year::DS::Source.basename:',
'baseDirectory' => MEDIA_TRANSFER,
'createDirectory' => true,
),
'Media.Media' => array(
'metadataLevel' => 2,
'makeVersions' => true,
'filterDirectory' => MEDIA_FILTER,
)
);


transfer.phpを見るとそのほかにいろいろ指定できる模様。
/plugin/media/models/behaviors/transfer.php

/**
* Default settings
*
* trustClient
* false -
* true - Trust the MIME type submitted together with an upload
*
* destinationFile
* A path (withouth leading slash) relative to `baseDirectory`.
*
* These markers can be used:
* :DS: Directory separator `'/'` or `'\'`
* :uuid: An uuid generated by String::uuid()
* :day: The current day
* :month: The current month
* :year: The current year
* :Model.name:
* :Model.alias:
* :Model.xyz: Where `xyz` is a field of the submitted record
* :Source.basename: e.g. `'logo.png'`
* :Source.filename: e.g. `'logo'`
* :Source.extension: e.g. `'png'`
* :Source.mimeType: e.g. `'image_png'`
* :Medium.name: Lowercased medium name of the source file (e.g. `'image'`)
* :Medium.short: Short medium name of the source file (e.g. `'img'`)
*
* baseDirectory
* string - An absolute path (with trailing slash) to a directory
*
* createDirectory
* false - Fail on missing directories
* true - Recursively create missing directories
*
* @var array
*/



作成される画像の設定

define('MEDIA', WWW_ROOT . 'media' . DS);
define('MEDIA_STATIC', MEDIA . 'static' . DS);
// CREATE DIR BASE
define('MEDIA_FILTER', '/var/www/);
// CREATE DIR
define('MEDIA_TRANSFER', MEDIA . 'test' . DS);

MEDIA_FILTER = 作成される画像の一番上のディレクト
MEDIA_TRANSFER その下のディレクトリ(データベースのdirname)


別のサーバーで実行したところtransfer.phpがエラー。
原因がわかるまでいろいろと調べましたが、どうやらphpのバージョンが5.2以降でないといけないみたい、。

728行目

/**
* Finds an alternative filename for an already existing file
*
* @param string $file Absolute path to file in local FS
* @param integer $tries Number of tries
* @return mixed A string if an alt. name was found, false if number of tries were exceeded
*/
function _alternativeFile($file, $tries = 100) {
extract(pathinfo($file), EXTR_SKIP);
//とりあえずランダムな数を代入してファイル名を作成
$filename = intval(rand());
$newFilename = $filename;

$Folder = new Folder($dirname);
$names = $Folder->find($filename . '.*');
$names = array_map(create_function('$basename', 'return pathinfo($basename, PATHINFO_FILENAME);'), $names);

for ($count = 2; in_array($newFilename, $names); $count++) {
if ($count > $tries) {
return false;
}

$newFilename = $filename . '_' . $count;
}

$new = $dirname . DS . $newFilename;

if (isset($extension)) {
$new .= '.' . $extension;
}
return $new;
}
/**