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

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

add 2 configuration settings to let the webmaster decide of errors margins.

(margins can be negative)

bug fixed: do not hide the check sign in front of "Automatic" option in size
list when the user selects another size.

File size: 4.1 KB
RevLine 
[23022]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        if ($size[0] <= $available_size[0] and $size[1] <= $available_size[1])
69        {
[23033]70          $automatic_size = $type;
[23022]71        }
72      }
73    }
74  }
75
76  global $page, $template;
77
[23033]78  load_language('plugin.lang', ASIZE_PATH);
[23022]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  }
[23032]86  $template->assign(
87    array(
88      'is_automatic_size' => $is_automatic_size,
89      'ASIZE_URL' => duplicate_picture_url(),
90      )
91    );
[23022]92 
[23033]93  if (isset($automatic_size))
[23022]94  {
95    if ($is_automatic_size)
96    {
[23033]97      $selected_derivative = $element_info['derivatives'][$automatic_size];
[23022]98    }
[23033]99   
100    $template->assign(
101      array(
102        'automatic_size' => $automatic_size,
103        'ASIZE_TITLE' => sprintf(
104          l10n('The best adapted size for this photo and your screen is size %s'),
105          l10n($automatic_size)
106          )
107        )
108      );
[23022]109  }
110
111  if ($show_original)
112  {
113    $template->assign( 'U_ORIGINAL', $element_info['element_url'] );
114  }
115
116  $template->append('current', array(
117      'selected_derivative' => $selected_derivative,
118      'unique_derivatives' => $unique_derivatives,
119    ), true);
120
121
122  $template->set_filenames(
123    array('default_content'=>'picture_content.tpl')
124    );
125
126  $template->assign( array(
127      'ALT_IMG' => $element_info['file'],
128      'COOKIE_PATH' => cookie_path(),
[23074]129      'asize_width_margin' => isset($conf['automatic_size_width_margin']) ? $conf['automatic_size_width_margin'] : 12,
130      'asize_height_margin' => isset($conf['automatic_size_height_margin']) ? $conf['automatic_size_height_margin'] : 40,
[23022]131      )
132    );
[23023]133
134  $template->set_filename('asize_picture_js', realpath(ASIZE_PATH.'picture_js.tpl'));
135  $template->parse('asize_picture_js');
136 
[23022]137  return $template->parse( 'default_content', true);
138}
139
140function asize_picture_prefilter($content, &$smarty)
141{
142  $pattern = '#\{foreach from=\$current\.unique_derivatives#';
143  $replacement = '
[23074]144<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>
[23022]145<br><br>
146{foreach from=$current.unique_derivatives';
147  $content = preg_replace($pattern, $replacement, $content);
148
149  return $content;
150}
151
152?>
Note: See TracBrowser for help on using the repository browser.