source: extensions/GThumb/functions.inc.php @ 12732

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

Double thumbnail is generated without ajax

File size: 3.8 KB
RevLine 
[12678]1<?php
2
3function ws_images_getGThumbPlusThumbnail($params, &$service)
4{
5  global $conf;
6
7  if (empty($params['image_id']))
8  {
9    return new PwgError(403, "image_id is required");
10  }
11
12  include_once(PHPWG_ROOT_PATH.'admin/include/image.class.php');
13
14  $forbidden = get_sql_condition_FandF(
15    array(
16      'forbidden_categories' => 'ic.category_id',
17      'visible_categories' => 'ic.category_id',
18      'visible_images' => 'i.id'
19    ),
20    'AND'
21  );
22
23  $query = 'SELECT i.*
24FROM '.IMAGES_TABLE.' AS i
25  INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON i.id = ic.image_id
26  INNER JOIN '.CATEGORIES_TABLE.' AS c ON ic.category_id = c.id
27WHERE i.id = '.$params['image_id'].'
28'.$forbidden.'
29;';
30
31  $picture = pwg_db_fetch_assoc(pwg_query($query));
32
33  if (empty($picture))
34  {
35    return new PwgError(404, "image not found");
36  }
37
38  $data = get_gthumb_data($picture, $params['size']);
39  $result = array();
40 
41  if (empty($data['src']))
42  {
[12732]43    $result = make_gthumb_image($picture, $data);
44    $file = $result['destination'];
[12678]45  }
46  else
47  {
48    $file = $data['src'];
49    $result['width'] = $data['width'];
50    $result['height'] = $data['height'];
51  }
52
53  if ($params['return'])
54  {
55    switch (get_extension($file))
56    {
57      case 'jpg':
58      case 'jpeg':
59        header('Content-type: image/jpeg'); break;
60      case 'gif':
61        header('Content-type: image/gif'); break;
62      case 'png':
63        header('Content-type: image/png'); break;
64      default:
65        header('Content-type: unknow'); break;
66    } 
67   
68    header('Last-Modified: '.date('r', filemtime($file)));
69    readfile($file);
70    if (!$conf['GThumb']['cache_big_thumb'])
71    {
72      @unlink($file);
73    }
74    exit();
75  }
76
77  return array(
78    'id' => $picture['id'],
79    'src' => $file,
80    'width' => $result['width'],
81    'height' => $result['height'],
82  );
83}
84
[12732]85function make_gthumb_image($picture, $data)
86{
87  global $conf;
88
89  $cache_dir = GTHUMB_CACHE_DIR.'/';
90  if ($data['size'] == 'small' or $conf['GThumb']['cache_big_thumb'])
91  {
92    $cache_dir = $data['cache_path'];
93  }
94  $file = $cache_dir.md5($picture['path'].(!empty($picture['md5sum']) ? $picture['md5sum'] : '')).'.'.$picture['tn_ext'];
95
96  if (!is_dir($cache_dir))
97  {
98    mkgetdir($cache_dir, MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR);
99    if (!is_writable($cache_dir))
100    {
101      die("Give write access (chmod 777) to $cache_dir directory at the root of your Piwigo installation");
102    }
103  }
104
105  $filepath = $picture['path'];
106  if ($data['use_high'])
107  {
108    include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
109    $filepath = file_path_for_type($filepath, 'high');
110  }
111  $img = new pwg_image($filepath);
112  $result = $img->pwg_resize($file, $data['width'], $data['height'], $conf['upload_form_thumb_quality'], false, true, ($data['size'] == 'big'), false);
113  $img->destroy();
114
115  return $result;
116}
117
[12678]118function gtdeltree($path)
119{
120  if (is_dir($path))
121  {
122    $fh = opendir($path);
123    while ($file = readdir($fh))
124    {
125      if ($file != '.' and $file != '..')
126      {
127        $pathfile = $path . '/' . $file;
128        if (is_dir($pathfile))
129        {
130          gtdeltree($pathfile);
131        }
132        else
133        {
134          @unlink($pathfile);
135        }
136      }
137    }
138    closedir($fh);
139    return @rmdir($path);
140  }
141}
142
[12691]143function gtdirsize($path, &$size=0, &$nb_files=0)
144{
145  if (is_dir($path))
146  {
147    $fh = opendir($path);
148    while ($file = readdir($fh))
149    {
150      if ($file != '.' and $file != '..' and $file != 'index.htm')
151      {
152        $pathfile = $path . '/' . $file;
153        if (is_dir($pathfile))
154        {
155          $data = gtdirsize($pathfile, $size, $nb_files);
156        }
157        else
158        {
159          $size += filesize($pathfile);
160          $nb_files++;
161        }
162      }
163    }
164    closedir($fh);
165  }
166  return array(
167    'size' => $size,
168    'nb_files' => $nb_files,
169  );
170}
171
[12678]172?>
Note: See TracBrowser for help on using the repository browser.