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

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

give a URL for Automatic size option link

File size: 3.6 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          $autosize = $type;
72        }
73      }
74    }
75  }
76
77  global $page, $template;
78
79  $template->set_prefilter('picture', 'asize_picture_prefilter');
80
81  $is_automatic_size = true;
82  if (@$_COOKIE['is_automatic_size'] == 'no')
83  {
84    $is_automatic_size = false;
85  }
86  $template->assign(
87    array(
88      'is_automatic_size' => $is_automatic_size,
89      'ASIZE_URL' => duplicate_picture_url(),
90      )
91    );
92 
93  if (isset($autosize))
94  {
95    if ($is_automatic_size)
96    {
97      $selected_derivative = $element_info['derivatives'][$autosize];
98    }
99    $template->assign('autosize', $autosize);
100  }
101
102  if ($show_original)
103  {
104    $template->assign( 'U_ORIGINAL', $element_info['element_url'] );
105  }
106
107  $template->append('current', array(
108      'selected_derivative' => $selected_derivative,
109      'unique_derivatives' => $unique_derivatives,
110    ), true);
111
112
113  $template->set_filenames(
114    array('default_content'=>'picture_content.tpl')
115    );
116
117  $template->assign( array(
118      'ALT_IMG' => $element_info['file'],
119      'COOKIE_PATH' => cookie_path(),
120      )
121    );
122
123  $template->set_filename('asize_picture_js', realpath(ASIZE_PATH.'picture_js.tpl'));
124  $template->parse('asize_picture_js');
125 
126  return $template->parse( 'default_content', true);
127}
128
129function asize_picture_prefilter($content, &$smarty)
130{
131  $pattern = '#\{foreach from=\$current\.unique_derivatives#';
132  $replacement = '
133<span class="switchCheck" id="aSizeChecked"{if !$is_automatic_size} style="visibility:hidden"{/if}>&#x2714; </span> <a id="aSize" href="{$ASIZE_URL}" data-checked="{if $is_automatic_size}yes{else}no{/if}">{\'Automatic\'|@translate}</a>
134<br><br>
135{foreach from=$current.unique_derivatives';
136  $content = preg_replace($pattern, $replacement, $content);
137
138  return $content;
139}
140
141?>
Note: See TracBrowser for help on using the repository browser.