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

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

merge r6616 from branch 2.1 to trunk

bug 1701 fixed: support for PNG file in browser direct upload.

If the picture is a PNG file, then the "web size" picture is also a PNG file
but the thumbnail is always a JPEG file.

File size: 7.1 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) = pwg_db_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.'.';
46
47  list($width, $height, $type) = getimagesize($source_filepath);
48  if (IMAGETYPE_PNG == $type)
49  {
50    $file_path.= 'png';
51  }
52  else
53  {
54    $file_path.= 'jpg';
55  }
56
57  prepare_directory($upload_dir);
58  if (is_uploaded_file($source_filepath))
59  {
60    move_uploaded_file($source_filepath, $file_path);
61  }
62  else
63  {
64    copy($source_filepath, $file_path);
65  }
66
67  if ($conf['upload_form_websize_resize']
68      and need_resize($file_path, $conf['upload_form_websize_maxwidth'], $conf['upload_form_websize_maxheight']))
69  {
70    $high_path = file_path_for_type($file_path, 'high');
71    $high_dir = dirname($high_path);
72    prepare_directory($high_dir);
73   
74    rename($file_path, $high_path);
75    $high_infos = pwg_image_infos($high_path);
76   
77    trigger_event('upload_image_resize',
78      false,
79      $high_path,
80      $file_path,
81      $conf['upload_form_websize_maxwidth'],
82      $conf['upload_form_websize_maxheight'],
83      $conf['upload_form_websize_quality']
84      );
85  }
86
87  $file_infos = pwg_image_infos($file_path);
88 
89  $thumb_path = file_path_for_type($file_path, 'thumb');
90  $thumb_dir = dirname($thumb_path);
91  prepare_directory($thumb_dir);
92 
93  trigger_event('upload_thumbnail_resize',
94    false,
95    $file_path,
96    $thumb_path,
97    $conf['upload_form_thumb_maxwidth'],
98    $conf['upload_form_thumb_maxheight'],
99    $conf['upload_form_thumb_quality']
100    );
101 
102  $thumb_infos = pwg_image_infos($thumb_path);
103
104  // database registration
105  $insert = array(
106    'file' => isset($original_filename) ? $original_filename : basename($file_path),
107    'date_available' => $dbnow,
108    'tn_ext' => 'jpg',
109    'path' => preg_replace('#^'.preg_quote(PHPWG_ROOT_PATH).'#', '', $file_path),
110    'filesize' => $file_infos['filesize'],
111    'width' => $file_infos['width'],
112    'height' => $file_infos['height'],
113    'md5sum' => $md5sum,
114    );
115
116  if (isset($high_infos))
117  {
118    $insert['has_high'] = 'true';
119    $insert['high_filesize'] = $high_infos['filesize'];
120  }
121
122  if (isset($level))
123  {
124    $insert['level'] = $level;
125  }
126 
127  mass_inserts(
128    IMAGES_TABLE,
129    array_keys($insert),
130    array($insert)
131    );
132 
133  $image_id = pwg_db_insert_id();
134
135  if (isset($categories) and count($categories) > 0)
136  {
137    associate_images_to_categories(
138      array($image_id),
139      $categories
140      );
141  }
142 
143  // update metadata from the uploaded file (exif/iptc)
144  update_metadata(array($image_id=>$file_path));
145 
146  invalidate_user_cache();
147
148  return $image_id;
149}
150
151function prepare_directory($directory)
152{
153  if (!is_dir($directory)) {
154    if (substr(PHP_OS, 0, 3) == 'WIN')
155    {
156      $directory = str_replace('/', DIRECTORY_SEPARATOR, $directory);
157    }
158    umask(0000);
159    $recursive = true;
160    if (!@mkdir($directory, 0777, $recursive))
161    {
162      die('[prepare_directory] cannot create directory "'.$directory.'"');
163    }
164  }
165
166  if (!is_writable($directory))
167  {
168    // last chance to make the directory writable
169    @chmod($directory, 0777);
170
171    if (!is_writable($directory))
172    {
173      die('[prepare_directory] directory "'.$directory.'" has no write access');
174    }
175  }
176
177  secure_directory($directory);
178}
179
180function need_resize($image_filepath, $max_width, $max_height)
181{
182  list($width, $height) = getimagesize($image_filepath);
183 
184  if ($width > $max_width or $height > $max_height)
185  {
186    return true;
187  }
188
189  return false;
190}
191
192function pwg_image_resize($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality)
193{
194  if ($result !== false)
195  {
196    //someone hooked us - so we skip
197    return $result;
198  }
199
200  if (!function_exists('gd_info'))
201  {
202    return false;
203  }
204
205  // extension of the picture filename
206  $extension = strtolower(get_extension($source_filepath));
207
208  $source_image = null;
209  if (in_array($extension, array('jpg', 'jpeg')))
210  {
211    $source_image = imagecreatefromjpeg($source_filepath);
212  }
213  else if ($extension == 'png')
214  {
215    $source_image = imagecreatefrompng($source_filepath);
216  }
217  else
218  {
219    die('unsupported file extension');
220  }
221 
222  // width/height
223  $source_width  = imagesx($source_image); 
224  $source_height = imagesy($source_image);
225 
226  $ratio_width  = $source_width / $max_width;
227  $ratio_height = $source_height / $max_height;
228 
229  // maximal size exceeded ?
230  if ($ratio_width > 1 or $ratio_height > 1)
231  {
232    if ($ratio_width < $ratio_height)
233    { 
234      $destination_width = ceil($source_width / $ratio_height);
235      $destination_height = $max_height; 
236    }
237    else
238    { 
239      $destination_width = $max_width; 
240      $destination_height = ceil($source_height / $ratio_width);
241    }
242  }
243  else
244  {
245    // the image doesn't need any resize! We just copy it to the destination
246    copy($source_filepath, $destination_filepath);
247    return true;
248  }
249 
250  $destination_image = imagecreatetruecolor($destination_width, $destination_height);
251 
252  imagecopyresampled(
253    $destination_image,
254    $source_image,
255    0,
256    0,
257    0,
258    0,
259    $destination_width,
260    $destination_height,
261    $source_width,
262    $source_height
263    );
264 
265  $extension = strtolower(get_extension($destination_filepath));
266  if ($extension == 'png')
267  {
268    imagepng($destination_image, $destination_filepath);
269  }
270  else
271  {
272    imagejpeg($destination_image, $destination_filepath, $quality);
273  }
274  // freeing memory ressources
275  imagedestroy($source_image);
276  imagedestroy($destination_image);
277
278  // everything should be OK if we are here!
279  return true;
280}
281
282function pwg_image_infos($path)
283{
284  list($width, $height) = getimagesize($path);
285  $filesize = floor(filesize($path)/1024);
286 
287  return array(
288    'width'  => $width,
289    'height' => $height,
290    'filesize' => $filesize,
291    );
292}
293
294function is_valid_image_extension($extension)
295{
296  return in_array(strtolower($extension), array('jpg', 'jpeg', 'png'));
297}
298?>
Note: See TracBrowser for help on using the repository browser.