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

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

Increase min ratio for big thumbnails smaller than required height

File size: 5.5 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
80    if (empty($small_thumb['src']))
81    {
82      include_once(GTHUMB_PATH.'functions.inc.php');
83      $data = get_gthumb_data($pictures[0]);
84      $result = make_gthumb_image($pictures[0], $data);
85      $small_thumb['src'] = $result['destination'];
86    }
87
88    // Big thumb data
89    $data = get_gthumb_data($pictures[0], 'big');
90
91    $big_thumb = array(
92      'id' => $ft['ID'],
93      'src' => $data['src'],
94      'width' => $data['width'],
95      'height' => $data['height'],
96    );
97    if (empty($big_thumb['src']))
98    {
99      if ($conf['GThumb']['cache_big_thumb'])
100      {
101        include_once(GTHUMB_PATH.'functions.inc.php');
102        $result = make_gthumb_image($pictures[0], $data);
103        $big_thumb['src'] = $result['destination'];
104      }
105      else
106      {
107        $big_thumb['src'] = 'ws.php?method=pwg.images.getGThumbPlusThumbnail&image_id='.$ft['ID'].'&size=big&return=true';
108      }
109    }
110
111    $template->assign(
112      array(
113        'small_thumb' => $small_thumb,
114        'big_thumb' => $big_thumb,
115      )
116    );
117    $ft['TN_SRC'] = $big_thumb['src'];
118    $ft['TN_WIDTH'] = $big_thumb['width'];
119    $ft['TN_HEIGHT'] = $big_thumb['height'];
120  }
121 
122  return $tpl_vars;
123}
124
125function add_gthumb_thumbnails_method($arr)
126{
127  include_once(GTHUMB_PATH.'functions.inc.php');
128
129  $service = &$arr[0];
130  $service->addMethod(
131    'pwg.images.getGThumbPlusThumbnail',
132    'ws_images_getGThumbPlusThumbnail',
133    array(
134      'image_id' => array(),
135      'size' => array('default'=>'small'),
136      'return' => array('default'=>false),
137    ),
138    'Get thumbnail for GThumb+ plugin. Size parameter can be "small" or "big".'
139  );
140}
141
142function get_gthumb_data($picture, $size='small')
143{
144  global $conf;
145
146  if (!in_array(get_extension($picture['path']), $conf['picture_ext']))
147  {
148    $file = get_thumbnail_url($picture);
149    list($width, $height) = getimagesize($file);
150
151    return array(
152      'src' => $file,
153      'width' => $width,
154      'height' => $height,
155    );
156  }
157
158  $new_height = $size == 'small' ? $conf['GThumb']['height'] : $conf['GThumb']['height'] * 2 + $conf['GThumb']['margin'];
159  $file = GTHUMB_CACHE_DIR.'/'.$new_height.'/'.md5($picture['path'].(!empty($picture['md5sum']) ? $picture['md5sum'] : '')).'.'.$picture['tn_ext'];
160
161  if (file_exists($file))
162  {
163    list($width, $height) = getimagesize($file);
164
165    return array(
166      'src' => $file,
167      'width' => $width,
168      'height' => $height,
169    );
170  }
171
172  $width = $picture['width'];
173  $height = $picture['height'];
174  $use_high = false;
175
176  if ($height < $new_height and $picture['has_high'] == 'true')
177  {
178    $width = $picture['high_width'];
179    $height = $picture['high_height'];
180    $use_high = true;
181  }
182
183  if ($size == 'big')
184  {
185    $width = min($width, round(max($height, $new_height) * 1.15));
186  }
187
188  $result = pwg_image::get_resize_dimensions($width, $height, 5000, $new_height);
189  $result['src'] = '';
190  $result['use_high'] = $use_high;
191  $result['cache_path'] = GTHUMB_CACHE_DIR.'/'.$new_height.'/';
192  $result['size'] = $size;
193
194  return $result;
195}
196
197function GThumb_prefilter($content, $smarty)
198{
199  $pattern = '#\<div.*?id\="thumbnails".*?\>\{\$THUMBNAILS\}\</div\>#';
200  $replacement = '<ul id="thumbnails">{$THUMBNAILS}</ul>';
201
202  return preg_replace($pattern, $replacement, $content);
203}
204
205function GThumb_admin_menu($menu)
206{
207  array_push($menu,
208    array(
209      'NAME' => 'GThumb+',
210      'URL' => get_root_url().'admin.php?page=plugin-'.basename(dirname(__FILE__)),
211    )
212  );
213  return $menu;
214}
215
216?>
Note: See TracBrowser for help on using the repository browser.