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
138 lines (123 loc) · 4.34 KB
/
Plugin.php
File metadata and controls
138 lines (123 loc) · 4.34 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
133
134
135
136
137
138
<?php namespace ToughDeveloper\ImageResizer;
use System\Classes\PluginBase;
use ToughDeveloper\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' => 'toughdeveloper.imageresizer::lang.plugin.name',
'description' => 'toughdeveloper.imageresizer::lang.plugin.description',
'author' => 'Tough Developer',
'icon' => 'icon-picture-o',
'homepage' => 'https://github.com/toughdeveloper/oc-imageresizer-plugin'
];
}
/**
* Registers any back-end permissions used by this plugin.
*
* @return array
*/
public function registerPermissions()
{
return [
'toughdeveloper.imageresizer.access_settings' => [
'tab' => 'toughdeveloper.imageresizer::lang.permission.tab',
'label' => 'toughdeveloper.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) {
$imagePath = (string) $image;
if(file_exists($imagePath) && is_file($imagePath)) return getimagesize($imagePath)[0];
},
'imageHeight' => function($image) {
$imagePath = (string) $image;
if(file_exists($imagePath) && is_file($imagePath)) return getimagesize($imagePath)[1];
},
'orientation' => function($image) {
$imagePath = (string) $image;
if(!file_exists($imagePath) || !is_file($imagePath)) return 'horizontal';
$imageDimensions = getimagesize($imagePath);
// Error fallback
if(!$imageDimensions) return 'horizontal';
// Height > width
if($imageDimensions[1] > $imageDimensions[0]) return 'horizontal';
return 'vertical';
}
]
];
}
public function registerSettings()
{
return [
'settings' => [
'label' => 'toughdeveloper.imageresizer::lang.settings.label',
'icon' => 'icon-picture-o',
'description' => 'toughdeveloper.imageresizer::lang.settings.description',
'class' => 'ToughDeveloper\ImageResizer\Models\Settings',
'order' => 0,
'permissions' => ['toughdeveloper.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();
}
}