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

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

simpler code (proposed by mistic100)

File size: 4.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  $asize_conf_default_values = array(
30    'automatic_size_width_margin' => 12,
31    'automatic_size_height_margin' => 40,
32    'automatic_size_min_ratio' => 0.2,
33    'automatic_size_max_ratio' => 5,
34    );
35
36  foreach ($asize_conf_default_values as $key => $value)
37  {
38    if (!isset($conf[$key]))
39    {
40      $conf[$key] = $value;
41    }
42  }
43
44  if ( !empty($content) )
45  {// someone hooked us - so we skip;
46    return $content;
47  }
48
49  if (isset($_COOKIE['picture_deriv']))
50  {
51    if ( array_key_exists($_COOKIE['picture_deriv'], ImageStdParams::get_defined_type_map()) )
52    {
53      pwg_set_session_var('picture_deriv', $_COOKIE['picture_deriv']);
54    }
55    setcookie('picture_deriv', false, 0, cookie_path() );
56  }
57  $deriv_type = pwg_get_session_var('picture_deriv', $conf['derivative_default_size']);
58  $selected_derivative = $element_info['derivatives'][$deriv_type];
59
60  $unique_derivatives = array();
61  $show_original = isset($element_info['element_url']);
62  $added = array();
63  foreach($element_info['derivatives'] as $type => $derivative)
64  {
65    if ($type==IMG_SQUARE || $type==IMG_THUMB)
66      continue;
67    if (!array_key_exists($type, ImageStdParams::get_defined_type_map()))
68      continue;
69    $url = $derivative->get_url();
70    if (isset($added[$url]))
71      continue;
72    $added[$url] = 1;
73    $show_original &= !($derivative->same_as_source());
74    $unique_derivatives[$type]= $derivative;
75
76    if (isset($_COOKIE['available_size']))
77    {
78      $available_size = explode('x', $_COOKIE['available_size']);
79
80      $size = $derivative->get_size();
81      if ($size)
82      {
83        // if we have a very high picture (such as an infographic), we only try to match width
84        if ($size[0]/$size[1] < $conf['automatic_size_min_ratio'])
85        {
86          if ($size[0] <= $available_size[0])
87          {
88            $automatic_size = $type;
89          }
90        }
91        // if we have a very wide picture (panoramic), we only try to match height
92        elseif ($size[0]/$size[1] > $conf['automatic_size_max_ratio'])
93        {
94          if ($size[1] <= $available_size[1])
95          {
96            $automatic_size = $type;
97          }
98        }
99        else
100        {
101          if ($size[0] <= $available_size[0] and $size[1] <= $available_size[1])
102          {
103            $automatic_size = $type;
104          }
105        }
106      }
107    }
108  }
109
110  global $page, $template;
111
112  load_language('plugin.lang', ASIZE_PATH);
113  $template->set_prefilter('picture', 'asize_picture_prefilter');
114
115  $is_automatic_size = true;
116  if (@$_COOKIE['is_automatic_size'] == 'no')
117  {
118    $is_automatic_size = false;
119  }
120  $template->assign(
121    array(
122      'is_automatic_size' => $is_automatic_size,
123      'ASIZE_URL' => duplicate_picture_url(),
124      )
125    );
126 
127  if (isset($automatic_size))
128  {
129    if ($is_automatic_size)
130    {
131      $selected_derivative = $element_info['derivatives'][$automatic_size];
132    }
133   
134    $template->assign(
135      array(
136        'automatic_size' => $automatic_size,
137        'ASIZE_TITLE' => sprintf(
138          l10n('The best adapted size for this photo and your screen is size %s'),
139          l10n($automatic_size)
140          )
141        )
142      );
143  }
144
145  if ($show_original)
146  {
147    $template->assign( 'U_ORIGINAL', $element_info['element_url'] );
148  }
149
150  $template->append('current', array(
151      'selected_derivative' => $selected_derivative,
152      'unique_derivatives' => $unique_derivatives,
153    ), true);
154
155
156  $template->set_filenames(
157    array('default_content'=>'picture_content.tpl')
158    );
159
160  $template->assign( array(
161      'ALT_IMG' => $element_info['file'],
162      'COOKIE_PATH' => cookie_path(),
163      'asize_width_margin' => $conf['automatic_size_width_margin'],
164      'asize_height_margin' => $conf['automatic_size_height_margin'],
165      )
166    );
167
168  $template->set_filename('asize_picture_js', realpath(ASIZE_PATH.'picture_js.tpl'));
169  $template->parse('asize_picture_js');
170 
171  return $template->parse( 'default_content', true);
172}
173
174function asize_picture_prefilter($content, &$smarty)
175{
176  $pattern = '#\{foreach from=\$current\.unique_derivatives#';
177  $replacement = '
178<span 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>
179<br><br>
180{foreach from=$current.unique_derivatives';
181  $content = preg_replace($pattern, $replacement, $content);
182
183  return $content;
184}
185
186?>
Note: See TracBrowser for help on using the repository browser.