/[drupal]/contributions/modules/avatar_gallery/avatar_gallery.module
ViewVC logotype

Contents of /contributions/modules/avatar_gallery/avatar_gallery.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.7 - (show annotations) (download) (as text)
Thu May 22 20:38:29 2008 UTC (18 months, 1 week ago) by pfournier
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +10 -5 lines
File MIME type: text/x-php
Fixed a warning
1 <?php
2 // $Id: avatar_gallery.module,v 1.6 2008/05/11 01:55:32 pfournier Exp $
3
4 /**
5 * Implementation of hook_menu()
6 *
7 * @see hook_menu()
8 */
9 function avatar_gallery_menu($may_cache) {
10 $items = array();
11 if ($may_cache) {
12 $items[] = array(
13 'path' => 'admin/settings/avatar_gallery',
14 'title' => t('Avatar gallery'),
15 'description' => t('Configure the avatar gallery.'),
16 'callback' => 'drupal_get_form',
17 'callback arguments' => array('avatar_gallery_admin_settings'),
18 'access' => user_access('administer site configuration'),
19 $items[] = array(
20 'path' => 'admin/settings/avatar_gallery/settings',
21 'title' => t('Settings'),
22 'description' => t('Configure images and text.'),
23 'access' => user_access('administer site configuration'),
24 'type' => MENU_DEFAULT_LOCAL_TASK),
25 'weight' => 0,
26 );
27 $items[] = array(
28 'path' => 'admin/settings/avatar_gallery/view',
29 'title' => t('View'),
30 'callback' => 'drupal_get_form',
31 'callback arguments' => array('avatar_gallery_view'),
32 'access' => user_access('administer site configuration'),
33 'type' => MENU_LOCAL_TASK,
34 'weight' => 1,
35 );
36 }
37 return $items;
38 }
39
40 /**
41 * Implementation of hook_block().
42 */
43 function avatar_gallery_block($op = 'list', $delta = 0) {
44 $menu = menu_get_menu();
45
46 if ($op == 'list') {
47 $blocks[]=array("info"=>t("Avatar gallery"));
48 return $blocks;
49 }
50 else if ($op == 'view') {
51 $data['subject'] = t("Avatar gallery");
52 $result = db_fetch_object(db_query("SELECT COUNT(u.uid) as count FROM {users} u WHERE u.status != 0 and u.uid != 0"));
53 $members = format_plural($result->count, '1 user', '@count users');
54 if (module_exists('profile')) {
55 $members = l($members, 'profile');
56 }
57 $data['content'] .= '<div id="usercount">'.t('There are !members on @sitename.', array('!members' => $members, '@sitename' => variable_get('site_name', 'drupal'))).'</div>';
58 $data['content'] .= _avatar_gallery_imagemap();
59 return $data;
60 }
61 }
62
63 /**
64 * Implementation of hook_user()
65 *
66 * On user creation or modification, regenerates the gallery
67 */
68 function avatar_gallery_user($op, $edit, $user) {
69 switch ($op) {
70 case 'update':
71 {
72 avatar_gallery_regenerate();
73 break;
74 }
75 case 'login':
76 {
77 $order = variable_get('avatar_gallery_ordering', 0);
78 if ($order == 2) {
79 avatar_gallery_regenerate();
80 }
81 break;
82 }
83 case 'insert':
84 case 'delete':
85 {
86 if (isset($edit["picture"])) {
87 avatar_gallery_regenerate();
88 }
89 break;
90 }
91 }
92 }
93
94 /**
95 * Menu callback to display the avatar gallery.
96 *
97 * @see _avatar_gallery_imagemap()
98 */
99 function avatar_gallery_view() {
100 $form['gallery'] = array(
101 '#value' => _avatar_gallery_imagemap(),
102 );
103
104 $form['submit'] = array(
105 '#type' => 'submit',
106 '#value' => t('Regenerate'),
107 );
108
109 return $form;
110 }
111
112 function avatar_gallery_view_submit($form_id, $form_values) {
113 avatar_gallery_regenerate();
114 return "admin/settings/avatar_gallery/view";
115 }
116
117 /**
118 * Menu callback for administration settings page.
119 */
120 function avatar_gallery_admin_settings() {
121 $form['visual'] = array(
122 '#type' => 'fieldset',
123 '#title' => t('General Settings'),
124 '#collapsible' => TRUE,
125 '#collapsed' => FALSE,
126 );
127 $form['visual']['avatar_gallery_ordering'] = array(
128 '#type' => 'select',
129 '#title' => t('Ordering'),
130 '#description' => t('Ordering of the pictures, left to right, then top to bottom.'),
131 '#options' => _avatar_gallery_ordering('strings'),
132 '#default_value' => variable_get('avatar_gallery_ordering', '0'),
133 );
134 $form['visual']['avatar_gallery_num_cols'] = array(
135 '#type' => 'textfield',
136 '#title' => t('Columns'),
137 '#description' => t('Number of pictures in one row of the gallery.'),
138 '#size' => 5,
139 '#maxlength' => 5,
140 '#default_value' => variable_get('avatar_gallery_num_cols', 6),
141 );
142 $form['visual']['avatar_gallery_num_rows'] = array(
143 '#type' => 'textfield',
144 '#title' => t('Rows'),
145 '#size' => 5,
146 '#maxlength' => 5,
147 '#description' => t('Maximum number of rows in the gallery. Zero means no limit (all users will be listed).'),
148 '#default_value' => variable_get('avatar_gallery_num_rows', 0),
149 );
150 $form['visual']['avatar_gallery_margin'] = array(
151 '#type' => 'textfield',
152 '#title' => t('Margin'),
153 '#description' => t('Margin around the avatar gallery, in pixels.'),
154 '#size' => 5,
155 '#maxlength' => 5,
156 '#default_value' => variable_get('avatar_gallery_margin', 25),
157 );
158 $form['visual']['avatar_gallery_hspacing'] = array(
159 '#type' => 'textfield',
160 '#title' => t('Column Width'),
161 '#description' => t('Column width, in pixels.'),
162 '#size' => 5,
163 '#maxlength' => 5,
164 '#default_value' => variable_get('avatar_gallery_hspacing', 100),
165 );
166 $form['visual']['avatar_gallery_vspacing'] = array(
167 '#type' => 'textfield',
168 '#title' => t('Row Height'),
169 '#description' => t('Row height, in pixels.'),
170 '#size' => 5,
171 '#maxlength' => 5,
172 '#default_value' => variable_get('avatar_gallery_vspacing', 100),
173 );
174 $form['visual']['avatar_gallery_thumb_hsize'] = array(
175 '#type' => 'textfield',
176 '#title' => t('Picture Width'),
177 '#description' => t('Width of the pictures, in pixels.'),
178 '#size' => 5,
179 '#maxlength' => 5,
180 '#default_value' => variable_get('avatar_gallery_thumb_hsize', 85),
181 );
182 $form['visual']['avatar_gallery_thumb_vsize'] = array(
183 '#type' => 'textfield',
184 '#title' => t('Picture Height'),
185 '#description' => t('Height of the pictures, in pixels.'),
186 '#size' => 5,
187 '#maxlength' => 5,
188 '#default_value' => variable_get('avatar_gallery_thumb_vsize', 85),
189 );
190 $form['visual']['avatar_gallery_crop_images'] = array(
191 '#type' => 'checkbox',
192 '#title' => t('Crop Images'),
193 '#description' => t('Crop the images so that their aspect ratio matches the one specified by Picture Width and Picture Height, above.'),
194 '#default_value' => variable_get('avatar_gallery_crop_images', FALSE),
195 );
196 $form['visual']['avatar_gallery_quality'] = array(
197 '#type' => 'textfield',
198 '#title' => t('JPEG Quality'),
199 '#description' => t('An integer between 0 and 100; higher values means better looking pictures, but larger gallery size.'),
200 '#size' => 5,
201 '#maxlength' => 5,
202 '#default_value' => variable_get('avatar_gallery_quality', 80),
203 );
204 $form['visual']['background_color'] = array(
205 '#type' => 'fieldset',
206 '#title' => t('Background Color'),
207 '#collapsible' => TRUE,
208 '#collapsed' => TRUE,
209 );
210 $form['visual']['background_color']['avatar_gallery_bcolr'] = array(
211 '#type' => 'textfield',
212 '#title' => t('Red'),
213 '#size' => 5,
214 '#maxlength' => 5,
215 '#default_value' => variable_get('avatar_gallery_bcolr', 255),
216 );
217 $form['visual']['background_color']['avatar_gallery_bcolg'] = array(
218 '#type' => 'textfield',
219 '#title' => t('Green'),
220 '#size' => 5,
221 '#maxlength' => 5,
222 '#default_value' => variable_get('avatar_gallery_bcolg', 255),
223 );
224 $form['visual']['background_color']['avatar_gallery_bcolb'] = array(
225 '#type' => 'textfield',
226 '#title' => t('Blue'),
227 '#size' => 5,
228 '#maxlength' => 5,
229 '#default_value' => variable_get('avatar_gallery_bcolb', 255),
230 );
231
232 $form['text'] = array(
233 '#type' => 'fieldset',
234 '#title' => t('Text Settings'),
235 '#collapsible' => TRUE,
236 '#collapsed' => TRUE,
237 );
238 $form['text']['avatar_gallery_showtext'] = array(
239 '#type' => 'checkbox',
240 '#title' => t('Show User Name'),
241 '#description' => t('Show user name under each picture.'),
242 '#default_value' => variable_get('avatar_gallery_showtext', 0),
243 );
244 $form['text']['avatar_gallery_font'] = array(
245 '#type' => 'textfield',
246 '#title' => t('TrueType Font Path'),
247 '#description' => t('Path of the TrueType font to use for the text (e.g. files/arial.ttf).'),
248 '#size' => 50,
249 '#maxlength' => 80,
250 '#default_value' => variable_get('avatar_gallery_font', ''),
251 );
252 $form['text']['avatar_gallery_textsize'] = array(
253 '#type' => 'textfield',
254 '#title' => t('Font Size'),
255 '#description' => t('Font size in pixels.'),
256 '#size' => 5,
257 '#maxlength' => 5,
258 '#default_value' => variable_get('avatar_gallery_textsize', 15),
259 );
260 $form['text']['text_color'] = array(
261 '#type' => 'fieldset',
262 '#title' => t('Text Color'),
263 '#collapsible' => TRUE,
264 '#collapsed' => TRUE,
265 );
266 $form['text']['text_color']['avatar_gallery_tcolr'] = array(
267 '#type' => 'textfield',
268 '#title' => t('Red'),
269 '#size' => 5,
270 '#maxlength' => 5,
271 '#default_value' => variable_get('avatar_gallery_tcolr', 0),
272 );
273 $form['text']['text_color']['avatar_gallery_tcolg'] = array(
274 '#type' => 'textfield',
275 '#title' => t('Green'),
276 '#size' => 5,
277 '#maxlength' => 5,
278 '#default_value' => variable_get('avatar_gallery_tcolg', 0),
279 );
280 $form['text']['text_color']['avatar_gallery_tcolb'] = array(
281 '#type' => 'textfield',
282 '#title' => t('Blue'),
283 '#size' => 5,
284 '#maxlength' => 5,
285 '#default_value' => variable_get('avatar_gallery_tcolb', 0),
286 );
287
288 $form['misc'] = array(
289 '#type' => 'fieldset',
290 '#title' => t('Missing Picture'),
291 '#collapsible' => TRUE,
292 '#collapsed' => FALSE,
293 );
294 $form['misc']['avatar_gallery_showall'] = array(
295 '#type' => 'checkbox',
296 '#title' => t('Show users with no avatars'),
297 '#default_value' => variable_get('avatar_gallery_showall', 0),
298 );
299 $form['misc']['avatar_gallery_miss_image'] = array(
300 '#type' => 'textfield',
301 '#title' => t('Image for missing avatars'),
302 '#description' => t('Path of the image to use for users with no picture (e.g. files/avatar.jpg).'),
303 '#size' => 50,
304 '#maxlength' => 80,
305 '#default_value' => variable_get('avatar_gallery_miss_image', ''),
306 );
307
308 return system_settings_form($form);
309 }
310
311 function _avatar_gallery_ordering($type) {
312 if ($type == 'strings') {
313 return array(
314 '0' => t('Newest first'),
315 '1' => t('By name'),
316 '2' => t('Last log in'),
317 );
318
319 }
320 else if ($type == 'sql') {
321 return array(
322 'created DESC',
323 'name ASC',
324 'login DESC',
325 );
326 }
327 }
328
329
330 /**
331 * Generate the HTML code necessary to display the image and the area map
332 *
333 * @args $root string the base url of links
334 * @returns string generally an <IMG> tag followed by a <AREA> tag
335 */
336 function _avatar_gallery_imagemap() {
337 $img_file = variable_get('file_directory_path', 'files' ) . '/' . variable_get('avatar_gallery_image_name', 'avatar_gallery.jpg');
338 $img = theme_image($img_file, t('Avatar gallery'), t('Avatar gallery'), array('border' => 0, 'usemap' => '#avatar_gallery_areamap'), FALSE) . _avatar_gallery_areamap_dyn($root);
339 return $img;
340 }
341
342 /**
343 * Build an areamap from coordinates cached by avatar_gallery_regenerate()
344 *
345 * This will call avatar_gallery_regenerate() to recreate the cache if
346 * missing or expired
347 *
348 * @arg $root string the base of urls
349 * @return string an <areamap> named avatar_gallery_areamap
350 * corresponding to the image created by avatar_gallery_regenerate()
351 * @see avatar_gallery_regenerate()
352 */
353 function _avatar_gallery_areamap_dyn($root = '') {
354 if (!$cache = cache_get('avatar_gallery_coordinates')) {
355 avatar_gallery_regenerate();
356 $cache = cache_get('avatar_gallery_coordinates');
357 }
358
359 $coords = unserialize($cache->data);
360 if ($coords) {
361 $amap = '';
362 foreach ($coords as $uid => $coord) {
363 $amap .= '<area shape="rect" coords="' . $coord . '" href="' . $root . url("user/" . $uid) . '">';
364 }
365 return '<map name="avatar_gallery_areamap">' . $amap . '</map>';
366 }
367 else {
368 return '<map name="avatar_gallery_areamap"/>';
369 }
370 }
371
372 /**
373 * Regenerate the avatar gallery
374 *
375 * This is where most of the work is done. here the image is created
376 * and the coordinates of the image map are computed and cached as
377 * avatar_gallery_coordinates in the cache system.
378 *
379 * The image is created in $file_directory_path/avatar_gallery_md5(time()).jpg
380 */
381 function avatar_gallery_regenerate()
382 {
383 $order_value = variable_get('avatar_gallery_ordering', 0);
384 $order_sql = _avatar_gallery_ordering('sql');
385 $order = $order_sql[$order_value];
386
387 $miss_image = variable_get('avatar_gallery_miss_image', '');
388 $show_miss = variable_get('avatar_gallery_showall', 0) && $miss_image;
389
390 $numcols = variable_get('avatar_gallery_num_cols', 6);
391
392 $query = 'SELECT uid,name,picture FROM {users} WHERE status != 0 and uid != 0';
393 if (!$show_miss) {
394 $query .= ' and picture <> \'\'';
395 }
396 $query .= ' ORDER BY %s';
397
398 $row_limit = variable_get('avatar_gallery_num_rows', 0);
399 if ($row_limit != 0) {
400 $limit = $row_limit * $numcols;
401 $query .= ' LIMIT ' . $limit;
402 }
403
404 $items = array();
405 $result = db_query($query, $order);
406 while ($account = db_fetch_object($result)) {
407 $items[] = $account;
408 }
409
410 // create background
411 $hspacing = variable_get('avatar_gallery_hspacing', 100);
412 $vspacing = variable_get('avatar_gallery_vspacing', 100);
413 $margin = variable_get('avatar_gallery_margin', 25);
414
415 $bcolr = variable_get('avatar_gallery_bcolr', 255);
416 $bcolg = variable_get('avatar_gallery_bcolg', 255);
417 $bcolb = variable_get('avatar_gallery_bcolb', 255);
418
419 $new_w = variable_get('avatar_gallery_thumb_hsize', 85);
420 $new_h = variable_get('avatar_gallery_thumb_vsize', 85);
421 $crop = variable_get('avatar_gallery_crop_images', FALSE);
422
423 $textsize = variable_get('avatar_gallery_textsize', 15);
424 $font = variable_get('avatar_gallery_font', '');
425 $showtext = variable_get('avatar_gallery_showtext', 0) && $font;
426 $tcolr = variable_get('avatar_gallery_tcolr', 0);
427 $tcolg = variable_get('avatar_gallery_tcolg', 0);
428 $tcolb = variable_get('avatar_gallery_tcolb', 0);
429
430 $quality = variable_get('avatar_gallery_quality', 80);
431
432 $maxx = $numcols * $hspacing + $margin * 2;
433 $maxy = ceil(count($items) / $numcols) * $vspacing + $margin * 2;
434
435 if ($maxy == 0) {
436 $maxy = $vspacing;
437 }
438
439 $dst_img = ImageCreateTrueColor($maxx, $maxy);
440 $bcol = imagecolorallocate($dst_img, $bcolr, $bcolg, $bcolb);
441 imagefill($dst_img, 0, 0, $bcol);
442
443 $tcol = imagecolorallocate($dst_img, $tcolr, $tcolg, $tcolb);
444
445 if ($showtext) {
446 $dimensions = imagettfbbox($textsize, 0, $font, 'W');
447 $lineHeight = ($dimensions[1] - $dimensions[7]); // the height of a single line
448 }
449
450 $colnum = -1;
451 $rownum = 0;
452 foreach ($items as $k => $v) {
453
454 $picfile = $v->picture;
455 if ($picfile != '' || $show_miss) {
456 if ($picfile == '' || !file_exists($picfile)) {
457 if ($miss_image == '') {
458 drupal_set_message(t('The missing image file has not been specified'));
459 }
460 $picfile = $miss_image;
461 }
462 $colnum++;
463 if ($colnum >= $numcols) {
464 $rownum++;
465 $colnum=0;
466 }
467
468 // we have a picture
469 $image_data = @getimagesize($picfile);
470 $image_type = $image_data[2];
471 if ($image_type == 1) {
472 $src_img = imagecreatefromgif($picfile);
473 }
474 elseif ($image_type == 2) {
475 $src_img = imagecreatefromjpeg($picfile);
476 }
477 elseif ($image_type == 3) {
478 $src_img = imagecreatefrompng($picfile);
479 }
480 else {
481 if ($picfile != '') {
482 drupal_set_message(t('Avatar Gallery: unsupported image format: @picfile', array('@picfile' => $picfile)));
483 }
484 continue;
485 }
486
487 $old_w = imagesx($src_img);
488 $old_h = imagesy($src_img);
489
490 if ($crop) {
491 $thumb_w = $new_w;
492 $thumb_h = $new_h;
493
494 $pic_x = $colnum * $hspacing + $margin;
495 $pic_y = $rownum * $vspacing + $margin;
496
497 if (($old_w / $old_h) > ($new_w / $new_h)) {
498 $w = $new_w * $old_h / $new_h;
499
500 $crop_x = ($old_w - $w) / 2;
501 $crop_y = 0;
502
503 $old_w = $w;
504 }
505 else if (($old_w / $old_h) < ($new_w / $new_h)) {
506 $h = $new_h * $old_w / $new_w;
507
508 $crop_x = 0;
509 $crop_y = ($old_h - $h) / 2;
510
511 $old_h = $h;
512 }
513 else if (($old_w / $old_h) == ($new_w / $new_h)) {
514 $crop_x = 0;
515 $crop_y = 0;
516 }
517 }
518 else {
519 if (($old_w / $old_h) > ($new_w / $new_h)) {
520 $thumb_w = $new_w;
521 $thumb_h = $old_h * ($new_w/$old_w);
522 }
523 else if (($old_w / $old_h) < ($new_w / $new_h)) {
524 $thumb_w = $old_w * ($new_h/$old_h);
525 $thumb_h = $new_h;
526 }
527 else if (($old_w / $old_h) == ($new_w / $new_h)) {
528 $thumb_w = $new_w;
529 $thumb_h = $new_h;
530 }
531
532 $maxs = max($thumb_w, $thumb_h);
533
534 $pic_x = ($maxs-$thumb_w)/2 + $colnum * $hspacing + $margin;
535 $pic_y = ($maxs-$thumb_h)/2 + $rownum * $vspacing + $margin;
536
537 $crop_x = 0;
538 $crop_y = 0;
539 }
540
541 imagecopyresampled(
542 $dst_img, $src_img,
543 $pic_x, $pic_y,
544 $crop_x, $crop_y,
545 $thumb_w, $thumb_h,
546 $old_w, $old_h);
547
548 $coords[$v->uid] = $pic_x.",".$pic_y.",".($pic_x+$thumb_w).",".($pic_y+$thumb_h);
549
550 // now write name of user under picture
551 if ($showtext) {
552 $dimensions = imagettfbbox($textsize, 0, $font, $v->name);
553 $lineWidth = $dimensions[2] - $dimensions[0]; // get the length of this line, if the word is to be included
554
555 $leftStart=-$lineWidth/2;
556 imagettftext(
557 $dst_img,
558 $textsize,
559 0,
560 $colnum * $hspacing + $margin + $new_w / 2 + $leftStart,
561 $rownum * $vspacing + $margin + $new_h + $lineHeight,
562 $tcol,
563 $font,
564 $v->name);
565 }
566
567 // destroy thumbnail
568 imagedestroy($src_img);
569 }
570 }
571
572 $current_image_path = variable_get('file_directory_path', 'files') . '/' . variable_get('avatar_gallery_image_name', 'avatar_gallery.jpg');
573
574 $new_image_name = 'avatar_gallery_' . md5(time()) . '.jpg';
575 imagejpeg($dst_img, variable_get('file_directory_path', 'files') . '/' . $new_image_name, $quality);
576 variable_set('avatar_gallery_image_name', $new_image_name);
577
578 if (file_exists($current_image_path)) {
579 file_delete($current_image_path);
580 }
581
582 imagedestroy($dst_img);
583
584 cache_clear_all('avatar_gallery_coordinates', 'cache');
585 cache_set('avatar_gallery_coordinates', 'cache', serialize($coords), CACHE_PERMANENT);
586 }
587
588 ?>

  ViewVC Help
Powered by ViewVC 1.1.2