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

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

Bug corrected with PWG Stuffs.

File size: 5.1 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  if (is_dir(GTHUMB_CACHE_DIR) and !is_dir(GTHUMB_CACHE_DIR.'/'.$conf['GThumb']['height']))
43  {
44    // We clean cache dir because configuration has changed
45    include_once(GTHUMB_PATH.'functions.inc.php');
46    gtdeltree(GTHUMB_CACHE_DIR);
47  }
48}
49
50function process_GThumb($tpl_vars, $pictures)
51{
52  global $template, $conf;
53
54  $template->set_filename( 'index_thumbnails', realpath(GTHUMB_PATH.'template/gthumb.tpl'));
55  $template->assign('GThumb', $conf['GThumb']);
56
57  include_once(PHPWG_ROOT_PATH.'admin/include/image.class.php');
58
59  foreach ($tpl_vars as $key => &$tpl_var)
60  {
61    $data = get_gthumb_data($pictures[$key]);
62
63    $tpl_var['TN_SRC'] = $data['src'];
64    $tpl_var['TN_WIDTH'] = $data['width'];
65    $tpl_var['TN_HEIGHT'] = $data['height'];
66  }
67
68  if ($conf['GThumb']['big_thumb'])
69  {
70    $ft = &$tpl_vars[0];
71
72    // Small thumb data
73    $small_thumb = array(
74      'id' => $ft['ID'],
75      'src' => $ft['TN_SRC'],
76      'width' => $ft['TN_WIDTH'],
77      'height' => $ft['TN_HEIGHT'],
78    );
79    if (empty($small_thumb['src']))
80    {
81      $small_thumb['src'] = 'ws.php?method=pwg.images.getGThumbPlusThumbnail&image_id='.$small_thumb['id'].'&return=true';
82    }
83
84    // Big thumb data
85    $data = get_gthumb_data($pictures[0], 'big');
86
87    $big_thumb = array(
88      'id' => $ft['ID'],
89      'src' => $data['src'],
90      'width' => $data['width'],
91      'height' => $data['height'],
92    );
93    if (empty($data['src']))
94    {
95      $big_thumb['src'] = 'ws.php?method=pwg.images.getGThumbPlusThumbnail&image_id='.$ft['ID'].'&size=big&return=true';
96    }
97
98    $template->assign(
99      array(
100        'small_thumb' => $small_thumb,
101        'big_thumb' => $big_thumb,
102      )
103    );
104    $ft['TN_SRC'] = $big_thumb['src'];
105    $ft['TN_WIDTH'] = $big_thumb['width'];
106    $ft['TN_HEIGHT'] = $big_thumb['height'];
107  }
108 
109  return $tpl_vars;
110}
111
112function add_gthumb_thumbnails_method($arr)
113{
114  include_once(GTHUMB_PATH.'functions.inc.php');
115
116  $service = &$arr[0];
117  $service->addMethod(
118    'pwg.images.getGThumbPlusThumbnail',
119    'ws_images_getGThumbPlusThumbnail',
120    array(
121      'image_id' => array(),
122      'size' => array('default'=>'small'),
123      'return' => array('default'=>false),
124    ),
125    'Get thumbnail for GThumb+ plugin. Size parameter can be "small" or "big".'
126  );
127}
128
129function get_gthumb_data($picture, $size='small')
130{
131  global $conf;
132
133  if (!in_array(get_extension($picture['file']), $conf['picture_ext']))
134  {
135    $file = get_thumbnail_url($picture);
136    list($width, $height) = getimagesize($file);
137
138    return array(
139      'src' => $file,
140      'width' => $width,
141      'height' => $height,
142    );
143  }
144
145  $new_height = $size == 'small' ? $conf['GThumb']['height'] : $conf['GThumb']['height'] * 2 + $conf['GThumb']['margin'];
146  $file = GTHUMB_CACHE_DIR.'/'.$new_height.'/'.md5($picture['path'].(!empty($picture['md5sum']) ? $picture['md5sum'] : '')).'.'.$picture['tn_ext'];
147
148  if (file_exists($file))
149  {
150    list($width, $height) = getimagesize($file);
151
152    return array(
153      'src' => $file,
154      'width' => $width,
155      'height' => $height,
156    );
157  }
158
159  $width = $picture['width'];
160  $height = $picture['height'];
161  $use_high = false;
162
163  if ($height < $new_height and $picture['has_high'] == 'true')
164  {
165    $width = $picture['high_width'];
166    $height = $picture['high_height'];
167    $use_high = true;
168  }
169
170  if ($size == 'big')
171  {
172    $width = min($width, round($height * 1.15));
173  }
174
175  $result = pwg_image::get_resize_dimensions($width, $height, 5000, $new_height);
176  $result['src'] = '';
177  $result['use_high'] = $use_high;
178  $result['cache_path'] = GTHUMB_CACHE_DIR.'/'.$new_height.'/';
179
180  return $result;
181}
182
183function GThumb_prefilter($content, $smarty)
184{
185  $pattern = '#\<div.*?id\="thumbnails".*?\>\{\$THUMBNAILS\}\</div\>#';
186  $replacement = '<ul id="thumbnails">{$THUMBNAILS}</ul>';
187
188  return preg_replace($pattern, $replacement, $content);
189}
190
191function GThumb_admin_menu($menu)
192{
193  array_push($menu,
194    array(
195      'NAME' => 'GThumb+',
196      'URL' => get_root_url().'admin.php?page=plugin-'.basename(dirname(__FILE__)),
197    )
198  );
199  return $menu;
200}
201
202?>
Note: See TracBrowser for help on using the repository browser.