root/extensions/GThumb/main.inc.php @ 12858

Revision 12858, 6.0 KB (checked in by patdenice, 17 months ago)

Allow to add personal thumbnails for GThumb display.
Don't generate thumbnail if original thumbnail have the correct size.

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'] = embellish_url(get_root_url().$result['destination']);
97      }
98      else
99      {
100        $big_thumb['src'] = get_root_url().'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  $picture_ext = array('jpg', 'jpeg', 'png', 'gif');
140
141  if (!in_array(strtolower(get_extension($picture['path'])), $picture_ext))
142  {
143    list($width, $height) = getimagesize(get_thumbnail_path($picture));
144
145    return array(
146      'src' => get_thumbnail_url($picture),
147      'width' => $width,
148      'height' => $height,
149    );
150  }
151
152  $new_height = $size == 'small' ? $conf['GThumb']['height'] : $conf['GThumb']['height'] * 2 + $conf['GThumb']['margin'];
153  $file = GTHUMB_CACHE_DIR.'/'.$new_height.'/'.md5($picture['path'].(!empty($picture['md5sum']) ? $picture['md5sum'] : '')).'.'.$picture['tn_ext'];
154
155  if (file_exists($file))
156  {
157    list($width, $height) = getimagesize($file);
158
159    return array(
160      'src' => embellish_url(get_root_url().$file),
161      'width' => $width,
162      'height' => $height,
163    );
164  }
165
166  if ( !empty( $picture['tn_ext'] ) )
167  {
168    $file = substr_replace(get_filename_wo_extension($picture['path']), '/GThumb/',strrpos($picture['path'],'/'),1).'.'.$picture['tn_ext'];
169    if (file_exists($file))
170    {
171      list($width, $height) = getimagesize($file);
172
173      $result = array(
174        'src' => embellish_url(get_root_url().$file),
175        'width' => $width,
176        'height' => $height,
177      );
178    }
179  }
180
181  $width = $picture['width'];
182  $height = $picture['height'];
183  $use_high = false;
184
185  if ($height < $new_height and $picture['has_high'] == 'true')
186  {
187    $width = $picture['high_width'];
188    $height = $picture['high_height'];
189    $use_high = true;
190  }
191
192  if ($size == 'big')
193  {
194    $width = min($width, round(max($height, $new_height) * 1.15));
195  }
196
197  $result = pwg_image::get_resize_dimensions($width, $height, 5000, $new_height);
198  $result['src'] = '';
199
200  // Test thumbnail size
201  list($width, $height) = getimagesize(get_thumbnail_path($picture));
202  if ($result['width'] == $width and $result['height'] == $height)
203  {
204    $result['src'] = get_thumbnail_url($picture);
205  }
206
207  $result['use_high'] = $use_high;
208  $result['cache_path'] = GTHUMB_CACHE_DIR.'/'.$new_height.'/';
209  $result['size'] = $size;
210
211  return $result;
212}
213
214function GThumb_prefilter($content, $smarty)
215{
216  $pattern = '#\<div.*?id\="thumbnails".*?\>\{\$THUMBNAILS\}\</div\>#';
217  $replacement = '<ul id="thumbnails">{$THUMBNAILS}</ul>';
218
219  return preg_replace($pattern, $replacement, $content);
220}
221
222function GThumb_admin_menu($menu)
223{
224  array_push($menu,
225    array(
226      'NAME' => 'GThumb+',
227      'URL' => get_root_url().'admin.php?page=plugin-'.basename(dirname(__FILE__)),
228    )
229  );
230  return $menu;
231}
232
233?>
Note: See TracBrowser for help on using the browser.