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

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

Add GThumb+ plugin

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