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

Contents of /contributions/modules/image_exact/image_exact.module

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


Revision 1.5 - (show annotations) (download) (as text)
Fri Mar 7 19:59:05 2008 UTC (20 months, 3 weeks ago) by joshk
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5
Changes since 1.4: +2 -3 lines
File MIME type: text/x-php
Resolves http://drupal.org/node/180921
thanks tomislander!
1 <?php
2 // $Id: image_exact.module,v 1.4 2007/05/27 20:19:50 joshk Exp $
3
4 /**
5 * Implementation of hook_help().
6 */
7
8 function image_exact_help($section) {
9 switch ($section) {
10 case 'admin/modules#description':
11 return t("Implements exact-size image thumbnail and avatars via cropping, based on max h/w from the image content type settings.");
12 }
13 }
14
15 /*
16 * Implementation of hook_menu
17 */
18 function image_exact_menu($may_cache) {
19 $items[] = array(
20 'path' => 'admin/settings/image_exact',
21 'title' => t('Image Exact'),
22 'description' => t('Configure the Image Exact module.'),
23 'callback' => 'drupal_get_form',
24 'callback arguments' => array('image_exact_admin_settings'),
25 'access' => user_access('administer site configuration'),
26 'type' => MENU_NORMAL_ITEM, // optional
27 );
28 return $items;
29 }
30
31 function image_exact_admin_settings() {
32 if (!image_get_toolkit()) {
33 drupal_set_message('You must enable an image toolkit for this module to work!', 'error');
34 }
35 else {
36 $image_settings = l('image settings page', 'admin/settings/image');
37 $user_settings = l('user settings page', 'admin/user/settings');
38 $form['image_exact_nodes'] = array(
39 '#type' => 'fieldset',
40 '#title' => t('Image Node Settings'),
41 '#collapsible' => true,
42 '#collapsed' => false,
43 );
44 $form['image_exact_nodes']['image_exact_thumbs'] = array(
45 '#type' => 'checkbox',
46 '#title' => t('Use exact sizes for image-nodes'),
47 '#default_value' => variable_get('image_exact_thumbs', 1),
48 '#description' => t('If checked, this will force images of the size(s) checked below (as defined on the !link) to be exactly the size specified. Otherwise, the settings below will be ignored.', array('!link' => $image_settings)),
49 );
50 foreach (_image_get_sizes() as $count => $size) {
51 $options[$count] = $size['label'];
52 }
53 $form['image_exact_nodes']['image_exact_size'] = array(
54 '#type' => 'checkboxes',
55 '#title' => t('Specific Image Size Settings'),
56 '#default_value' => variable_get('image_exact_size', array(0)),
57 '#options' => $options,
58 '#description' => t('Each image size checked will be cropped and resized to the specified size defined in the !link. Note that existing images will not be resized until viewing that specific image edit tab, and possibly refreshing the browser.', array('!link' => $image_settings)),
59 );
60
61 if (variable_get('image_exact_avatars', 0)) {
62 list($final_w, $final_h) = explode('x', variable_get('user_picture_dimensions', '85x85'));
63 $w = round($final_w / 2);
64 $h = round($final_h / 2);
65 $form['image_exact_warning'] = array(
66 '#title' => t('Exact Avatar Size'),
67 '#value' => t('NOTICE: your exact-size avatars will be sized at @w pixels wide and @h pixels tall. To change this enter DOUBLE your desired amount on the !link', array('!link' => $user_settings, '@w' => $w, '@h' => $h))
68 );
69 }
70 $form['image_exact_avatars'] = array(
71 '#type' => 'checkbox',
72 '#title' => t('Use exact sizes for avatars?'),
73 '#default_value' => variable_get('image_exact_avatars', 0),
74 '#description' => t('Because of how the avatar system works, you need to enter a value in the !link that is DOUBLE what you want the real avatar size to be. If you want 85x85 exact, check the above box, then change the setting on the !link to 170x170', array('!link' => $user_settings)),
75 );
76 }
77 return system_settings_form($form);
78 }
79
80 /*
81 * Implementation of hook_nodeapi
82 *
83 * Looks for images and if the setting is set will resize thumbs
84 */
85
86 function image_exact_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
87 //Set thumbnail final dimensions here - use settings from image content type.
88 if ($node->type == 'image' && $op == 'validate' && variable_get('image_exact_thumbs', 1)) {
89 $sizes = _image_get_sizes();
90 foreach(variable_get('image_exact_size', array(0)) as $i) {
91 $source = file_create_path($node->images['_original']);
92 $destination = file_create_path($node->images[$i]);
93 $final_w = $sizes[$i]['width'];
94 $final_h = $sizes[$i]['height'];
95 if ($final_w && $final_h) {
96 image_exact_resize($source, $destination, $final_w, $final_h);
97 }
98 }
99 }
100 }
101
102 /*
103 * Implementation of hook_user
104 * Handles exact-sizes for avatars
105 */
106
107 function image_exact_user($op, &$edit, &$user, $category = NULL) {
108 if ($op == 'validate' && variable_get('image_exact_avatars', 0)) {
109 list($final_w, $final_h) = explode('x', variable_get('user_picture_dimensions', '190x190'));
110 $w = round($final_w / 2);
111 $h = round($final_h / 2);
112 image_exact_resize($edit['picture'], $edit['picture'], $w, $h);
113 }
114 }
115
116 function image_exact_resize($source, $destination, $final_w, $final_h) {
117 if (file_exists($source)) {
118 $source_info = image_get_info($source);
119 $source_ar = $source_info['width'] / $source_info['height'];
120 if ($source_info['width'] > $final_w && $source_info['height'] > $final_h) {
121 // only proceed if we've got a big enough source file... don't stretch a tiny one
122 $final_ar = $final_w / $final_h;
123 if($source_ar > $final_ar) { //Too wide!
124 $width = round($source_info['width'] / ($source_ar / $final_ar));
125 $x = round(($source_info['width'] - $width) / 2); // Start the crop at the halfway point to retain center
126 $y = 0;
127 $height = $source_info['height'];
128 image_crop($source,$destination,$x,$y,$width,$height);
129 // drupal_set_message("Crop: $x,$y,$width,$height",'message');
130 } elseif ($source_ar < $final_ar) { // Too tall!
131 $height = round($source_info['height'] * ($source_ar / $final_ar));
132 $y = round(($source_info['height'] - $height) / 2); //Start the crop at the halfway point to retain center
133 $x = 0;
134 $width = $source_info['width'];
135 image_crop($source,$destination,$x,$y,$width,$height);
136 // drupal_set_message("Crop: $x,$y,$width,$height",'message');
137 }
138 }
139 image_resize($destination, $destination, $final_w, $final_h);
140 // drupal_set_message("Resize: $final_w, $final_h",'message');
141 if(!file_exists($destination)) {
142 drupal_set_message("Image_exact: Image resize failed.","error");
143 }
144 } else {
145 drupal_set_message('image_exact: File does not exist.','error');
146 }
147 }
148 ?>

  ViewVC Help
Powered by ViewVC 1.1.2