yii2-thumb-action
composer
"require": {
"xj/yii2-thumb-action": "~2.0.0"
},
URLManger
'urlManager' => [
'class' => 'yii\web\UrlManager',
'showScriptName' => false,
'enablePrettyUrl' => true,
'rules' => [
'thumb/<path:(.*)>' => '/site/thumb',
],
],
Action
//Version 2.0
public function actions()
{
return [
'thumb' => [
'class' => ThumbAction::className(),
'imageDirectoryPath' => '@app/web/upload',
'thumbDirectoryPath' => '@app/web/thumb',
'defaultDirectoryPrefix' => ['dropzone/image/'],
'defaultThumbQuality' => 100, // 1 - 100
'rules' => [
//Simple Rule
['sizes' => [[100, 100]], 't' => ThumbRequest::TYPE_AUTO],
['sizes' => [[200, 200]], 't' => ThumbRequest::TYPE_AUTO, 'q' => 50],
//Full Rule
[
'prefix' => ['image/2015', 'image/cover'],
'sizes' => [
[100, 100],
[100, '*'],
[200, '*'],
],
't' => [ThumbRequest::TYPE_AUTO, ThumbRequest::TYPE_CROP],
'q' => [50, 75],
],
],
],
];
}
//Version 1.0
public function actions()
{
return [
'thumb' => [
'class' => ThumbAction::className(),
'imageDirectoryPath' => '@app/web/upload',
'thumbDirectoryPath' => '@app/web/thumb',
'thumbQuality' => 55, // 1 - 100
// 'directoryPrefix' => ['article', 'image/cover'], //limit request Url Prefix (option)
'acl' => [
['w' => 100, 'h' => null, 't' => ThumbAction::TYPE_AUTO], //keep ratio
['w' => 100, 'h' => 100, 't' => ThumbAction::TYPE_AUTO], //keep ratio
['w' => 100, 'h' => 100, 't' => ThumbAction::TYPE_FORCE], //ignore ratio
['w' => 100, 'h' => 100, 't' => ThumbAction::TYPE_CROP], //crop to size
],
],
];
}
AccessThumb
http://example.com/thumb/article/imageName_w_100_t_auto.jpg
http://example.com/thumb/article/imageName_w_100_h_100_t_auto.jpg
//imageSource web/upload/article/imageName.jpg
//imageThumb web/thumb/article/imageName_w_100_h_100_t_force.jpg
http://example.com/thumb/article/imageName_w_100_h_100_t_force.jpg
//imageSource web/upload/image/imageName.jpg
//imageThumb web/thumb/image/imageName_w_100_h_100_t_crop.jpg
http://example.com/thumb/image/imageName_w_100_h_100_t_crop.jpg
Helper
//1
$t = ThumbHelper::model()->setOptions(['w' => '100', 'h' => '100', 't' => ThumbAction::TYPE_AUTO]);
var_dump($t->getUrl('image.jpg'));
var_dump($t->getUrl('article/image.jpg'));
var_dump($t->getUrl('http://www.example.com/image.jpg', ['w' => '100', 'h' => '101']));
var_dump($t->getUrl('http://www.example.com/article/image.jpg', ['w' => '101', 'h' => '100']));
//2
var_dump(ThumbHelper::model('http://www.example.com')->getUrl('article/image.jpg', ['w' => '100', 'h' => '100']));
var_dump(ThumbHelper::model('/baseUrl')->getUrl('article/image.jpg', ['w' => '100', 'h' => '100']));
//print
//1
string 'image_w_100_h_100_t_auto.jpg' (length=28)
string 'article/image_w_100_h_100_t_auto.jpg' (length=36)
string 'http://www.example.com/image_w_100_h_101_t_auto.jpg' (length=51)
string 'http://www.example.com/article/image_w_101_h_100_t_auto.jpg' (length=59)
//2
string 'http://www.example.com/article/image_w_100_h_100.jpg' (length=52)
string '/baseUrl/article/image_w_100_h_100.jpg' (length=38)
Comments