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

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

initial import

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