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

Contents of /contributions/modules/webfm_images/webfm_images.module

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


Revision 1.4 - (show annotations) (download) (as text)
Wed Feb 20 18:30:57 2008 UTC (21 months ago) by robmilne
Branch: MAIN
CVS Tags: DRUPAL-5--1-2, HEAD
Changes since 1.3: +3 -2 lines
File MIME type: text/x-php
fix to remove reference to fname field dropped from webfm_file table
1 <?php
2 /* $Id: webfm_images.module,v 1.3 2008/01/31 17:22:50 robmilne Exp $ */
3
4 function webfm_images_menu($may_cache){
5 $items = array();
6 if($may_cache){
7 $items[] = array('title' => 'webfm images_js',
8 'type' => 'callback',
9 'callback' => 'webfm_images_js',
10 'path' => 'webfm_images_js',
11 'access' => 'access content');
12 $items[] = array('path' => 'admin/settings/webfm_images',
13 'title' => t('WebFM images'),
14 'callback' => 'drupal_get_form',
15 'callback arguments' => array('webfm_images_settings'),
16 'access' => user_access('administer site configuration'),
17 'type' => MENU_NORMAL_ITEM,
18 'description' => t('Image module settings.'),
19 );
20 }
21 return $items;
22 }
23
24 /**
25 * Form API callback to validate the webfm_images settings form.
26 */
27 function webfm_images_settings_validate($form_id, $form_values) {
28 $err_element = array();
29 $err_element[0] = 'webfm_images_sizes';
30 foreach($form_values['webfm_images_sizes'] as $key => $size) {
31 $err_element[1] = $key;
32 $label = $size['label'];
33 $width = $size['width'];
34 $height = $size['height'];
35 if(!empty($label)) {
36 if(empty($width) && empty($height)) {
37 $err_element[2] = 'label';
38 form_set_error(implode('][', $err_element), t('There must be at least one dimension set for %label.', array('%label' => $label)));
39 }
40 if(!empty($width) && !is_numeric($width)) {
41 $err_element[2] = 'width';
42 form_set_error(implode('][', $err_element), t('Invalid width set for %label.', array('%label' => $label)));
43 }
44 if(!empty($height) && !is_numeric($height)) {
45 $err_element[2] = 'height';
46 form_set_error(implode('][', $err_element), t('Invalid height set for %label.', array('%label' => $label)));
47 }
48 } else if(!empty($width) || !empty($height)) {
49 $err_element[2] = 'label';
50 form_set_error(implode('][', $err_element), t('Size name missing.'));
51 }
52 }
53 }
54
55 /**
56 * Admin settings callback.
57 * The only settings available are the target dimensions of an image file
58 * This settings code is mainly the same as the image module (in some cases character for character)
59 * But, in this case we don't need to worry about upload settings or anything like that - the files are already on the system.
60 *
61 * @return a system settings form
62 */
63 function webfm_images_settings() {
64 // Sanity check : make sure we've got a working toolkit - yoinked from image module
65 if (!image_get_toolkit()) {
66 drupal_set_message(t('No image toolkit is currently enabled. Without one the image module will not be able to resize your images. You can select one from the <a href="!link">image toolkit settings page</a>.', array('!link' => url('admin/settings/image-toolkit'))), 'error');
67 return false;
68 }
69 $form['sizes'] = array('#type' => 'fieldset', '#title' => t('Image sizes'));
70 $form['sizes']['webfm_images_sizes'] = webfm_images_settings_sizes_form();
71 return system_settings_form($form);
72 }
73
74 /**
75 * Helper function to return the defined sizes (or proper defaults).
76 */
77 function _webfm_images_get_sizes() {
78 $sizes = variable_get('webfm_images_sizes', array(array('width' => 100, 'height' => 100, 'label' => 'thumbnail'), ));
79 return array_filter($sizes, create_function('$size', 'return !empty($size["label"]);'));
80 }
81
82 function webfm_images_settings_sizes_form() {
83
84 $sizes = _webfm_images_get_sizes();
85
86 $form['#type'] = 'item';
87 $form['#description'] = t('Create various pixel dimensions ("thumbnail" is required).<br />
88 For each Label and dimension you enter a new "right-click" menu option will become available in WebFM for image files.<br />
89 Clicking the menu item with resize the image based on these settings.');
90 $form['#tree'] = TRUE;
91 $form['#theme'] = 'webfm_images_settings_sizes_form';
92 for ($i = 0; $i < 5; $i++) {
93 $form[$i]['label'] = array('#type' => 'textfield', '#default_value' => $sizes[$i]['label'], '#size' => 25);
94 if (in_array($sizes[$i]['label'], array('thumbnail'))) {
95 $form[$i]['label']['#attributes'] = array('disabled' => 'disabled');
96 $form[$i]['label']['#value'] = $sizes[$i]['label'];
97 }
98 $form[$i]['width'] = array('#type' => 'textfield', '#default_value' => $sizes[$i]['width'], '#size' => 5, '#maxlength' => 5);
99 $form[$i]['height'] = array('#type' => 'textfield', '#default_value' => $sizes[$i]['height'], '#size' => 5, '#maxlength' => 5);
100 }
101
102 return $form;
103 }
104
105 function theme_webfm_images_settings_sizes_form (&$form) {
106 $header = array(t('Label'), t('Width'), t('Height'));
107 foreach (element_children($form) as $key) {
108 $row = array();
109 $row[] = drupal_render($form[$key]['label']);
110 $row[] = drupal_render($form[$key]['width']);
111 $row[] = drupal_render($form[$key]['height']);
112 $rows[] = $row;
113 }
114 $output = theme('table', $header, $rows, $attributes = array('summary' => 'webfm_images_sizes'));
115 $output .= drupal_render($form);
116
117 return $output;
118 }
119
120 /**
121 * Menu callback used for AJAX calls
122 */
123 function webfm_images_js(){
124 global $user;
125
126 //3 possible outcomes - the user is either an admin, user or prohibited
127 if(($user->uid == 1) || user_access('administer webfm')) {
128 // Admins have total access
129 $webfm_perm = WEBFM_ADMIN;
130 } else if(user_access('access webfm')) {
131 $webfm_perm = WEBFM_USER;
132 } else {
133 //no feedback
134 exit();
135 }
136
137 $webfm_root_path = webfm_get_root_path();
138 if($webfm_root_path == NULL) {
139 //WebFM root dir must exist
140 webfm_json(array('status' => FALSE, 'err' => t('WebFM root not set')));
141 exit();
142 }
143 $root_dir = ($webfm_perm == WEBFM_ADMIN)? file_directory_path() : file_directory_path().$webfm_root_path;
144
145 $action = trim(strtolower(rawurldecode($_POST["action"])));
146 if($action == 'get_menus') {
147 $sizes = _webfm_images_get_sizes();
148 $arraylen = count($sizes);
149 for($i = 0; $i < $arraylen; $i++) {
150 $menus[] = $sizes[$i]['label'];
151 }
152 webfm_json(array('status' => TRUE, 'data' => $menus));
153 } else if (strpos($action, "resize to") === 0) {
154 //first check if we should operate on this file (i.e. one that is in the database)
155 $filepath = trim(rawurldecode($_POST["filepath"]));
156 if($file = webfm_get_file_record('', $root_dir.$filepath)) {
157 // Only admins, file owners or roles that can view this file can resize
158 if($webfm_perm == WEBFM_ADMIN ||
159 $user->uid == $file->uid ||
160 webfm_file_view_access($file)) {
161 $dir = dirname($file->fpath);
162 $fname = strrev(substr(strrev($file->fpath), 0, strpos(strrev($file->fpath), '/')));
163
164 //extract operation from action variable
165 $variable = str_replace('resize to ', '', $action);
166 $prefix = str_replace(' ', '_', $variable); //pepare the variable to be used as a file prefix
167 $path = $dir .'/'. $prefix. '_'. $fname; //prepare a path+filename for the file we are going to create
168 $sizes = _webfm_images_get_sizes();
169
170 foreach($sizes as $key => $value) {
171 if($value['label'] == $variable) {
172 $height = $value['height'];
173 $width = $value['width'];
174 }
175 }
176
177 //as per moshe's patch but with added protection against sys error
178 $original_size = @getimagesize ($dir .'/'. $filename);
179 if(is_object($original_size)) {
180 // scale to a fixed width
181 if (!$height) {
182 $height = ($width / $original_size[0]) * $original_size[1];
183 }
184 // scale to a fixed height
185 if (!$width) {
186 $width = ($height / $original_size[1]) * $original_size[0];
187 }
188 } else {
189 if(empty($width))
190 if(empty($height))
191 //shouldn't happen after validation
192 exit();
193 else
194 $width = $height;
195 else if(empty($height))
196 $height = $width;
197 }
198
199 //create our new file
200 if(image_scale($file->fpath, $path, $width, $height)) {
201 $err_arr = array();
202 if($ret = webfm_insert_file($path, $err_arr)) {
203 webfm_json(array('status' => TRUE, 'data' => 'resize success'));
204 } else {
205 webfm_json(array('status' => FALSE, 'data' => $err_arr));
206 }
207 } else {
208 webfm_json(array('status' => FALSE, 'data' => 'resize failed to ' . $path));
209 }
210 } else {
211 webfm_json(array('status' => FALSE, 'data' => 'insufficient params'));
212 }
213 } else {
214 webfm_json(array('status' => FALSE, 'data' => 'file '.$filepath.' not found'));
215 }
216 } else {
217 webfm_json(array('status' => FALSE, 'data' => 'unknown operation'));
218 }
219 exit();
220 }
221
222 function webfm_images_webfm_extend_js(){
223 $modulepath = drupal_get_path('module', 'webfm_images');
224 drupal_add_js($modulepath. '/webfm_images.js');
225 }
226

  ViewVC Help
Powered by ViewVC 1.1.2