-
Notifications
You must be signed in to change notification settings - Fork 1
/
imgrd.php
272 lines (226 loc) · 6.33 KB
/
imgrd.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/**
* imgrd creates a nice g+ style grid for your images.
*
* @author Christoph Bach <info@christoph-bach.net>
*
**/
function imgrd ($images, $options = array()) {
$default = array(
'width' => 450,
'margin' => 10,
'imagesPerRow' => 3,
'cropLast' => false,
'rowsPerPage' => 0
);
$options = array_merge($default, $options);
$imgrd = new Imgrd($images, $options);
return $imgrd;
}
class Imgrd {
private $images = array();
private $rows = array();
private $gridWidth;
private $gridMargin;
private $originals = array();
private $cropLast;
private $imagesPerRow;
private $rowsPerPage;
private $imgWidth = 0;
private $imgHeight = 0;
private $page = 0;
function __construct($images, $options) {
$this->images = $images;
$this->gridWidth = $options['width'] + $options['margin'];
$this->gridMargin = $options['margin'];
$this->cropLast = $options['cropLast'];
$this->imagesPerRow = $options['imagesPerRow'];
$this->rowsPerPage = $options['rowsPerPage'];
global $site;
$this->page = ($site->uri()->params('page') > 0)? $site->uri->params('page') : 1;
$this->page--;
// prevent division by zero
$imagesPerRow = ($options['imagesPerRow'] > 1)? $options['imagesPerRow'] : 2;
// heuristic base image width
$this->imgWidth = (int) ($this->gridWidth / ($imagesPerRow - 1));
// assuming most pictures have a proportion of 4:3
$this->imgHeight = (int) ($this->imgWidth / 4 * 3);
// create the basic grid
$this->grid();
}
/**
* This function will generate the grid's rows. Therefore it uses
* a rough estimation generated by fitWidth() and fitHeight() to
* calculate the number of images per row.
**/
public function grid() {
$row = array();
$rowWidth = 0;
$i = 0;
$images = $this->images;
foreach($images as $img) {
$this->originals[$i] = clone $img;
// scale the image to nice base size
if ($this->getOrientation($img) < 1) {
$img->fitWidth($this->imgWidth, true);
} else {
$img->fitHeight($this->imgHeight, true);
}
$rowWidth = $rowWidth + $img->width() + $this->gridMargin;
$row[$i] = $img;
// begin new row if current one is overfull
if ($rowWidth-$this->gridMargin >= $this->gridWidth
|| $i == $images->count()-1) {
$this->rows[] = array(
'images' => $row,
'width' => $rowWidth
);
$row = array();
$rowWidth = 0;
}
$i++;
}
}
/**
* returns the next pages's URL
**/
public function getNextURL() {
global $site;
return ($this->hasNext())? $site->uri()->path()."/page:".($this->page + 2) : false;
}
/**
* @return the previous pages's URL
**/
public function getPreviousURL() {
global $site;
if ($this->page === 1)
return $site->uri()->path();
else
return ($this->hasPrevious())? $site->uri()->path()."/page:".($this->page) : false;
}
/**
* @return true if there's a previous page.
**/
public function hasPrevious() {
return (bool) ($this->page > 0);
}
/**
* @return true if there's another page
**/
public function hasNext() {
if ($this->rowsPerPage < 1) {
return false;
} else {
return (bool) ($this->page + 1 < $this->countPages());
}
}
/**
* @return number of pages
**/
public function countPages() {
if ($this->rowsPerPage > 0) {
$numRows = count($this->rows);
$add = ($numRows % $this->rowsPerPage > 0)? 1 : 0;
return ((int) ($numRows / $this->rowsPerPage) + $add);
}
return 1;
}
/**
* prints the grid.
**/
public function show() {
// print everything
if ($this->rowsPerPage < 1) {
$this->printRows(0, count($this->rows)-1);
} else {
$min = $this->page * $this->rowsPerPage;
$max = $min + $this->rowsPerPage;
if ($min < count($this->rows)) {
$this->printRows($min, $max-1);
}
}
}
/**
* prints rows from $start to $end
**/
private function printRows($start, $end) {
$start = max(0, $start);
$end = min($end, count($this->rows)-1);
for ($i = $start; $i <= $end; $i++) {
$this->fitRow($this->rows[$i]['images'], $this->rows[$i]['width']);
}
}
/**
* This function will make the images of a row the same height
* and it will scale all images so that they exactly fit in a row
*
* @param $row
* Array of image objects that will fill a row. The row's width should
* be equal or larger compared to the grid's width. Otherwise it will
* assume that this is the last row of the grid and will just adjust
* the images' height.
* @param $rowWidth
* The width of all images together
**/
private function fitRow($row, $rowWidth) {
$margin = (count($row)) * $this->gridMargin;
if ($rowWidth >= 0.9 * $this->gridWidth)
$scaleFactor = ($this->gridWidth - $margin) / ($rowWidth - $margin);
else {
$scaleFactor = 1;
}
$fixedHeight = 0;
$realWidth = 0;
$i = 0;
// scale images down, so that they will fit
foreach ($row as $key => $img) {
$newWidth = (int) ($img->width() * $scaleFactor);
$img->fitWidth($newWidth, true);
// create fixed height so that every image has the same height
if ($fixedHeight > 0)
$img->info()->height = $fixedHeight;
else
$fixedHeight = $img->height();
// last element will be cropped to pixel-perfect size
if ($i == count($row)-1 && ($scaleFactor < 1 || $this->cropLast)) {
$fittedWidth = $this->gridWidth - $margin - $realWidth;
$img->info()->width = $fittedWidth;
}
$realWidth = $realWidth + $img->width();
$i++;
}
$this->printRow($row);
}
/**
* Prints a row.
*
* @param $row
* An array of image objects
**/
private function printRow($row) {
foreach ($row as $key => $img) {
$options = array(
'width' => $img->width(),
'height' => $img->height(),
'crop' => true
);
// modifiy markup here
echo "<a href=\"".$this->originals[$key]->url()."\"
class=\"fancybox\" rel=\"gallery\">\n";
echo thumb($this->originals[$key], $options)."\n";
echo "</a>\n";
}
}
/**
* @param $img
* the image object
* @return
* 0 for landscape
* 1 for portrait
*
**/
private function getOrientation($img) {
return ($img->width() > $img->height())? 0 : 1;
}
}
?>