-
Notifications
You must be signed in to change notification settings - Fork 2
/
extension.driver.php
164 lines (133 loc) · 4.6 KB
/
extension.driver.php
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
Class extension_resizeupload extends Extension{
public function about(){
return array('name' => 'Resize Uploaded Image Files',
'version' => '1.0.4',
'release-date' => '2011-06-18',
'author' => array('name' => 'Thomas Appel',
'website' => 'http://thomas-appel.com')
);
}
public function getSubscribedDelegates(){
return array(
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => '__appendPreferences'
),
array(
'page' => '/publish/edit/',
'delegate' => 'EntryPostEdit',
'callback' => 'resizeUpload'
),
array(
'page' => '/publish/new/',
'delegate' => 'EntryPostCreate',
'callback' => 'resizeUpload'
)
);
}
public function install() {
// Add defaults to config.php
if (!Symphony::Configuration()->get('resizeupload')) {
Symphony::Configuration()->set('im_path', '/usr/local/bin/', 'resizeupload');
Symphony::Configuration()->set('max_w', '800', 'resizeupload');
Symphony::Configuration()->set('max_h', '800', 'resizeupload');
}
return Administration::instance()->saveConfig();
}
public function uninstall() {
Symphony::Configuration()->remove('resizeupload');
return Administration::instance()->saveConfig();
}
public function resizeUpload($context) {
$entry = & $context['entry'];
$entry_id = $entry->get('id');
$content = & $entry->getData();
$fM = new FieldManager($this->_Parent);
foreach($content as $i => $field) {
$fieldType = $fM->fetchFieldTypeFromID($i);
if ($fieldType == 'upload' || $fieldType == 'uniqueupload') {
# prefilter if field is image
$current_field = $fM->fetch($i);
if (preg_match('/^image+\/.*?/',$field['mimetype'])) {
$this->processImageFile($field, $i, $entry_id);
}
}
}
}
public function processImageFile(&$field, $field_id, $entry_id) {
if (!$meta = getimagesize($file = WORKSPACE . $field['file'])) {
return;
}
if (file_exists($file) && is_readable($file)) {
preg_match('/[^\/]+\.([a-z]+)$/i',$field['file'],$filename);
$path = WORKSPACE . preg_replace('/'.$filename[0].'/', '', $field['file']);
$tempfile = $path . 'temp_' . $filename[0];
$conf = Symphony::Configuration()->get('resizeupload');
if ($meta[0] > $conf['max_w'] || $meta[1] > $conf['max_h']) {
rename($file, $tempfile);
self::convert($tempfile, $file, $conf['max_w'],$conf['max_h'], $conf['im_path']);
if (file_exists($file) && is_readable($file)) {
require_once (TOOLKIT . '/fields/field.upload.php');
$field['size'] = filesize($file);
$field['meta'] = serialize(fieldUpload::getMetaInfo($file, $field['mimetype']));
Symphony::Database()->update($field, 'tbl_entries_data_'.$field_id, "`entry_id` = '$entry_id'");
unlink($tempfile);
} else {
rename($tempfile, $file);
}
} else {
return;
}
}
}
/**
* Resizes an Image to a given maximum width and height
*
* @param string $ifile
* input file
* @param string $ofile
* output file
* @param string $maxw
* maximum width
* @param string $maxh
* maximum height
* @return void
* executes convert
*/
public static function convert($ifile = null, $ofile = null, $maxw = null, $maxh = null, $impath = NULL){
exec("{$impath}convert $ifile -thumbnail {$maxw}x{$maxh} $ofile");
}
public function __appendPreferences($context) {
$group = new XMLElement('fieldset');
$group->setAttribute('class', 'settings');
$group->appendChild(new XMLElement('legend', 'Resize Uploaded Images'));
$label = Widget::Label('Path to ImageMagick / GraphicsMagick');
$label->appendChild(
Widget::Input(
'settings[resizeupload][im_path]',
General::Sanitize(Symphony::Configuration()->get('im_path', 'resizeupload'))
)
);
$group->appendChild($label);
$label = Widget::Label('Maximum Image Width');
$label->appendChild(
Widget::Input(
'settings[resizeupload][max_w]',
General::Sanitize(Symphony::Configuration()->get('max_w', 'resizeupload'))
)
);
$group->appendChild($label);
$label = Widget::Label('Maximum Image Height');
$label->appendChild(
Widget::Input(
'settings[resizeupload][max_h]',
General::Sanitize(Symphony::Configuration()->get('max_h', 'resizeupload'))
)
);
$group->appendChild($label);
$context['wrapper']->appendChild($group);
}
}
?>