source: extensions/automatic_size/main.inc.php @ 23033

Last change on this file since 23033 was 23033, checked in by plg, 11 years ago

l10n

File size: 3.9 KB
Line 
1<?php
2/*
3Plugin Name: Automatic Size
4Version: auto
5Description: Automatically selects the biggest photo size
6Plugin URI: http://piwigo.org/ext/extension_view.php?eid=
7Author: plg
8Author URI: http://le-gall.net/pierrick
9*/
10
11if (!defined('PHPWG_ROOT_PATH'))
12{
13  die('Hacking attempt!');
14}
15
16define('ASIZE_PATH' , PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/');
17
18add_event_handler(
19  'render_element_content',
20  'asize_picture_content',
21  EVENT_HANDLER_PRIORITY_NEUTRAL-1,
22  2
23  );
24
25function asize_picture_content($content, $element_info)
26{
27  global $conf;
28
29  if ( !empty($content) )
30  {// someone hooked us - so we skip;
31    return $content;
32  }
33
34  if (isset($_COOKIE['picture_deriv']))
35  {
36    if ( array_key_exists($_COOKIE['picture_deriv'], ImageStdParams::get_defined_type_map()) )
37    {
38      pwg_set_session_var('picture_deriv', $_COOKIE['picture_deriv']);
39    }
40    setcookie('picture_deriv', false, 0, cookie_path() );
41  }
42  $deriv_type = pwg_get_session_var('picture_deriv', $conf['derivative_default_size']);
43  $selected_derivative = $element_info['derivatives'][$deriv_type];
44
45  $unique_derivatives = array();
46  $show_original = isset($element_info['element_url']);
47  $added = array();
48  foreach($element_info['derivatives'] as $type => $derivative)
49  {
50    if ($type==IMG_SQUARE || $type==IMG_THUMB)
51      continue;
52    if (!array_key_exists($type, ImageStdParams::get_defined_type_map()))
53      continue;
54    $url = $derivative->get_url();
55    if (isset($added[$url]))
56      continue;
57    $added[$url] = 1;
58    $show_original &= !($derivative->same_as_source());
59    $unique_derivatives[$type]= $derivative;
60
61    if (isset($_COOKIE['available_size']))
62    {
63      $available_size = explode('x', $_COOKIE['available_size']);
64
65      $size = $derivative->get_size();
66      if ($size)
67      {
68        // echo $type.' => '.$size[0].' x '.$size[1].'<br>';
69        if ($size[0] <= $available_size[0] and $size[1] <= $available_size[1])
70        {
71          $automatic_size = $type;
72        }
73      }
74    }
75  }
76
77  global $page, $template;
78
79  load_language('plugin.lang', ASIZE_PATH);
80  $template->set_prefilter('picture', 'asize_picture_prefilter');
81
82  $is_automatic_size = true;
83  if (@$_COOKIE['is_automatic_size'] == 'no')
84  {
85    $is_automatic_size = false;
86  }
87  $template->assign(
88    array(
89      'is_automatic_size' => $is_automatic_size,
90      'ASIZE_URL' => duplicate_picture_url(),
91      )
92    );
93 
94  if (isset($automatic_size))
95  {
96    if ($is_automatic_size)
97    {
98      $selected_derivative = $element_info['derivatives'][$automatic_size];
99    }
100   
101    $template->assign(
102      array(
103        'automatic_size' => $automatic_size,
104        'ASIZE_TITLE' => sprintf(
105          l10n('The best adapted size for this photo and your screen is size %s'),
106          l10n($automatic_size)
107          )
108        )
109      );
110  }
111
112  if ($show_original)
113  {
114    $template->assign( 'U_ORIGINAL', $element_info['element_url'] );
115  }
116
117  $template->append('current', array(
118      'selected_derivative' => $selected_derivative,
119      'unique_derivatives' => $unique_derivatives,
120    ), true);
121
122
123  $template->set_filenames(
124    array('default_content'=>'picture_content.tpl')
125    );
126
127  $template->assign( array(
128      'ALT_IMG' => $element_info['file'],
129      'COOKIE_PATH' => cookie_path(),
130      )
131    );
132
133  $template->set_filename('asize_picture_js', realpath(ASIZE_PATH.'picture_js.tpl'));
134  $template->parse('asize_picture_js');
135 
136  return $template->parse( 'default_content', true);
137}
138
139function asize_picture_prefilter($content, &$smarty)
140{
141  $pattern = '#\{foreach from=\$current\.unique_derivatives#';
142  $replacement = '
143<span class="switchCheck" id="aSizeChecked"{if !$is_automatic_size} style="visibility:hidden"{/if}>&#x2714; </span> <a id="aSize" href="{$ASIZE_URL}" title="{$ASIZE_TITLE}" data-checked="{if $is_automatic_size}yes{else}no{/if}">{\'Automatic\'|@translate}</a>
144<br><br>
145{foreach from=$current.unique_derivatives';
146  $content = preg_replace($pattern, $replacement, $content);
147
148  return $content;
149}
150
151?>
Note: See TracBrowser for help on using the repository browser.