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

Last change on this file since 7935 was 4897, checked in by plg, 15 years ago

feature 1409 added: new tab "Settings".

Note: I would have like to use jQuery UI sliders for resize dimensions and
image quality BUT I can't even find the documentation of the very old version
of jQuery UI we use. I'll implement theses widget when integrating UploadForm
into trunk (with jQuery + jQuery UI updated).

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