source: extensions/upload_form/include/functions_upload.inc.php @ 4739

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

initial revision for the new upload form

File size: 5.8 KB
RevLine 
[4739]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$page['max_width'] = 800;
21$page['max_height'] = 800;
22
23$page['thumb_width'] = 128;
24$page['thumb_height'] = 96;
25
26$page['source_file'] = '/home/pierrick/public_html/test/resize/phpthumb/examples/statue_small.jpg';
27
28function add_uploaded_file($source_filepath, $name=null, $categories=null)
29{
30  global $page;
31
32  // current date
33  list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
34  list($year, $month, $day) = preg_split('/[^\d]/', $dbnow, 4);
35 
36  // upload directory hierarchy
37  $upload_dir = sprintf(
38    PHPWG_ROOT_PATH.'upload/%s/%s/%s',
39    $year,
40    $month,
41    $day
42    );
43
44  // compute file path
45  $md5sum = md5_file($source_filepath);
46  $date_string = preg_replace('/[^\d]/', '', $dbnow);
47  $random_string = substr($md5sum, 0, 8);
48  $filename_wo_ext = $date_string.'-'.$random_string;
49  $file_path = $upload_dir.'/'.$filename_wo_ext.'.jpg';
50
51  prepare_directory($upload_dir);
52  if (is_uploaded_file($source_filepath))
53  {
54    move_uploaded_file($source_filepath, $file_path);
55  }
56  else
57  {
58    copy($source_filepath, $file_path);
59  }
60
61  if (need_resize($file_path, $page['max_width'], $page['max_height']))
62  {
63    $high_path = file_path_for_type($file_path, 'high');
64    $high_dir = dirname($high_path);
65    prepare_directory($high_dir);
66   
67    rename($file_path, $high_path);
68    $high_infos = pwg_image_infos($high_path);
69   
70    pwg_image_resize($high_path, $file_path, $page['max_width'], $page['max_height']);
71  }
72
73  $file_infos = pwg_image_infos($file_path);
74 
75  $thumb_path = file_path_for_type($file_path, 'thumb');
76  $thumb_dir = dirname($thumb_path);
77  prepare_directory($thumb_dir);
78  pwg_image_resize($file_path, $thumb_path, $page['thumb_width'], $page['thumb_height']);
79  $thumb_infos = pwg_image_infos($thumb_path);
80
81  // database registration
82  $insert = array(
83    'file' => basename($file_path),
84    'date_available' => $dbnow,
85    'tn_ext' => 'jpg',
86    'path' => $file_path,
87    'filesize' => $file_infos['filesize'],
88    'width' => $file_infos['width'],
89    'height' => $file_infos['height'],
90    'md5sum' => $md5sum,
91    );
92
93  if (isset($name))
94  {
95    $insert['name'] = $name;
96  }
97
98  if (isset($high_infos))
99  {
100    $insert['has_high'] = 'true';
101    $insert['high_filesize'] = $high_infos['filesize'];
102  }
103 
104  mass_inserts(
105    IMAGES_TABLE,
106    array_keys($insert),
107    array($insert)
108    );
109 
110  $image_id = mysql_insert_id();
111
112  if (isset($categories) and count($categories) > 0)
113  {
114    associate_images_to_categories(
115      array($image_id),
116      $categories
117      );
118  }
119 
120  // update metadata from the uploaded file (exif/iptc)
121  update_metadata(array($image_id=>$file_path));
122 
123  invalidate_user_cache();
124}
125
126function prepare_directory($directory)
127{
128  if (!is_dir($directory)) {
129    umask(0000);
130    $recursive = true;
131    if (!@mkdir($directory, 0777, $recursive))
132    {
133      die('[prepare_directory] cannot create directory "'.$directory.'"');
134    }
135  }
136
137  if (!is_writable($directory))
138  {
139    // last chance to make the directory writable
140    @chmod($directory, 0777);
141
142    if (!is_writable($directory))
143    {
144      die('[prepare_directory] directory "'.$directory.'" has no write access');
145    }
146  }
147
148  secure_directory($directory);
149}
150
151function need_resize($image_filepath, $max_width, $max_height)
152{
153  list($width, $height) = getimagesize($image_filepath);
154 
155  if ($width > $max_width or $height > $max_height)
156  {
157    return true;
158  }
159
160  return false;
161}
162
163function pwg_image_resize($source_filepath, $destination_filepath, $max_width, $max_height)
164{
165  if (!function_exists('gd_info'))
166  {
167    return false;
168  }
169
170  // extension of the picture filename
171  $extension = strtolower(get_extension($source_filepath));
172
173  $source_image = null;
174  if (in_array($extension, array('jpg', 'jpeg')))
175  {
176    $source_image = @imagecreatefromjpeg($source_filepath);
177  }
178  else if ($extension == 'png')
179  {
180    $source_image = @imagecreatefrompng($source_filepath);
181  }
182  else
183  {
184    die('unsupported file extension');
185  }
186 
187  // width/height
188  $source_width  = imagesx($source_image); 
189  $source_height = imagesy($source_image);
190 
191  $ratio_width  = $source_width / $max_width;
192  $ratio_height = $source_height / $max_height;
193 
194  // maximal size exceeded ?
195  if ($ratio_width > 1 or $ratio_height > 1)
196  {
197    if ($ratio_width < $ratio_height)
198    { 
199      $destination_width = ceil($source_width / $ratio_height);
200      $destination_height = $max_height; 
201    }
202    else
203    { 
204      $destination_width = $max_width; 
205      $destination_height = ceil($source_height / $ratio_width);
206    }
207  }
208  else
209  {
210    // the image doesn't need any resize! We just copy it to the destination
211    copy($source_filepath, $destination_filepath);
212    return true;
213  }
214 
215  $destination_image = imagecreatetruecolor($destination_width, $destination_height);
216 
217  imagecopyresampled(
218    $destination_image,
219    $source_image,
220    0,
221    0,
222    0,
223    0,
224    $destination_width,
225    $destination_height,
226    $source_width,
227    $source_height
228    );
229 
230  imagejpeg($destination_image, $destination_filepath, 95);
231  // freeing memory ressources
232  imagedestroy($source_image);
233  imagedestroy($destination_image);
234
235  // everything should be OK if we are here!
236  return true;
237}
238
239function pwg_image_infos($path)
240{
241  list($width, $height) = getimagesize($path);
242  $filesize = floor(filesize($path)/1024);
243 
244  return array(
245    'width'  => $width,
246    'height' => $height,
247    'filesize' => $filesize,
248    );
249}
250?>
Note: See TracBrowser for help on using the repository browser.