forked from matt-pawley/oc-imageresizer-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.php
More file actions
132 lines (120 loc) · 3.88 KB
/
Plugin.php
File metadata and controls
132 lines (120 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php namespace Cubic\ImageResizer;
use System\Classes\PluginBase;
use Cubic\ImageResizer\Classes\Image;
use Validator;
/**
* ImageResizer Plugin Information File
*/
class Plugin extends PluginBase
{
/**
* Returns information about this plugin.
*
* @return array
*/
public function pluginDetails()
{
return [
'name' => 'cubic.imageresizer::lang.plugin.name',
'description' => 'cubic.imageresizer::lang.plugin.description',
'author' => 'Cubic Ltd',
'icon' => 'icon-picture-o',
'homepage' => 'https://github.com/cubicltd/wn-imageresizer-plugin',
'replaces' => [
'ToughDeveloper.ImageResizer' => '<1.5.0'
],
];
}
/**
* Registers any back-end permissions used by this plugin.
*
* @return array
*/
public function registerPermissions()
{
return [
'cubic.imageresizer.access_settings' => [
'tab' => 'cubic.imageresizer::lang.permission.tab',
'label' => 'cubic.imageresizer::lang.permission.label'
]
];
}
public function boot(){
Validator::extend('valid_tinypng_key', function($attribute, $value, $parameters) {
try {
\Tinify\setKey($value);
\Tinify\validate();
} catch(\Tinify\Exception $e) {
return false;
}
return true;
});
}
public function registerMarkupTags()
{
return [
'filters' => [
'resize' => function($file_path, $width = false, $height = false, $options = []) {
$image = new Image($file_path);
return $image->resize($width, $height, $options);
},
'imageWidth' => function($image) {
if (!$image instanceOf Image) {
$image = new Image($image);
}
return getimagesize($image->getCachedImagePath())[0];
},
'imageHeight' => function($image) {
if (!$image instanceOf Image) {
$image = new Image($image);
}
return getimagesize($image->getCachedImagePath())[1];
}
]
];
}
public function registerSettings()
{
return [
'settings' => [
'label' => 'cubic.imageresizer::lang.settings.label',
'icon' => 'icon-picture-o',
'description' => 'cubic.imageresizer::lang.settings.description',
'class' => 'Cubic\ImageResizer\Models\Settings',
'order' => 0,
'permissions' => ['cubic.imageresizer.access_settings']
]
];
}
public function registerListColumnTypes()
{
return [
'thumb' => [$this, 'evalThumbListColumn'],
];
}
public function evalThumbListColumn($value, $column, $record)
{
$config = $column->config;
// Get config options with defaults
$width = isset($config['width']) ? $config['width'] : 50;
$height = isset($config['height']) ? $config['height'] : 50;
$options = isset($config['options']) ? $config['options'] : [];
// attachMany relation?
if (isset($record->attachMany[$column->columnName]))
{
$file = $value->first();
}
// attachOne relation?
else if (isset($record->attachOne[$column->columnName]))
{
$file = $value;
}
// Mediafinder
else
{
$file = storage_path() . '/app/media' . $value;
}
$image = new Image($file);
return $image->resize($width, $height, $options)->render();
}
}