-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontroller.php
More file actions
84 lines (74 loc) · 2.84 KB
/
Copy pathcontroller.php
File metadata and controls
84 lines (74 loc) · 2.84 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
<?php
namespace Concrete\Package\OptimizedImages;
use Package;
use Concrete\Core\Backup\ContentImporter;
use Events;
use Concrete\Package\OptimizedImages\Src\OptimizedImage ;
class Controller extends Package {
protected $pkgHandle = 'optimized_images';
protected $appVersionRequired = '5.7.1';
protected $pkgVersion = '0.10.4';
protected $map = [
'optimizedImageSetting.tinyPngApiKey' => 'tinyPngApiKey',
'optimizedImageSetting.allowCustomThumbOptimize' => 'allowCustomThumbOptimize',
];
public function getPackageDescription() {
return t("Package for optimize your image size");
}
public function getPackageName() {
return t("Optimize Image Size");
}
protected function installXmlContent()
{
$pkg = Package::getByHandle($this->pkgHandle);
$ci = new ContentImporter();
$ci->importContentFile($pkg->getPackagePath() . '/install.xml');
}
public function install() {
$pkg = parent::install();
$this->installXmlContent();
$this->processInstallOptions();
}
public function upgrade() {
$pkg = parent::upgrade();
$this->installXmlContent();
}
public function getConfigMap()
{
return $this->map;
}
private function processInstallOptions()
{
$pkg = Package::getByHandle($this->pkgHandle);
$map = $this->getConfigMap();
foreach ($map as $configKey => $formElement) {
$data = $_REQUEST[$formElement];
$pkg->getConfig()->save($configKey, $data);
}
}
public function on_start() {
$pkg = Package::getByHandle($this->pkgHandle);
$config = $pkg->getConfig();
$allowCustomThumbOptimize = $config->get('optimizedImageSetting.allowCustomThumbOptimize');
if($allowCustomThumbOptimize != 1){
Events::addListener('on_file_add', function(){
$fileList = OptimizedImage::getLastRecord();
$pkg = Package::getByHandle('optimized_images');
$config = $pkg->getConfig();
$tinyPngApiKey = $config->get('optimizedImageSetting.tinyPngApiKey');
$fileObject = \File::getByID($fileList);
$ext = pathinfo($fileObject->getFileName(), PATHINFO_EXTENSION);
$imageExtesions = array('jpg', 'jpeg', 'png', 'JPEG', 'PNG', 'JPG');
if (in_array($ext, $imageExtesions)) {
$source_img = DIR_FILES_UPLOADED_STANDARD . '/' . $fileObject->getFileResource()->getPath();
if (file_exists($source_img)) {
$d = OptimizedImage::tinyPngCompress($source_img, $tinyPngApiKey);
if(!$d){
OptimizedImage::save($fileList);
}
}
}
});
}
}
}