-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProcessPixlrEditor.module
More file actions
155 lines (109 loc) · 4.54 KB
/
ProcessPixlrEditor.module
File metadata and controls
155 lines (109 loc) · 4.54 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* Process Page for PixlrEditor Module
*
* ProcessWire 2.x
* Copyright (C) 2012 Paweł Gruchociak
* Licensed under GNU/GPL v2
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*/
class ProcessPixlrEditor extends Process implements Module {
public static function getModuleInfo() {
return array(
'title' => 'Pixlr Image Processing',
'summary' => 'Process images edited in Pixlr',
'href' => 'http://pixlr.com',
'version' => 106,
'permanent' => false,
'autoload' => false,
'singular' => true,
'permission' => "page-edit",
'requires' => 'PixlrEditor'
);
}
public function __construct() {
$p = $this->pages->get('has_parent=3, name=pixlr-image-process, include=all');
if( $p->id && $p->isHidden() === false ){
$p->addStatus(Page::statusHidden);
$p->save();
}
}
public function ___execute() {
$out = '';
if($this->input->get('exit'))
{
// close overlay
return '<script> window.parent.PixlrEditor.closeOverlay(); </script>';
}
//return $out;
$this->setFuel('processHeadline', 'Preview');
$image_url = $this->input->get->image; // url to image file on pixlr.com
$image_id = (int) $this->input->get->image_id;
$page_id = (int) $this->input->get->page_id;
$filename = $this->input->get->filename; // basename
$field = $this->input->get->field; // field name eg 'images'
$prefix = $this->input->get->prefix; // thumb prefix
/* @var $page Page */
$page = wire('pages')->get($page_id);
if( !$page->id )
{
throw new WireException( $this->_("Page not found.") );
}
// Is there field...
if( !$field && $page->template->fieldgroup->getField($field,false) !=null )
{
throw new WireException( sprintf( $this->_("Field %s not found."), $field) );
}
// and proper image URL...
if ( !$image_url || !preg_match('/^http:\/\/[a-z0-9]+.pixlr.com/i', $image_url)) {
throw new WireException( sprintf( $this->_("Image file: %s not found or wrong hostname."), $image_url) );
} else {
// then let's replace images.
/* @var $old_img FieldtypeImage */
if ( !$old_img = $page->get($field)->get("$filename") ) // find image
{
throw new WireException( sprintf( $this->_("Image file: %s not found in %s."), $filename, $field) );
}
// check if it's a thumbnail
if ($prefix && $url = $old_img->getThumb($prefix))
{
$thumb_path = wire('config')->paths->root . str_replace(wire('config')->urls->root, '', $url);
file_put_contents($thumb_path, file_get_contents($image_url) );
// show new image to user.
$href = $url ;
}
else // if $old_img
{
file_put_contents($old_img->filename, file_get_contents($image_url)); // replace image
// if ( ! $old_img->thumbnail ) $old_img->removeVariations(); // clear variations
$old_img->removeVariations(); // clear variations
// show new image to user.
$href = $old_img->url ;
}
$out = '<img src="'. $href .'?'. time() .'" style="max-height:95%;max-width:95%;display:block;margin:auto;" alt="" />';
}
return $out;
}
public function ___install() {
parent::___install();
//$this->modules->get('InputfieldCheckboxes');
$p = new Page();
$p->template = $this->templates->get("admin");
$p->parent = wire('pages')->get(3); // /admin/pages
$p->title = 'Pixlr Image Process';
$p->name = 'pixlr-image-process';
$p->process = $this;
$p->addStatus(Page::statusHidden);
$p->save();
}
public function ___uninstall() {
parent::___uninstall();
$p = $this->pages->get('has_parent=3, name=pixlr-image-process, include=all');
if ($p->id) $p->delete();
$p = $this->permissions->get('pixlr-image-process');
if ($p->id) $p->delete();
}
}