source: trunk/admin/include/functions_upload.inc.php @ 6052

Last change on this file since 6052 was 6052, checked in by plg, 14 years ago

bug 1639 fixed: the upload form now correctly uses the $confupload_dir
parameter (web API already use it).

By default, the $confupload_dir is no longer dependent to PHPWG_ROOT_PATH
because it becomes a real mess when admin/include/uploadify.php (called
directly, not from an include) tries to perform an upload.

Improvement: make clearer how $confupload_dir can be set (relative to the
Piwigo installation directory + HTTP reachable)

File size: 6.6 KB
Line 
1<?php
2// TODO
3// * check md5sum (already exists?)
4
5include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
6include_once(PHPWG_ROOT_PATH.'include/ws_functions.inc.php');
7include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
8
9// Here is the plan
10//
11// 1) move uploaded file to upload/2010/01/22/20100122003814-449ada00.jpg
12//
13// 2) if taller than max_height or wider than max_width, move to pwg_high
14//    + web sized creation
15//
16// 3) thumbnail creation from web sized
17//
18// 4) register in database
19
20// add default event handler for image and thumbnail resize
21add_event_handler('upload_image_resize', 'pwg_image_resize', EVENT_HANDLER_PRIORITY_NEUTRAL, 6);
22add_event_handler('upload_thumbnail_resize', 'pwg_image_resize', EVENT_HANDLER_PRIORITY_NEUTRAL, 6);
23
24function add_uploaded_file($source_filepath, $original_filename=null, $categories=null, $level=null)
25{
26  global $conf;
27
28  // current date
29  list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
30  list($year, $month, $day) = preg_split('/[^\d]/', $dbnow, 4);
31 
32  // upload directory hierarchy
33  $upload_dir = sprintf(
34    PHPWG_ROOT_PATH.$conf['upload_dir'].'/%s/%s/%s',
35    $year,
36    $month,
37    $day
38    );
39
40  // compute file path
41  $md5sum = md5_file($source_filepath);
42  $date_string = preg_replace('/[^\d]/', '', $dbnow);
43  $random_string = substr($md5sum, 0, 8);
44  $filename_wo_ext = $date_string.'-'.$random_string;
45  $file_path = $upload_dir.'/'.$filename_wo_ext.'.jpg';
46
47  prepare_directory($upload_dir);
48  if (is_uploaded_file($source_filepath))
49  {
50    move_uploaded_file($source_filepath, $file_path);
51  }
52  else
53  {
54    copy($source_filepath, $file_path);
55  }
56
57  if ($conf['upload_form_websize_resize']
58      and need_resize($file_path, $conf['upload_form_websize_maxwidth'], $conf['upload_form_websize_maxheight']))
59  {
60    $high_path = file_path_for_type($file_path, 'high');
61    $high_dir = dirname($high_path);
62    prepare_directory($high_dir);
63   
64    rename($file_path, $high_path);
65    $high_infos = pwg_image_infos($high_path);
66   
67    trigger_event('upload_image_resize',
68      false,
69      $high_path,
70      $file_path,
71      $conf['upload_form_websize_maxwidth'],
72      $conf['upload_form_websize_maxheight'],
73      $conf['upload_form_websize_quality']
74      );
75  }
76
77  $file_infos = pwg_image_infos($file_path);
78 
79  $thumb_path = file_path_for_type($file_path, 'thumb');
80  $thumb_dir = dirname($thumb_path);
81  prepare_directory($thumb_dir);
82 
83  trigger_event('upload_thumbnail_resize',
84    false,
85    $file_path,
86    $thumb_path,
87    $conf['upload_form_thumb_maxwidth'],
88    $conf['upload_form_thumb_maxheight'],
89    $conf['upload_form_thumb_quality']
90    );
91 
92  $thumb_infos = pwg_image_infos($thumb_path);
93
94  // database registration
95  $insert = array(
96    'file' => isset($original_filename) ? $original_filename : basename($file_path),
97    'date_available' => $dbnow,
98    'tn_ext' => 'jpg',
99    'path' => preg_replace('#^'.preg_quote(PHPWG_ROOT_PATH).'#', '', $file_path),
100    'filesize' => $file_infos['filesize'],
101    'width' => $file_infos['width'],
102    'height' => $file_infos['height'],
103    'md5sum' => $md5sum,
104    );
105
106  if (isset($high_infos))
107  {
108    $insert['has_high'] = 'true';
109    $insert['high_filesize'] = $high_infos['filesize'];
110  }
111
112  if (isset($level))
113  {
114    $insert['level'] = $level;
115  }
116 
117  mass_inserts(
118    IMAGES_TABLE,
119    array_keys($insert),
120    array($insert)
121    );
122 
123  $image_id = mysql_insert_id();
124
125  if (isset($categories) and count($categories) > 0)
126  {
127    associate_images_to_categories(
128      array($image_id),
129      $categories
130      );
131  }
132 
133  // update metadata from the uploaded file (exif/iptc)
134  update_metadata(array($image_id=>$file_path));
135 
136  invalidate_user_cache();
137
138  return $image_id;
139}
140
141function prepare_directory($directory)
142{
143  if (!is_dir($directory)) {
144    umask(0000);
145    $recursive = true;
146    if (!@mkdir($directory, 0777, $recursive))
147    {
148      die('[prepare_directory] cannot create directory "'.$directory.'"');
149    }
150  }
151
152  if (!is_writable($directory))
153  {
154    // last chance to make the directory writable
155    @chmod($directory, 0777);
156
157    if (!is_writable($directory))
158    {
159      die('[prepare_directory] directory "'.$directory.'" has no write access');
160    }
161  }
162
163  secure_directory($directory);
164}
165
166function need_resize($image_filepath, $max_width, $max_height)
167{
168  list($width, $height) = getimagesize($image_filepath);
169 
170  if ($width > $max_width or $height > $max_height)
171  {
172    return true;
173  }
174
175  return false;
176}
177
178function pwg_image_resize($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality)
179{
180  if ($result !== false)
181  {
182    //someone hooked us - so we skip
183    return $result;
184  }
185
186  if (!function_exists('gd_info'))
187  {
188    return false;
189  }
190
191  // extension of the picture filename
192  $extension = strtolower(get_extension($source_filepath));
193
194  $source_image = null;
195  if (in_array($extension, array('jpg', 'jpeg')))
196  {
197    $source_image = @imagecreatefromjpeg($source_filepath);
198  }
199  else if ($extension == 'png')
200  {
201    $source_image = @imagecreatefrompng($source_filepath);
202  }
203  else
204  {
205    die('unsupported file extension');
206  }
207 
208  // width/height
209  $source_width  = imagesx($source_image); 
210  $source_height = imagesy($source_image);
211 
212  $ratio_width  = $source_width / $max_width;
213  $ratio_height = $source_height / $max_height;
214 
215  // maximal size exceeded ?
216  if ($ratio_width > 1 or $ratio_height > 1)
217  {
218    if ($ratio_width < $ratio_height)
219    { 
220      $destination_width = ceil($source_width / $ratio_height);
221      $destination_height = $max_height; 
222    }
223    else
224    { 
225      $destination_width = $max_width; 
226      $destination_height = ceil($source_height / $ratio_width);
227    }
228  }
229  else
230  {
231    // the image doesn't need any resize! We just copy it to the destination
232    copy($source_filepath, $destination_filepath);
233    return true;
234  }
235 
236  $destination_image = imagecreatetruecolor($destination_width, $destination_height);
237 
238  imagecopyresampled(
239    $destination_image,
240    $source_image,
241    0,
242    0,
243    0,
244    0,
245    $destination_width,
246    $destination_height,
247    $source_width,
248    $source_height
249    );
250 
251  imagejpeg($destination_image, $destination_filepath, $quality);
252  // freeing memory ressources
253  imagedestroy($source_image);
254  imagedestroy($destination_image);
255
256  // everything should be OK if we are here!
257  return true;
258}
259
260function pwg_image_infos($path)
261{
262  list($width, $height) = getimagesize($path);
263  $filesize = floor(filesize($path)/1024);
264 
265  return array(
266    'width'  => $width,
267    'height' => $height,
268    'filesize' => $filesize,
269    );
270}
271
272function is_valid_image_extension($extension)
273{
274  return in_array(strtolower($extension), array('jpg', 'jpeg', 'png'));
275}
276?>
Note: See TracBrowser for help on using the repository browser.