I am using the Media Plugin (http://www.ohloh.net/p/cakephp-media).
I wanna define custom folder for all the uplaods. i am a little confused where it has to be done. This is the folder structure i want to achieve
webroot/media/image/original (for the original file storage)
webroot/media/image/large (for the large image filter)
webroot/media/image/medium (for the medium image filter)
webroot/media/image/small (for the small image filter)
also i want to use a random name that i want to generate using the following sript.
//UUID generator
function _imgName() {
return time() . substr(md5(micr开发者_运维知识库otime()), 0, 12);
}
Firstly, having used this plugin, I would recommend considering the default directory structure to keep your app simpler. You don't want to edit the plugin directly as it will make upgrading more painful...
# /app/webroot/media/transfer/img/slug.ext (for the original file storage)
# /app/webroot/media/filter/l/img/slug.ext (for the large image filter)
# /app/webroot/media/filter/m/img/slug.ext (for the medium image filter)
# /app/webroot/media/filter/s/img/slug.ext (for the small image filter)
However, the Media plugin configuration file is located at /app/plugins/media/config/core.php
and contains some constants that you can override application-wide by defining them in /app/config/bootstrap.php
first. To achieve a format somewhat similar to what you want, you could define the following variables:
define('MEDIA_TRANSFER', WWW_ROOT . 'media' . DS . 'original' . DS);
define('MEDIA_FILTER', WWW_ROOT . 'media' . DS);
define('MEDIA_TRANSFER_URL', 'media/original/');
define('MEDIA_FILTER_URL', 'media/');
# /app/webroot/media/original/img/slug.ext (for the original file storage)
# /app/webroot/media/l/img/slug.ext (for the large image filter)
# /app/webroot/media/m/img/slug.ext (for the medium image filter)
# /app/webroot/media/s/img/slug.ext (for the small image filter)
(Note: you can also set the above paths on a per-model basis by passing the correct configuration options when adding the behavior to your models.)
You can also redefine the names of the image filters being used to get a step closer to your goal. You need to do this in /app/config/bootstrap.php
again, but after you have loaded the Media plugin configuration:
require APP . 'plugins' . DS . 'media' . DS . 'config' . DS . 'core.php';
Configure::write('Media.filter.image', array(
'small' => array('convert' => 'image/png', 'zoomCrop' => array(100, 100)),
'medium' => array('convert' => 'image/png', 'fitCrop' => array(300, 300)),
'large' => array('convert' => 'image/png', 'fit' => array(600, 440)),
));
# /app/webroot/media/original/img/slug.ext (for the original file storage)
# /app/webroot/media/large/img/slug.ext (for the large image filter)
# /app/webroot/media/medium/img/slug.ext (for the medium image filter)
# /app/webroot/media/small/img/slug.ext (for the small image filter)
If you read the documentation for the Media.TransferBehavior::transferTo()
method, you will see you can customize the latter part of the path (ie. img/slug.ext
) by reimplementing this method in your model (eg. MyModel::transferTo()
). Something like this would get you even closer:
class MyModel extends AppModel {
public function transferTo($via, $from) {
extract($from);
$mime = Mime_Type::guessName($mimeType ? $mimeType : $file);
$name = $this->_imgName();
$path = $mime . DS . $name
$path .= !empty($extension) ? '.' . strtolower($extension) : null;
return $path;
}
}
# /app/webroot/media/original/image/129916996632c787226a0b.ext (for the original file storage)
# /app/webroot/media/large/image/129916998392a3570a1828.ext (for the large image filter)
# /app/webroot/media/medium/image/12991699891c7625bebedb.ext (for the medium image filter)
# /app/webroot/media/small/image/12991699938ab22d80cfc6.ext (for the small image filter)
While not exactly what you were looking for (/large/image/
vs /image/large/
), this is about as far as you can go without reimplementing larger portions of the plugin. This is because the path is treated as two parts (eg. /media/large/
and image/129916998392a3570a1828.ext
) that get appended together later. You can see the append operation happening in the Media.TransferBehavior::_prepare()
method and in the Media.GeneratorBehavior::make()
method. You would need to either extend the plugin and duplicate those methods (with alterations) in your application code, or hack those two lines directly to get the desired output!
精彩评论