source: extensions/GThumb/main.inc.php @ 12758

Last change on this file since 12758 was 12758, checked in by patdenice, 12 years ago

Delete cache automaticaly if height or margin has changed

File size: 5.2 KB
Line 
1<?php
2/*
3Plugin Name: GThumb+
4Version: auto
5Description: Display thumbnails as patchwork
6Plugin URI: auto
7Author: P@t
8Author URI: http://www.gauchon.com
9*/
10
11global $conf;
12
13if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
14
15define('GTHUMB_PATH' , PHPWG_PLUGINS_PATH . basename(dirname(__FILE__)) . '/');
16define('GTHUMB_CACHE_DIR', PHPWG_ROOT_PATH.PWG_LOCAL_DIR.'GThumb');
17
18$conf['GThumb'] = unserialize($conf['GThumb']);
19
20// RV Thumbnails Scroller
21if (isset($_GET['rvts']))
22{
23  $conf['GThumb']['big_thumb'] = false;
24  add_event_handler('loc_end_index_thumbnails', 'process_GThumb', 50, 2);
25}
26
27add_event_handler('loc_begin_index', 'GThumb_init', 60);
28add_event_handler('ws_add_methods', 'add_gthumb_thumbnails_method');
29add_event_handler('get_admin_plugin_menu_links', 'GThumb_admin_menu');
30
31function GThumb_init()
32{
33  global $conf, $user, $page, $template;
34
35  $template->set_prefilter('index', 'GThumb_prefilter');
36
37  add_event_handler('loc_end_index_thumbnails', 'process_GThumb', 50, 2);
38
39  $user['nb_image_page'] = $conf['GThumb']['nb_image_page'];
40  $page['nb_image_page'] = $conf['GThumb']['nb_image_page'];
41}
42
43function process_GThumb($tpl_vars, $pictures)
44{
45  global $template, $conf;
46
47  $template->set_filename( 'index_thumbnails', realpath(GTHUMB_PATH.'template/gthumb.tpl'));
48  $template->assign('GThumb', $conf['GThumb']);
49
50  include_once(PHPWG_ROOT_PATH.'admin/include/image.class.php');
51
52  foreach ($tpl_vars as $key => &$tpl_var)
53  {
54    $data = get_gthumb_data($pictures[$key]);
55
56    $tpl_var['TN_SRC'] = $data['src'];
57    $tpl_var['TN_WIDTH'] = $data['width'];
58    $tpl_var['TN_HEIGHT'] = $data['height'];
59  }
60
61  if ($conf['GThumb']['big_thumb'])
62  {
63    $ft = &$tpl_vars[0];
64
65    // Small thumb data
66    $small_thumb = array(
67      'id' => $ft['ID'],
68      'src' => $ft['TN_SRC'],
69      'width' => $ft['TN_WIDTH'],
70      'height' => $ft['TN_HEIGHT'],
71    );
72
73    if (empty($small_thumb['src']))
74    {
75      include_once(GTHUMB_PATH.'functions.inc.php');
76      $data = get_gthumb_data($pictures[0]);
77      $result = make_gthumb_image($pictures[0], $data);
78      $small_thumb['src'] = $result['destination'];
79    }
80
81    // Big thumb data
82    $data = get_gthumb_data($pictures[0], 'big');
83
84    $big_thumb = array(
85      'id' => $ft['ID'],
86      'src' => $data['src'],
87      'width' => $data['width'],
88      'height' => $data['height'],
89    );
90    if (empty($big_thumb['src']))
91    {
92      if ($conf['GThumb']['cache_big_thumb'])
93      {
94        include_once(GTHUMB_PATH.'functions.inc.php');
95        $result = make_gthumb_image($pictures[0], $data);
96        $big_thumb['src'] = $result['destination'];
97      }
98      else
99      {
100        $big_thumb['src'] = 'ws.php?method=pwg.images.getGThumbPlusThumbnail&image_id='.$ft['ID'].'&size=big&return=true';
101      }
102    }
103
104    $template->assign(
105      array(
106        'small_thumb' => $small_thumb,
107        'big_thumb' => $big_thumb,
108      )
109    );
110    $ft['TN_SRC'] = $big_thumb['src'];
111    $ft['TN_WIDTH'] = $big_thumb['width'];
112    $ft['TN_HEIGHT'] = $big_thumb['height'];
113  }
114 
115  return $tpl_vars;
116}
117
118function add_gthumb_thumbnails_method($arr)
119{
120  include_once(GTHUMB_PATH.'functions.inc.php');
121
122  $service = &$arr[0];
123  $service->addMethod(
124    'pwg.images.getGThumbPlusThumbnail',
125    'ws_images_getGThumbPlusThumbnail',
126    array(
127      'image_id' => array(),
128      'size' => array('default'=>'small'),
129      'return' => array('default'=>false),
130    ),
131    'Get thumbnail for GThumb+ plugin. Size parameter can be "small" or "big".'
132  );
133}
134
135function get_gthumb_data($picture, $size='small')
136{
137  global $conf;
138
139  if (!in_array(get_extension($picture['path']), $conf['picture_ext']))
140  {
141    $file = get_thumbnail_url($picture);
142    list($width, $height) = getimagesize($file);
143
144    return array(
145      'src' => $file,
146      'width' => $width,
147      'height' => $height,
148    );
149  }
150
151  $new_height = $size == 'small' ? $conf['GThumb']['height'] : $conf['GThumb']['height'] * 2 + $conf['GThumb']['margin'];
152  $file = GTHUMB_CACHE_DIR.'/'.$new_height.'/'.md5($picture['path'].(!empty($picture['md5sum']) ? $picture['md5sum'] : '')).'.'.$picture['tn_ext'];
153
154  if (file_exists($file))
155  {
156    list($width, $height) = getimagesize($file);
157
158    return array(
159      'src' => $file,
160      'width' => $width,
161      'height' => $height,
162    );
163  }
164
165  $width = $picture['width'];
166  $height = $picture['height'];
167  $use_high = false;
168
169  if ($height < $new_height and $picture['has_high'] == 'true')
170  {
171    $width = $picture['high_width'];
172    $height = $picture['high_height'];
173    $use_high = true;
174  }
175
176  if ($size == 'big')
177  {
178    $width = min($width, round(max($height, $new_height) * 1.15));
179  }
180
181  $result = pwg_image::get_resize_dimensions($width, $height, 5000, $new_height);
182  $result['src'] = '';
183  $result['use_high'] = $use_high;
184  $result['cache_path'] = GTHUMB_CACHE_DIR.'/'.$new_height.'/';
185  $result['size'] = $size;
186
187  return $result;
188}
189
190function GThumb_prefilter($content, $smarty)
191{
192  $pattern = '#\<div.*?id\="thumbnails".*?\>\{\$THUMBNAILS\}\</div\>#';
193  $replacement = '<ul id="thumbnails">{$THUMBNAILS}</ul>';
194
195  return preg_replace($pattern, $replacement, $content);
196}
197
198function GThumb_admin_menu($menu)
199{
200  array_push($menu,
201    array(
202      'NAME' => 'GThumb+',
203      'URL' => get_root_url().'admin.php?page=plugin-'.basename(dirname(__FILE__)),
204    )
205  );
206  return $menu;
207}
208
209?>
Note: See TracBrowser for help on using the repository browser.