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

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

Thumbnails can be generated in admin pannel.

File size: 3.8 KB
Line 
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  {
43    $result = make_gthumb_image($picture, $data);
44    $file = $result['destination'];
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    'filesize' => filesize($file),
83  );
84}
85
86function make_gthumb_image($picture, $data)
87{
88  global $conf;
89
90  $cache_dir = GTHUMB_CACHE_DIR.'/';
91  if ($data['size'] == 'small' or $conf['GThumb']['cache_big_thumb'])
92  {
93    $cache_dir = $data['cache_path'];
94  }
95  $file = $cache_dir.md5($picture['path'].(!empty($picture['md5sum']) ? $picture['md5sum'] : '')).'.'.$picture['tn_ext'];
96
97  if (!is_dir($cache_dir))
98  {
99    mkgetdir($cache_dir, MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR);
100    if (!is_writable($cache_dir))
101    {
102      die("Give write access (chmod 777) to $cache_dir directory at the root of your Piwigo installation");
103    }
104  }
105
106  $filepath = $picture['path'];
107  if ($data['use_high'])
108  {
109    include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
110    $filepath = file_path_for_type($filepath, 'high');
111  }
112  $img = new pwg_image($filepath);
113  $result = $img->pwg_resize($file, $data['width'], $data['height'], $conf['upload_form_thumb_quality'], false, true, ($data['size'] == 'big'), false);
114  $img->destroy();
115
116  return $result;
117}
118
119function gtdeltree($path)
120{
121  if (is_dir($path))
122  {
123    $fh = opendir($path);
124    while ($file = readdir($fh))
125    {
126      if ($file != '.' and $file != '..')
127      {
128        $pathfile = $path . '/' . $file;
129        if (is_dir($pathfile))
130        {
131          gtdeltree($pathfile);
132        }
133        else
134        {
135          @unlink($pathfile);
136        }
137      }
138    }
139    closedir($fh);
140    return @rmdir($path);
141  }
142}
143
144function gtdirsize($path, &$size=0, &$nb_files=0)
145{
146  if (is_dir($path))
147  {
148    $fh = opendir($path);
149    while ($file = readdir($fh))
150    {
151      if ($file != '.' and $file != '..' and $file != 'index.htm')
152      {
153        $pathfile = $path . '/' . $file;
154        if (is_dir($pathfile))
155        {
156          $data = gtdirsize($pathfile, $size, $nb_files);
157        }
158        else
159        {
160          $size += filesize($pathfile);
161          $nb_files++;
162        }
163      }
164    }
165    closedir($fh);
166  }
167  return array(
168    'size' => $size,
169    'nb_files' => $nb_files,
170  );
171}
172
173?>
Note: See TracBrowser for help on using the repository browser.