This repository was archived by the owner on Aug 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPlugin.php
More file actions
114 lines (97 loc) · 4.19 KB
/
Plugin.php
File metadata and controls
114 lines (97 loc) · 4.19 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
<?php namespace PopcornPHP\ImageCompress;
use System\Classes\PluginBase;
use October\Rain\Database\Attach\File;
use October\Rain\Database\Attach\Resizer;
use PopcornPHP\ImageCompress\Models\Settings as ImageCompressSettings;
class Plugin extends PluginBase
{
public function pluginDetails()
{
return [
'name' => 'ImageCompress',
'description' => 'Native compress and resize for images',
'author' => 'Alexander Shapoval',
'icon' => 'icon-compress',
'homepage' => 'https://popcornphp.github.io',
];
}
public function boot()
{
File::extend(function ($model) {
$model->bindEvent('model.beforeCreate', function () use ($model) {
if ($model->isImage()) {
$isChangeQuality = ImageCompressSettings::get('is_change_quality');
$isChangeWidth = ImageCompressSettings::get('is_change_width');
$isChangeHeight = ImageCompressSettings::get('is_change_height');
$isMakeEnlarge = ImageCompressSettings::get('is_make_enlarge', false);
if (
$isChangeQuality == true ||
$isChangeWidth == true ||
$isChangeHeight == true
) {
/**
* Prepare
*/
$filePath = $model->getLocalPath();
$options = [];
$maxWidth = false;
$maxHeight = false;
list($originalWidth, $originalHeight) = getimagesize($filePath);
$image = Resizer::open($filePath);
/**
* Set options (set quality and mode resize)
*/
if ($isChangeQuality == true) {
$options['quality'] = ImageCompressSettings::get('quality');
}
if ($isChangeWidth == true || $isChangeHeight == true) {
$options['mode'] = ImageCompressSettings::get('resize_mode');
}
$image->setOptions($options);
/**
* Set width and height
*/
if ($isChangeWidth == true) {
$maxWidth = ImageCompressSettings::get('max_width');
}
if ($isChangeHeight == true) {
$maxHeight = ImageCompressSettings::get('max_height');
}
if (
($isChangeWidth == true || $isChangeHeight == true) &&
($isMakeEnlarge == true || $originalWidth > $maxWidth || $originalHeight > $maxHeight)
) {
$image->resize($maxWidth, $maxHeight);
}
/**
* Save image, set new size of file
*/
$image->save($filePath);
clearstatcache();
$model->file_size = filesize($filePath);
}
}
});
});
}
public function registerSettings()
{
return [
'compress' => [
'label' => 'Compress images',
'description' => 'Image compression management',
'category' => 'system::lang.system.categories.system',
'icon' => 'icon-compress',
'class' => 'PopcornPHP\ImageCompress\Models\Settings',
'permissions' => ['popcornphp.imagecompress.access_settings'],
'order' => 500,
'keywords' => 'images compress',
],
];
}
public function registerPermissions() {
return [
'popcornphp.imagecompress.access_settings' => ['tab' => 'Image Compress', 'label' => 'Access to Image Compress settings'],
];
}
}