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

Revision 4875, 6.1 KB checked in by plg, 10 years ago (diff)

feature 1141 added: multiple select and progress bar with uploadify (jQuery
plugin).

Note: the HTML upload is disabled but I will provide a switch, either you use
Uploadify OR you use HTML multiple file boxes.

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$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, $original_filename=null, $categories=null, $level=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' => isset($original_filename) ? $original_filename : basename($file_path),
84    'date_available' => $dbnow,
85    'tn_ext' => 'jpg',
86    'path' => preg_replace('/^.*?upload/', './upload', $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($high_infos))
94  {
95    $insert['has_high'] = 'true';
96    $insert['high_filesize'] = $high_infos['filesize'];
97  }
98
99  if (isset($level))
100  {
101    $insert['level'] = $level;
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  return $image_id;
126}
127
128function prepare_directory($directory)
129{
130  if (!is_dir($directory)) {
131    umask(0000);
132    $recursive = true;
133    if (!@mkdir($directory, 0777, $recursive))
134    {
135      die('[prepare_directory] cannot create directory "'.$directory.'"');
136    }
137  }
138
139  if (!is_writable($directory))
140  {
141    // last chance to make the directory writable
142    @chmod($directory, 0777);
143
144    if (!is_writable($directory))
145    {
146      die('[prepare_directory] directory "'.$directory.'" has no write access');
147    }
148  }
149
150  secure_directory($directory);
151}
152
153function need_resize($image_filepath, $max_width, $max_height)
154{
155  list($width, $height) = getimagesize($image_filepath);
156 
157  if ($width > $max_width or $height > $max_height)
158  {
159    return true;
160  }
161
162  return false;
163}
164
165function pwg_image_resize($source_filepath, $destination_filepath, $max_width, $max_height)
166{
167  if (!function_exists('gd_info'))
168  {
169    return false;
170  }
171
172  // extension of the picture filename
173  $extension = strtolower(get_extension($source_filepath));
174
175  $source_image = null;
176  if (in_array($extension, array('jpg', 'jpeg')))
177  {
178    $source_image = @imagecreatefromjpeg($source_filepath);
179  }
180  else if ($extension == 'png')
181  {
182    $source_image = @imagecreatefrompng($source_filepath);
183  }
184  else
185  {
186    die('unsupported file extension');
187  }
188 
189  // width/height
190  $source_width  = imagesx($source_image); 
191  $source_height = imagesy($source_image);
192 
193  $ratio_width  = $source_width / $max_width;
194  $ratio_height = $source_height / $max_height;
195 
196  // maximal size exceeded ?
197  if ($ratio_width > 1 or $ratio_height > 1)
198  {
199    if ($ratio_width < $ratio_height)
200    { 
201      $destination_width = ceil($source_width / $ratio_height);
202      $destination_height = $max_height; 
203    }
204    else
205    { 
206      $destination_width = $max_width; 
207      $destination_height = ceil($source_height / $ratio_width);
208    }
209  }
210  else
211  {
212    // the image doesn't need any resize! We just copy it to the destination
213    copy($source_filepath, $destination_filepath);
214    return true;
215  }
216 
217  $destination_image = imagecreatetruecolor($destination_width, $destination_height);
218 
219  imagecopyresampled(
220    $destination_image,
221    $source_image,
222    0,
223    0,
224    0,
225    0,
226    $destination_width,
227    $destination_height,
228    $source_width,
229    $source_height
230    );
231 
232  imagejpeg($destination_image, $destination_filepath, 95);
233  // freeing memory ressources
234  imagedestroy($source_image);
235  imagedestroy($destination_image);
236
237  // everything should be OK if we are here!
238  return true;
239}
240
241function pwg_image_infos($path)
242{
243  list($width, $height) = getimagesize($path);
244  $filesize = floor(filesize($path)/1024);
245 
246  return array(
247    'width'  => $width,
248    'height' => $height,
249    'filesize' => $filesize,
250    );
251}
252
253function is_valid_image_extension($extension)
254{
255  return in_array(strtolower($extension), array('jpg', 'jpeg', 'png'));
256}
257?>
Note: See TracBrowser for help on using the repository browser.