forked from Notanotherdotcom/Thumbnails
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldtypeCropImage.module
More file actions
139 lines (98 loc) · 3.68 KB
/
FieldtypeCropImage.module
File metadata and controls
139 lines (98 loc) · 3.68 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
<?php
class FieldtypeCropImage extends FieldtypeImage {
public static function getModuleInfo() {
return array(
'title' => 'Images with cropping',
'version' => 100,
'summary' => 'Field that stores one or more GIF, JPG, or PNG images',
);
}
public function init() {
$this->addHook('Pageimage::getThumb', $this, 'getThumb');
}
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("There is no thumb called: $thumb");
return;
}
if ($this->w < 1 || $this->h < 1) {
throw new WireException("Width and/or height not found for thumb: $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;
}
// Image not found, we take basic crop from original
else {
$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
$imageFields = $page->fields->find('type=FieldtypeCropImage');
// 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;
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);
}
}
}