forked from apeisa/Thumbnails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FieldtypeCropImage.module
163 lines (128 loc) · 4.72 KB
/
FieldtypeCropImage.module
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
<?php
class FieldtypeCropImage extends FieldtypeImage {
public static function getModuleInfo() {
return array(
'title' => __('Images with cropping', __FILE__), // Module Title
'summary' => __('Field that stores one or more GIF, JPG, or PNG images', __FILE__), // Module summary
'version' => 103,
);
}
public function init() {
$this->addHook('Pageimage::getThumb', $this, 'getThumb');
$this->addHookAfter('Pageimages::delete', $this, 'deleteThumb');
}
/**
* Delete a crop when the parent image is deleted
*
*/
public function deleteThumb(HookEvent $event) {
$images = $event->object;
$image = $event->arguments[0];
$event->object = $image;
$inputfield = $this->_getInputFieldInstance($event);
if(!$inputfield) return;
if(is_string($image)) $image = $images->get($image);
if(!$image) return;
$crops = $inputfield->thumbSetting;
$crops_a = explode("\n", $crops);
foreach($crops_a as $crop) {
$crop = explode(',', $crop);
$name = wire('sanitizer')->name($crop[0]);
if(!strlen($name)) continue;
$cropFilename = $images->path() . $name . '_' . $image->basename;
if(is_file($cropFilename)) {
unlink($cropFilename);
$this->message($this->_("Removed crop") . ': ' . basename($cropFilename), Notice::debug);
}
}
}
public function getThumb(HookEvent $event) {
$thumbFound = false;
$thumb = $event->arguments[0];
// return InputfieldCropImage or null if not found
$inputFieldInstance = $this->_getInputFieldInstance($event);
if(!$inputFieldInstance) return true;
// read crop settings from InputfieldCropImage
$crops = $inputFieldInstance->thumbSetting;
// and we continue...
$crops_a = explode("\n", $crops); // ie. thumbname,200,200 (name,width,height)
foreach($crops_a as $crop) {
$crop = explode(",", $crop);
if ($crop[0] == $thumb) {
$this->w = (int) $crop[1];
$this->h = (int) $crop[2];
$thumbFound = true;
break;
}
}
if ($thumbFound === false) {
throw new WireException(sprintf($this->_("There is no thumb called: %s"), $thumb));
return;
}
if ($this->w < 1 || $this->h < 1) {
throw new WireException(sprintf($this->_("Width and/or height not found for thumb: %s"), $thumb));
return;
}
$prefix = $this->sanitizer->name($thumb);
$img = $event->object;
$imgPath = $img->pagefiles->path() . $prefix . "_" . $img->basename;
$imgUrl = $img->pagefiles->url() . $prefix . "_" . $img->basename;
// We have a file, that is probably ok...
if(is_file($imgPath)) {
// But we check if the size matches for desired and resize if not
$sizer = new ImageSizer($imgPath);
if ($this->w != $sizer->getWidth() || $this->h != $sizer->getHeight()) {
$this->_copyAndResize($img, $imgPath);
}
// And finally we return the url
$event->return = $imgUrl;
} else {
// Image not found, we take basic crop from original
$this->_copyAndResize($img, $imgPath);
$event->return = $imgUrl;
}
return true;
}
public function _getInputFieldInstance(HookEvent $event) {
$field = null; // where we'll keep the field we're looking for
$image = $event->object;
$page = $image->page;
$action = $event->arguments[0];
// find all fields of type FieldtypeImage that are part of the page we're using
// or regular image fields with InputfieldImage inputfield assigned
$imageFields = array();
foreach($page->fields as $f) {
if($f->type instanceof FieldtypeCropImage || ($f->inputfieldClass && $f->inputfieldClass == 'InputfieldCropImage')) {
$imageFields[] = $f;
}
}
// loop through to find the one we're looking for
foreach($imageFields as $imageField) {
// good to get unformatted in case it's a single image field,
// because it'll still be an array rather than 1 image
$pagefiles = $page->getUnformatted($imageField->name);
// if the image's pagefiles property matches the one with the
// field we're looking at, we have a match. save in $field
if($image->pagefiles === $pagefiles) {
$field = $imageField->getInputfield($page);
break;
}
}
if($field) {
//$event->return = $out;
return $field;
}
return null;
}
public function install() {
parent::___install();
$this->modules->get('ProcessCropImage');
$this->modules->get('InputfieldCropImage');
}
private function _copyAndResize($img, $imgPath) {
if(@copy($img->pagefiles->path . $img->basename, $imgPath)) {
$sizer = new ImageSizer($imgPath);
$sizer->resize($this->w, $this->h);
}
}
}