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

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

Bug corrected with special images (like 3d pictures).
Work with question mark in urls = false.

File size: 5.4 KB
RevLine 
[12678]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
[12712]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
[12678]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
[12712]37  add_event_handler('loc_end_index_thumbnails', 'process_GThumb', 50, 2);
38
[12678]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    );
[12732]72
[12678]73    if (empty($small_thumb['src']))
74    {
[12732]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'];
[12678]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    );
[12732]90    if (empty($big_thumb['src']))
[12678]91    {
[12732]92      if ($conf['GThumb']['cache_big_thumb'])
93      {
94        include_once(GTHUMB_PATH.'functions.inc.php');
95        $result = make_gthumb_image($pictures[0], $data);
[12857]96        $big_thumb['src'] = embellish_url(get_root_url().$result['destination']);
[12732]97      }
98      else
99      {
[12857]100        $big_thumb['src'] = get_root_url().'ws.php?method=pwg.images.getGThumbPlusThumbnail&image_id='.$ft['ID'].'&size=big&return=true';
[12732]101      }
[12678]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
[12857]139  $picture_ext = array('jpg', 'jpeg', 'png', 'gif');
140
141  if (!in_array(strtolower(get_extension($picture['path'])), $picture_ext))
[12678]142  {
[12857]143    list($width, $height) = getimagesize(get_thumbnail_path($picture));
[12678]144
145    return array(
[12857]146      'src' => get_thumbnail_url($picture),
[12678]147      'width' => $width,
148      'height' => $height,
149    );
150  }
151
152  $new_height = $size == 'small' ? $conf['GThumb']['height'] : $conf['GThumb']['height'] * 2 + $conf['GThumb']['margin'];
[12705]153  $file = GTHUMB_CACHE_DIR.'/'.$new_height.'/'.md5($picture['path'].(!empty($picture['md5sum']) ? $picture['md5sum'] : '')).'.'.$picture['tn_ext'];
[12678]154
155  if (file_exists($file))
156  {
157    list($width, $height) = getimagesize($file);
158
159    return array(
[12857]160      'src' => embellish_url(get_root_url().$file),
[12678]161      'width' => $width,
162      'height' => $height,
163    );
164  }
165
166  $width = $picture['width'];
167  $height = $picture['height'];
168  $use_high = false;
169
170  if ($height < $new_height and $picture['has_high'] == 'true')
171  {
172    $width = $picture['high_width'];
173    $height = $picture['high_height'];
174    $use_high = true;
175  }
176
177  if ($size == 'big')
178  {
[12751]179    $width = min($width, round(max($height, $new_height) * 1.15));
[12678]180  }
181
182  $result = pwg_image::get_resize_dimensions($width, $height, 5000, $new_height);
183  $result['src'] = '';
184  $result['use_high'] = $use_high;
185  $result['cache_path'] = GTHUMB_CACHE_DIR.'/'.$new_height.'/';
[12732]186  $result['size'] = $size;
[12678]187
188  return $result;
189}
190
191function GThumb_prefilter($content, $smarty)
192{
193  $pattern = '#\<div.*?id\="thumbnails".*?\>\{\$THUMBNAILS\}\</div\>#';
194  $replacement = '<ul id="thumbnails">{$THUMBNAILS}</ul>';
195
196  return preg_replace($pattern, $replacement, $content);
197}
198
199function GThumb_admin_menu($menu)
200{
201  array_push($menu,
202    array(
203      'NAME' => 'GThumb+',
204      'URL' => get_root_url().'admin.php?page=plugin-'.basename(dirname(__FILE__)),
205    )
206  );
207  return $menu;
208}
209
210?>
Note: See TracBrowser for help on using the repository browser.