source: trunk/i.php @ 13668

Last change on this file since 13668 was 13651, checked in by rvelices, 12 years ago

multisize: remove 2 php warnings, delete custom derivatives (batch man + maintenance), watermark applied to custom derivatives

File size: 15.1 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2012 Piwigo Team                  http://piwigo.org |
6// +-----------------------------------------------------------------------+
7// | This program is free software; you can redistribute it and/or modify  |
8// | it under the terms of the GNU General Public License as published by  |
9// | the Free Software Foundation                                          |
10// |                                                                       |
11// | This program is distributed in the hope that it will be useful, but   |
12// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
13// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
14// | General Public License for more details.                              |
15// |                                                                       |
16// | You should have received a copy of the GNU General Public License     |
17// | along with this program; if not, write to the Free Software           |
18// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
19// | USA.                                                                  |
20// +-----------------------------------------------------------------------+
21
22define('PHPWG_ROOT_PATH','./');
23
24// fast bootstrap - no db connection
25include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
26@include(PHPWG_ROOT_PATH. 'local/config/config.inc.php');
27
28defined('PWG_LOCAL_DIR') or define('PWG_LOCAL_DIR', 'local/');
29defined('PWG_DERIVATIVE_DIR') or define('PWG_DERIVATIVE_DIR', $conf['data_location'].'i/');
30
31function trigger_action() {}
32function get_extension( $filename )
33{
34  return substr( strrchr( $filename, '.' ), 1, strlen ( $filename ) );
35}
36
37function mkgetdir($dir)
38{
39  if ( !is_dir($dir) )
40  {
41    global $conf;
42    if (substr(PHP_OS, 0, 3) == 'WIN')
43    {
44      $dir = str_replace('/', DIRECTORY_SEPARATOR, $dir);
45    }
46    $umask = umask(0);
47    $mkd = @mkdir($dir, $conf['chmod_value'], true);
48    umask($umask);
49    if ($mkd==false)
50    {
51      return false;
52    }
53
54    $file = $dir.'/index.htm';
55    file_exists($file) or @file_put_contents( $file, 'Not allowed!' );
56  }
57  if ( !is_writable($dir) )
58  {
59    return false;
60  }
61  return true;
62}
63
64// end fast bootstrap
65
66function ilog()
67{
68  global $conf;
69  if (!$conf['enable_i_log']) return;
70
71  $line = date("c");
72  foreach( func_get_args() as $arg)
73  {
74    $line .= ' ';
75    if (is_array($arg))
76    {
77      $line .= implode(' ', $arg);
78    }
79    else
80    {
81      $line .= $arg;
82    }
83  }
84        $file=PHPWG_ROOT_PATH.$conf['data_location'].'tmp/i.log';
85  if (false == file_put_contents($file, $line."\n", FILE_APPEND))
86        {
87                mkgetdir(dirname($file));
88        }
89}
90
91function ierror($msg, $code)
92{
93  if ($code==301 || $code==302)
94  {
95    if (ob_get_length () !== FALSE)
96    {
97      ob_clean();
98    }
99    // default url is on html format
100    $url = html_entity_decode($msg);
101    header('Request-URI: '.$url);
102    header('Content-Location: '.$url);
103    header('Location: '.$url);
104    exit;
105  }
106  if ($code>=400)
107  {
108    $protocol = $_SERVER["SERVER_PROTOCOL"];
109    if ( ('HTTP/1.1' != $protocol) && ('HTTP/1.0' != $protocol) )
110      $protocol = 'HTTP/1.0';
111
112    header( "$protocol $code $msg", true, $code );
113  }
114  //todo improve
115  echo $msg;
116  exit;
117}
118
119function time_step( &$step )
120{
121  $tmp = $step;
122  $step = microtime(true);
123  return intval(1000*($step - $tmp));
124}
125
126function url_to_size($s)
127{
128  $pos = strpos($s, 'x');
129  if ($pos===false)
130  {
131    return array((int)$s, (int)$s);
132  }
133  return array((int)substr($s,0,$pos), (int)substr($s,$pos+1));
134}
135
136function parse_custom_params($tokens)
137{
138  if (count($tokens)<1)
139    ierror('Empty array while parsing Sizing', 400);
140
141  $crop = 0;
142  $min_size = null;
143
144  $token = array_shift($tokens);
145  if ($token[0]=='s')
146  {
147    $size = url_to_size( substr($token,1) );
148  }
149  elseif ($token[0]=='e')
150  {
151    $crop = 1;
152    $size = $min_size = url_to_size( substr($token,1) );
153  }
154  else
155  {
156    $size = url_to_size( $token );
157    if (count($tokens)<2)
158      ierror('Sizing arr', 400);
159
160    $token = array_shift($tokens);
161    $crop = char_to_fraction($token);
162
163    $token = array_shift($tokens);
164    $min_size = url_to_size( $token );
165  }
166  return new DerivativeParams( new SizingParams($size, $crop, $min_size) );
167}
168
169function parse_request()
170{
171  global $conf, $page;
172
173  if ( $conf['question_mark_in_urls']==false and
174       isset($_SERVER["PATH_INFO"]) and !empty($_SERVER["PATH_INFO"]) )
175  {
176    $req = $_SERVER["PATH_INFO"];
177    $req = str_replace('//', '/', $req);
178    $path_count = count( explode('/', $req) );
179    $page['root_path'] = PHPWG_ROOT_PATH.str_repeat('../', $path_count-1);
180  }
181  else
182  {
183    $req = $_SERVER["QUERY_STRING"];
184    if ($pos=strpos($req, '&'))
185    {
186      $req = substr($req, 0, $pos);
187    }
188    /*foreach (array_keys($_GET) as $keynum => $key)
189    {
190      $req = $key;
191      break;
192    }*/
193    $page['root_path'] = PHPWG_ROOT_PATH;
194  }
195
196  $req = ltrim($req, '/');
197
198  foreach (preg_split('#/+#', $req) as $token)
199  {
200    preg_match($conf['sync_chars_regex'], $token) or ierror('Invalid chars in request', 400);
201  }
202 
203  $page['derivative_path'] = PHPWG_ROOT_PATH.PWG_DERIVATIVE_DIR.$req;
204
205  $pos = strrpos($req, '.');
206  $pos!== false || ierror('Missing .', 400);
207  $ext = substr($req, $pos);
208  $page['derivative_ext'] = $ext;
209  $req = substr($req, 0, $pos);
210
211  $pos = strrpos($req, '-');
212  $pos!== false || ierror('Missing -', 400);
213  $deriv = substr($req, $pos+1);
214  $req = substr($req, 0, $pos);
215
216  $deriv = explode('_', $deriv);
217  foreach (ImageStdParams::get_defined_type_map() as $type => $params)
218  {
219    if ( derivative_to_url($type) == $deriv[0])
220    {
221      $page['derivative_type'] = $type;
222      $page['derivative_params'] = $params;
223      break;
224    }
225  }
226
227  if (!isset($page['derivative_type']))
228  {
229    if (derivative_to_url(IMG_CUSTOM) == $deriv[0])
230    {
231      $page['derivative_type'] = IMG_CUSTOM;
232    }
233    else
234    {
235      ierror('Unknown parsing type', 400);
236    }
237  }
238  array_shift($deriv);
239
240  if ($page['derivative_type'] == IMG_CUSTOM)
241  {
242    $params = $page['derivative_params'] = parse_custom_params($deriv);
243    ImageStdParams::apply_global($params);
244
245    if ($params->sizing->ideal_size[0] < 20 or $params->sizing->ideal_size[1] < 20)
246    {
247      ierror('Invalid size', 400);
248    }
249    if ($params->sizing->max_crop < 0 or $params->sizing->max_crop > 1)
250    {
251      ierror('Invalid crop', 400);
252    }
253    $greatest = ImageStdParams::get_by_type(IMG_XXLARGE);
254
255    $key = array();
256    $params->add_url_tokens($key);
257    $key = implode('_', $key);
258    if (!isset(ImageStdParams::$custom[$key]))
259    {
260      ierror('Size not allowed', 403);
261    }
262  }
263
264  if (is_file(PHPWG_ROOT_PATH.$req.$ext))
265  {
266    $req = './'.$req; // will be used to match #iamges.path
267  }
268  elseif (is_file(PHPWG_ROOT_PATH.'../'.$req.$ext))
269  {
270    $req = '../'.$req;
271  }
272
273  $page['src_location'] = $req.$ext;
274  $page['src_path'] = PHPWG_ROOT_PATH.$page['src_location'];
275  $page['src_url'] = $page['root_path'].$page['src_location'];
276}
277
278function try_switch_source(DerivativeParams $params, $original_mtime)
279{
280  global $page;
281  $candidates = array();
282  foreach(ImageStdParams::get_defined_type_map() as $candidate)
283  {
284    if ($candidate->type == $params->type)
285      continue;
286    if ($candidate->use_watermark != $params->use_watermark)
287      continue;
288    if ($candidate->max_width() < $params->max_width() || $candidate->max_height() < $params->max_height())
289      continue;
290    if ($params->sizing->max_crop==0)
291    {
292      if ($candidate->sizing->max_crop!=0)
293        continue;
294    }
295    else
296    {
297      if ($candidate->sizing->max_crop!=0)
298        continue; // this could be optimized
299      if (!isset($page['original_size']))
300        continue;
301      $candidate_size = $candidate->compute_final_size($page['original_size']);
302      if ($candidate_size[0] < $params->sizing->min_size[0] || $candidate_size[1] < $params->sizing->min_size[1] )
303        continue;
304    }
305    $candidates[] = $candidate;
306  }
307
308  foreach( array_reverse($candidates) as $candidate)
309  {
310    $candidate_path = $page['derivative_path'];
311    $candidate_path = str_replace( '-'.derivative_to_url($params->type), '-'.derivative_to_url($candidate->type), $candidate_path);
312    $candidate_mtime = @filemtime($candidate_path);
313    if ($candidate_mtime === false
314      || $candidate_mtime < $original_mtime
315      || $candidate_mtime < $candidate->last_mod_time)
316      continue;
317    $params->use_watermark = false;
318    $params->sharpen = min(1, $params->sharpen);
319    $page['src_path'] = $candidate_path;
320    $page['src_url'] = $page['root_path'] . substr($candidate_path, strlen(PHPWG_ROOT_PATH));
321  }
322}
323
324function send_derivative($expires)
325{
326  global $page;
327
328  if (isset($_GET['ajaxload']) and $_GET['ajaxload'] == 'true')
329  {
330    include_once(PHPWG_ROOT_PATH.'include/functions_cookie.inc.php');
331    include_once(PHPWG_ROOT_PATH.'include/functions_url.inc.php');
332
333    $response = new json_response();
334    $response->url = embellish_url(get_absolute_root_url().$page['derivative_path']);
335    echo json_encode($response);
336    return;
337  }
338  $fp = fopen($page['derivative_path'], 'rb');
339
340  $fstat = fstat($fp);
341  header('Last-Modified: '.gmdate('D, d M Y H:i:s', $fstat['mtime']).' GMT');
342  if ($expires!==false)
343  {
344    header('Expires: '.gmdate('D, d M Y H:i:s', $expires).' GMT');
345  }
346  header('Content-length: '.$fstat['size']);
347  header('Connection: close');
348
349  $ctype="application/octet-stream";
350  switch (strtolower($page['derivative_ext']))
351  {
352    case ".jpe": case ".jpeg": case ".jpg": $ctype="image/jpeg"; break;
353    case ".png": $ctype="image/png"; break;
354    case ".gif": $ctype="image/gif"; break;
355  }
356  header("Content-Type: $ctype");
357
358  fpassthru($fp);
359  fclose($fp);
360}
361
362class json_response
363{
364  var $url;
365}
366
367$page=array();
368$begin = $step = microtime(true);
369$timing=array();
370foreach( explode(',','load,rotate,crop,scale,sharpen,watermark,save,send') as $k )
371{
372  $timing[$k] = '';
373}
374
375include_once( PHPWG_ROOT_PATH .'/include/derivative_params.inc.php');
376include_once( PHPWG_ROOT_PATH .'/include/derivative_std_params.inc.php');
377
378ImageStdParams::load_from_file();
379
380
381parse_request();
382//var_export($page);
383
384$params = $page['derivative_params'];
385
386$src_mtime = @filemtime($page['src_path']);
387if ($src_mtime === false)
388{
389  ierror('Source not found', 404);
390}
391
392$need_generate = false;
393$derivative_mtime = @filemtime($page['derivative_path']);
394if ($derivative_mtime === false or
395    $derivative_mtime < $src_mtime or
396    $derivative_mtime < $params->last_mod_time)
397{
398  $need_generate = true;
399}
400
401$expires=false;
402$now = time();
403if ( isset($_GET['b']) )
404{
405  $expires = $now + 100;
406  header("Cache-control: no-store, max-age=100");
407}
408elseif ( $now > (max($src_mtime, $params->last_mod_time) + 24*3600) )
409{// somehow arbitrary - if derivative params or src didn't change for the last 24 hours, we send an expire header for several days
410  $expires = $now + 10*24*3600;
411}
412
413if (!$need_generate)
414{
415  if ( isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] )
416    and strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $derivative_mtime)
417  {// send the last mod time of the file back
418    header('Last-Modified: '.gmdate('D, d M Y H:i:s', $derivative_mtime).' GMT', true, 304);
419    header('Expires: '.gmdate('D, d M Y H:i:s', time()+10*24*3600).' GMT', true, 304);
420    exit;
421  }
422  send_derivative($expires);
423  exit;
424}
425
426$page['coi'] = null;
427if (strpos($page['src_location'], '/pwg_representative/')===false
428    && strpos($page['src_location'], 'themes/')===false
429    && strpos($page['src_location'], 'plugins/')===false)
430{
431  @include(PHPWG_ROOT_PATH.PWG_LOCAL_DIR .'config/database.inc.php');
432  include(PHPWG_ROOT_PATH .'include/dblayer/functions_'.$conf['dblayer'].'.inc.php');
433  try
434  {
435    $pwg_db_link = pwg_db_connect($conf['db_host'], $conf['db_user'],
436                                  $conf['db_password'], $conf['db_base']);
437    $query = 'SELECT coi, width, height FROM '.$prefixeTable.'images WHERE path=\''.$page['src_location'].'\'';
438    if ( ($row=pwg_db_fetch_assoc(pwg_query($query))) )
439    {
440      if (isset($row['width']))
441      {
442        $page['original_size'] = array($row['width'],$row['height']);
443      }
444      $page['coi'] = $row['coi'];
445    }
446    mysql_close($pwg_db_link);
447    if (!$row)
448    {
449      ierror('Db file path not found', 404);
450    }
451  }
452  catch (Exception $e)
453  {
454    ilog("db error", $e->getMessage());
455  }
456}
457
458try_switch_source($params, $src_mtime);
459
460if (!mkgetdir(dirname($page['derivative_path'])))
461{
462  ierror("dir create error", 500);
463}
464
465include_once(PHPWG_ROOT_PATH . 'admin/include/image.class.php');
466
467ignore_user_abort(true);
468set_time_limit(0);
469
470$image = new pwg_image($page['src_path']);
471$timing['load'] = time_step($step);
472
473$changes = 0;
474
475// todo rotate
476
477// Crop & scale
478$o_size = $d_size = array($image->get_width(),$image->get_height());
479$params->sizing->compute($o_size , $page['coi'], $crop_rect, $scaled_size );
480if ($crop_rect)
481{
482  $changes++;
483  $image->crop( $crop_rect->width(), $crop_rect->height(), $crop_rect->l, $crop_rect->t);
484  $timing['crop'] = time_step($step);
485}
486
487if ($scaled_size)
488{
489  $changes++;
490  $image->resize( $scaled_size[0], $scaled_size[1] );
491  $d_size = $scaled_size;
492  $timing['scale'] = time_step($step);
493}
494
495if ($params->sharpen)
496{
497  $changes += $image->sharpen( $params->sharpen );
498  $timing['sharpen'] = time_step($step);
499}
500
501if ($params->use_watermark)
502{
503  $wm = ImageStdParams::get_watermark();
504  $wm_image = new pwg_image(PHPWG_ROOT_PATH.$wm->file);
505  $wm_size = array($wm_image->get_width(),$wm_image->get_height());
506  if ($d_size[0]<$wm_size[0] or $d_size[1]<$wm_size[1])
507  {
508    $wm_scaling_params = SizingParams::classic($d_size[0], $d_size[1]);
509    $wm_scaling_params->compute($wm_size, null, $tmp, $wm_scaled_size);
510    $wm_size = $wm_scaled_size;
511    $wm_image->resize( $wm_scaled_size[0], $wm_scaled_size[1] );
512  }
513  $x = round( ($wm->xpos/100)*($d_size[0]-$wm_size[0]) );
514  $y = round( ($wm->ypos/100)*($d_size[1]-$wm_size[1]) );
515  if ($image->compose($wm_image, $x, $y, $wm->opacity))
516  {
517    $changes++;
518    if ($wm->xrepeat)
519    {
520      // todo
521      $pad = $wm_size[0] + max(30, round($wm_size[0]/4));
522      for($i=-$wm->xrepeat; $i<=$wm->xrepeat; $i++)
523      {
524        if (!$i) continue;
525        $x2 = $x + $i * $pad;
526        if ($x2>=0 && $x2+$wm_size[0]<$d_size[0])
527          if (!$image->compose($wm_image, $x2, $y, $wm->opacity))
528            break;
529      }
530    }
531  }
532  $wm_image->destroy();
533  $timing['watermark'] = time_step($step);
534}
535
536// no change required - redirect to source
537if (!$changes)
538{
539  header("X-i: No change");
540  ierror( $page['src_url'], 301);
541}
542
543if ($d_size[0]*$d_size[1] < 256000)
544{// strip metadata for small images
545  $image->strip();
546}
547$image->set_compression_quality( $params->quality );
548$image->write( $page['derivative_path'] );
549$image->destroy();
550$timing['save'] = time_step($step);
551
552send_derivative($expires);
553$timing['send'] = time_step($step);
554
555ilog('perf',
556  basename($page['src_path']), $o_size, $o_size[0]*$o_size[1],
557  basename($page['derivative_path']), $d_size, $d_size[0]*$d_size[1],
558  function_exists('memory_get_peak_usage') ? round( memory_get_peak_usage()/(1024*1024), 1) : '',
559  time_step($begin),
560  '|', $timing);
561?>
Note: See TracBrowser for help on using the repository browser.