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

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

Clean code

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