-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldtypeFocusPointImage.module
More file actions
145 lines (114 loc) · 3.53 KB
/
FieldtypeFocusPointImage.module
File metadata and controls
145 lines (114 loc) · 3.53 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
<?php
/**
* ProcessWire Image Fieldtype With Focus Point
*
* ProcessWire 2.x
* Copyright (C) 2012 by Niek Bosch
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.demediafabriek.nl
*
*/
class FieldtypeFocusPointImage extends FieldtypeImage {
public static function getModuleInfo() {
return array(
'title' => 'Images with focus point',
'version' => 100,
'summary' => 'Field that stores one or more GIF, JPG, or PNG images with a focus point',
'installs' => 'InputfieldFocusPointImage'
);
}
public function init() {
$this->addHook('Pageimage::sizeWithFocusPointCropping', $this, 'sizeWithFocusPointCropping');
$this->defaultInputfieldClass = 'InputfieldFocusPointImage';
}
public function sizeWithFocusPointCropping(HookEvent $event) {
$pageimage = $event->object;
$width = $event->arguments[0];
$height = $event->arguments[1];
$options = isset($event->arguments[2]) ? $event->arguments[2] : array();
if ( ! is_array($options)) {
if (is_string($options)) {
$options = array();
} else if(is_int($options)) {
$options = array('quality' => $options);
} else if(is_bool($options)) {
$options = array('upscaling' => $options);
}
}
$options['cropping'] = $pageimage->focus_point_x . '%,' . $pageimage->focus_point_y . '%';
$event->return = $pageimage->size($width, $height, $options);
return true;
}
/**
* Return the DB schema used by this field's table
*
*/
public function getDatabaseSchema(Field $field) {
$schema = parent::getDatabaseSchema($field);
$schema['focus_point_x'] = 'tinyint NOT NULL';
$schema['focus_point_y'] = 'tinyint NOT NULL';
return $schema;
}
/**
* Perform output formatting on the value delivered to the API
*
*/
public function ___formatValue(Page $page, Field $field, $value) {
$value = parent::___formatValue($page, $field, $value);
if ( ! $value instanceof Pagefiles) {
return $value;
}
if ($field->entityEncode) {
foreach ($value as $val) {
$val->set('focus_point_x', (int)$val->focus_point_x);
$val->set('focus_point_y', (int)$val->focus_point_y);
}
}
return $value;
}
/**
* Given a raw value (value as stored in DB), return the value as it would appear in a Page object
*
* @param Page $page
* @param Field $field
* @param string|int|array $value
* @return string|int|array|object $value
*
*/
public function ___wakeupValue(Page $page, Field $field, $value) {
$pagefiles = parent::___wakeupValue($page, $field, $value);
foreach ($pagefiles as $pagefile) {
foreach ($value as $val) {
if ($val['data'] !== $pagefile->basename) {
continue;
}
$pagefile->set('focus_point_x', (int)$val['focus_point_x']);
$pagefile->set('focus_point_y', (int)$val['focus_point_y']);
}
}
$pagefiles->resetTrackChanges(true);
return $pagefiles;
}
/**
* Given an 'awake' value, as set by wakeupValue, convert the value back to a basic type for storage in DB.
*
* @param Page $page
* @param Field $field
* @param string|int|array|object $value
* @return string|int
*
*/
public function ___sleepValue(Page $page, Field $field, $value) {
$sleepValue = parent::___sleepValue($page, $field, $value);
if ( ! $value instanceof Pagefiles) {
return $sleepValue;
}
for ($i = 0, $n = count($sleepValue); $i < $n; $i++) {
$pagefile = $value->eq($i);
$sleepValue[$i]['focus_point_x'] = round(min(100, max(0, (int)$pagefile->focus_point_x)));
$sleepValue[$i]['focus_point_y'] = round(min(100, max(0, (int)$pagefile->focus_point_y)));
}
return $sleepValue;
}
}