/[drupal]/contributions/modules/imagepicker/imagepicker.upload.inc
ViewVC logotype

Contents of /contributions/modules/imagepicker/imagepicker.upload.inc

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


Revision 1.3 - (show annotations) (download) (as text)
Thu Aug 28 21:25:20 2008 UTC (14 months, 3 weeks ago) by hutch
Branch: MAIN
CVS Tags: DRUPAL-6--1-4, DRUPAL-6--1-5, DRUPAL-6--1-0, DRUPAL-6--1-1, DRUPAL-6--1-2, DRUPAL-6--1-3, HEAD
Branch point for: DRUPAL-6--2
Changes since 1.2: +9 -4 lines
File MIME type: text/x-php
improved menus, added admin mode to image browser, added drupal paging to image browser, added groups
1 <?php
2 // $Id: imagepicker.upload.inc,v 1.2 2008/07/01 15:02:55 hutch Exp $
3 // $Name: HEAD $
4
5 /**
6 * @file
7 * upload functions
8 */
9
10 /**
11 * Menu callback; presents the upload form for imagepicker
12 */
13 // iframe
14 function imagepicker_upload() {
15 variable_del('imagepicker_advanced_browser_pagestart');
16 $content .= "<div class='imgp_help'>" . t('Upload images. You can give them a title and description') ."</div>";
17 $content = drupal_get_form('imagepicker_upload_form');
18 theme('imagepicker', $content);
19 }
20 // account
21 function imagepicker_user_upload() {
22 variable_del('imagepicker_advanced_browser_pagestart');
23 $content .= "<div class='imgp_help'>" . t('Upload images. You can give them a title and description') ."</div>";
24 $content .= drupal_get_form('imagepicker_upload_form', TRUE);
25 return $content;
26 }
27
28 function imagepicker_upload_form(&$form_state, $account=FALSE) {
29 $form['#attributes']['enctype'] = 'multipart/form-data';
30 $form['file_upload'] = array(
31 '#type' => 'file',
32 '#title' => t('Image file'),
33 '#description' => t('Browse your computer for image file'),
34 '#required' => TRUE,
35 '#value' => 1
36 );
37 $form['thumb'] = array(
38 '#type' => 'textfield',
39 '#title' => t('Thumbnail size'),
40 '#size' => 10,
41 '#default_value' => variable_get('imagepicker_default_thumbnail_size', 100),
42 '#description' => t('Size in pixels of thumbnail\'s bigger side'),
43 '#required' => TRUE
44 );
45 $form['scale'] = array(
46 '#type' => 'textfield',
47 '#title' => t('Scale image'),
48 '#size' => 10,
49 '#default_value' => variable_get('imagepicker_default_scale', ''),
50 '#description' => t('Scale image to this size in pixels if not left empty')
51 );
52 $form['title'] = array(
53 '#type' => 'textfield',
54 '#title' => t('Title'),
55 '#description' => t('Add a title for your image')
56 );
57 $form['description'] = array(
58 '#type' => 'textarea',
59 '#title' => t('Description'),
60 '#rows' => 2,
61 '#cols' => 80,
62 '#description' => t('Add a description for your image')
63 );
64 $form['account'] = array(
65 '#type' => 'hidden',
66 '#value' => $account,
67 );
68 $form['submit'] = array(
69 '#type' => 'submit',
70 '#value' => t('Upload'),
71 );
72 return $form;
73 }
74
75 /**
76 * Validate form
77 */
78 function imagepicker_upload_form_validate($form, &$form_state) {
79 foreach ($form_state['values'] as $name => $value) {
80 $value = trim($value);
81 switch ($name) {
82 case 'file_upload':
83 if (empty($_FILES['files']['name']['file_upload'])) {
84 form_set_error($name, t('Image file field is required.'));
85 }
86 elseif (!isset($_FILES['files']['tmp_name']['file_upload']) || !file_exists($_FILES['files']['tmp_name']['file_upload'])) {
87 form_set_error($name, t('Error while uploading file.'));
88 }
89 elseif (!image_get_info($_FILES['files']['tmp_name']['file_upload'])) {
90 form_set_error($name, t('Uploaded file is not an image.'));
91 }
92 elseif (!imagepicker_get_uploaded_file_extension('file_upload')) {
93 form_set_error($name, t('Only .jpg, .gif and .png image files are accepted.'));
94 }
95 break;
96
97 case 'thumb':
98 if (!preg_match('/^[0-9]{1,3}$/', $value) || $value <= 0) {
99 form_set_error($name, t('Thumbnail size should be an integer between 1 and 999.'));
100 }
101 break;
102
103 case 'scale':
104 if (!preg_match('/^[0-9]{0,3}$/', $value)) {
105 form_set_error($name, t('Scale value should be an integer between 1 and 999 or leave it empty if you don\'t want to scale your image.'));
106 }
107 break;
108 }
109 }
110 }
111
112 /**
113 * Submit form
114 */
115 function imagepicker_upload_form_submit($form, &$form_state) {
116 if ($form_state['values']['op'] == t('Upload')) {
117 global $user;
118 $destination = imagepicker_get_path(FALSE, TRUE);
119 $thumbsdir = $destination .'thumbs';
120 $browserdir = $destination .'browser';
121
122 if (file_check_directory($destination, TRUE) && file_check_directory($thumbsdir, TRUE) && file_check_directory($browserdir, TRUE)) {
123 // Add DIRECTORY_SEPARATORS here because drupals' functions remove trailing slashes
124 $destination .= DIRECTORY_SEPARATOR;
125 $thumbsdir = $thumbsdir . DIRECTORY_SEPARATOR;
126 $browserdir = $browserdir . DIRECTORY_SEPARATOR;
127
128 $maxthumbsize = $form_state['values']['thumb'] ? $form_state['values']['thumb'] : 100;
129 $scaleto = $form_state['values']['scale'] ? $form_state['values']['scale'] : FALSE;
130
131 if (!$scaleto) {
132 // Use $path instead of original $destination variable cause this
133 // variable's value will be changed during copying file, so we won't
134 // loose it.
135 $path = $destination;
136 $imagemoved = imagepicker_copy_uploaded_file($path, 'file_upload');
137 $file = basename($path);
138 }
139 else {
140 $source = $_FILES['files']['tmp_name']['file_upload'];
141 $file = imagepicker_get_uploaded_file_name($destination, 'file_upload');
142 $imagescaled = imagepicker_scale_image($source, $destination . $file, $scaleto);
143 }
144
145 if (!$scaleto && $imagemoved || $scaleto && $imagescaled) {
146
147 // Source file should still be an uploaded one, as scaled image
148 // might have some watermarks etc. from drupal's filters/hooks.
149 $source = $_FILES['files']['tmp_name']['file_upload'];
150
151 if (imagepicker_scale_image($source, $thumbsdir . $file, $maxthumbsize)) {
152 imagepicker_scale_image($source, $browserdir . $file, variable_get('imagepicker_default_browser_thumbnail_size', 100));
153 $title = htmlspecialchars($form_state['values']['title']);
154 $description = htmlspecialchars($form_state['values']['description']);
155 $date = date('Y-m-d H:i:s');
156 $result = db_query("INSERT INTO {imagepicker} (uid, img_name, img_title, img_description, img_date) VALUES ('%d', '%s', '%s', '%s', '%s')", array($user->uid, $file, $title, $description, $date));
157 if ($result) {
158 $nextimgid = db_last_insert_id('imagepicker', 'img_id');
159 drupal_set_message(t('Image was successfully uploaded.'));
160 if ($form_state['values']['account']) {
161 $outpath = 'user/'. $user->uid .'/imagepicker/images/browse';
162 }
163 else {
164 $outpath = 'imagepicker/browse/'. $nextimgid;
165 }
166 drupal_goto($outpath);
167 }
168 else {
169 file_delete($thumbsdir . $file);
170 file_delete($browserdir . $file);
171 drupal_set_message(t('Error while saving information to database for uploaded image.'), 'error');
172 }
173 }
174 else {
175 drupal_set_message(t('Error while creating a thumbnail for uploaded image.'), 'error');
176 }
177 }
178 else {
179 if (!$scaleto && !$imagemoved) {
180 drupal_set_message(t('Error while moving uploaded file to its destination.'), 'error');
181 }
182 else {
183 drupal_set_message(t('Error while scaling uploaded file.'), 'error');
184 }
185 }
186 file_delete($destination . $file);
187 }
188 else {
189 drupal_set_message(t('Unable to create a directory structure for your images.'), 'error');
190 }
191 }
192 if ($form_state['values']['account']) {
193 $outpath = 'user/'. $user->uid .'/imagepicker';
194 }
195 else {
196 $outpath = 'imagepicker';
197 }
198
199 drupal_goto($outpath);
200 }
201
202
203 function imagepicker_copy_uploaded_file(&$destination, $name) {
204 $source = $_FILES['files']['tmp_name'][$name];
205
206 if (file_copy($source, $destination, FILE_EXISTS_RENAME)) {
207 // Fix bug in drupal's file_copy function which uses '/' instead of
208 // DIRECTORY_SEPARATOR for making directories. This causes problems on
209 // Windows mashines.
210 $source = str_replace('/', DIRECTORY_SEPARATOR, $source);
211
212 $file = imagepicker_get_uploaded_file_name($destination, $name);
213 $destination = $destination . $file;
214 return @rename($source, $destination);
215 }
216
217 return FALSE;
218 }
219
220 function imagepicker_get_uploaded_file_extension($name) {
221 switch ($_FILES['files']['type'][$name]) {
222 case 'image/pjpeg':
223 // "What genius at microsoft decided to rename the mime type for jpgs?"
224 // Thats a nice phrase I have found about this mime type :) Wonder what
225 // am I talking about? Try to upload some type of jpg image via IE7.
226 // Don't know if it's the same with IE6, but IE7 might give you a mime
227 // type of image/pjpeg. So lets just treat this 'progressive jpg' as a
228 // normal jpg image.
229 case 'image/jpeg': $fileext = '.jpg'; break;
230 case 'image/gif': $fileext = '.gif'; break;
231 case 'image/png': $fileext = '.png'; break;
232 default: $fileext = '';
233 }
234
235 return $fileext;
236 }
237
238 function imagepicker_get_uploaded_file_name($destination, $name) {
239 $fileext = imagepicker_get_uploaded_file_extension($name);
240
241 if (FALSE !== strpos($_FILES['files']['name'][$name], '.')) {
242 $filename = drupal_substr($_FILES['files']['name'][$name], 0, strrpos($_FILES['files']['name'][$name], '.'));
243 }
244 else {
245 $filename = $_FILES['files']['name'][$name];
246 }
247
248 $file = $filename . $fileext;
249 $i = 0;
250 while (file_exists($destination . $file)) {
251 $i++;
252 $file = $filename .'_'. $i . $fileext;
253 }
254 return $file;
255 }
256
257
258 function imagepicker_scale_image($source, $destination, $maxsize) {
259 $info = image_get_info($source);
260
261 $width = ($maxsize >= $info['width']) ? $info['width'] : $maxsize;
262 $height = ($maxsize >= $info['height']) ? $info['height'] : $maxsize;
263
264 $aspect = $info['height'] / $info['width'];
265 if ($aspect < $height / $width) {
266 $width = (int)min($width, $info['width']);
267 $height = (int)round($width * $aspect);
268 }
269 else {
270 $height = (int)min($height, $info['height']);
271 $width = (int)round($height / $aspect);
272 }
273
274 return image_toolkit_invoke('resize', array($source, $destination, $width, $height));
275 }
276

  ViewVC Help
Powered by ViewVC 1.1.2