source: trunk/include/functions_picture.inc.php @ 5698

Last change on this file since 5698 was 5196, checked in by plg, 15 years ago

increase copyright year to 2010

File size: 10.9 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2010 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24/**
25 * @param element_info array containing element information from db;
26 * at least 'id', 'path' should be present
27 */
28function get_element_path($element_info)
29{
30  $path = get_element_location($element_info);
31  if ( !url_is_remote($path) )
32  {
33    $path = PHPWG_ROOT_PATH.$path;
34  }
35  return $path;
36}
37
38/*
39 * @param element_info array containing element information from db;
40 * at least 'id', 'path' should be present
41 */
42function get_element_url($element_info)
43{
44  $url = get_element_location($element_info);
45  if ( !url_is_remote($url) )
46  {
47    $url = embellish_url(get_root_url().$url);
48  }
49  // plugins want another url ?
50  return trigger_event('get_element_url', $url, $element_info);
51}
52
53/**
54 * Returns the relative path of the element with regards to to the root
55 * of PWG (not the current page). This function is not intended to be
56 * called directly from code.
57 * @param element_info array containing element information from db;
58 * at least 'id', 'path' should be present
59 */
60function get_element_location($element_info)
61{
62  // maybe a cached watermark ?
63  return trigger_event('get_element_location',
64    $element_info['path'], $element_info);
65}
66
67
68/**
69 * Returns the PATH to the image to be displayed in the picture page. If the
70 * element is not a picture, then the representative image or the default
71 * mime image. The path can be used in the php script, but not sent to the
72 * browser.
73 * @param element_info array containing element information from db;
74 * at least 'id', 'path', 'representative_ext' should be present
75 */
76function get_image_path($element_info)
77{
78  global $conf;
79  $ext = get_extension($element_info['path']);
80  if (in_array($ext, $conf['picture_ext']))
81  {
82    if (isset($element_info['element_path']) )
83    {
84      return $element_info['element_path'];
85    }
86    return get_element_path($element_info);
87  }
88
89  $path = get_image_location($element_info);
90  if ( !url_is_remote($path) )
91  {
92    $path = PHPWG_ROOT_PATH.$path;
93  }
94  return $path;
95}
96
97/**
98 * Returns the URL of the image to be displayed in the picture page. If the
99 * element is not a picture, then the representative image or the default
100 * mime image. The URL can't be used in the php script, but can be sent to the
101 * browser.
102 * @param element_info array containing element information from db;
103 * at least 'id', 'path', 'representative_ext' should be present
104 */
105function get_image_url($element_info)
106{
107  global $conf;
108  $ext = get_extension($element_info['path']);
109  if (in_array($ext, $conf['picture_ext']))
110  {
111    if (isset($element_info['element_url']) )
112    {
113      return $element_info['element_url'];
114    }
115    return get_element_url($element_info);
116  }
117
118  $url = get_image_location($element_info);
119  if ( !url_is_remote($url) )
120  {
121    $url = embellish_url(get_root_url().$url);
122  }
123  return $url;
124}
125
126/**
127 * Returns the relative path of the image (element/representative/mimetype)
128 * with regards to the root of PWG (not the current page). This function
129 * is not intended to be called directly from code.
130 * @param element_info array containing element information from db;
131 * at least 'id', 'path', 'representative_ext' should be present
132 */
133function get_image_location($element_info)
134{
135    if (isset($element_info['representative_ext'])
136          and $element_info['representative_ext'] != '')
137    {
138      $pi = pathinfo($element_info['path']);
139      $file_wo_ext = get_filename_wo_extension($pi['basename']);
140      $path =
141        $pi['dirname'].'/pwg_representative/'
142        .$file_wo_ext.'.'.$element_info['representative_ext'];
143    }
144    else
145    {
146      $ext = get_extension($element_info['path']);
147      $path = get_themeconf('mime_icon_dir');
148      $path.= strtolower($ext).'.png';
149      if ( !file_exists(PHPWG_ROOT_PATH.$path)
150          and !empty($element_info['tn_ext']) )
151      {
152        $path = get_thumbnail_location($element_info);
153      }
154    }
155
156  // plugins want another location ?
157  return trigger_event( 'get_image_location', $path, $element_info);
158}
159
160
161/*
162 * @param element_info array containing element information from db;
163 * at least 'id', 'path', 'has_high' should be present
164 */
165function get_high_path($element_info)
166{
167  $path = get_high_location($element_info);
168  if (!empty($path) and !url_is_remote($path) )
169  {
170    $path = PHPWG_ROOT_PATH.$path;
171  }
172  return $path;
173}
174
175/**
176 * @param element_info array containing element information from db;
177 * at least 'id', 'path', 'has_high' should be present
178 */
179function get_high_url($element_info)
180{
181  $url = get_high_location($element_info);
182  if (!empty($url) and !url_is_remote($url) )
183  {
184    $url = embellish_url(get_root_url().$url);
185  }
186  // plugins want another url ?
187  return trigger_event('get_high_url', $url, $element_info);
188}
189
190/**
191 * @param element_info array containing element information from db;
192 * at least 'id', 'path', 'has_high' should be present
193 */
194function get_high_location($element_info)
195{
196  $location = '';
197  if ($element_info['has_high'] == 'true')
198  {
199    $pi = pathinfo($element_info['path']);
200    $location=$pi['dirname'].'/pwg_high/'.$pi['basename'];
201  }
202  return trigger_event( 'get_high_location', $location, $element_info);
203}
204
205
206/**
207 * @param what_part string one of 't' (thumbnail), 'e' (element), 'i' (image),
208 *   'h' (high resolution image)
209 * @param element_info array containing element information from db;
210 * at least 'id', 'path' should be present
211 */
212function get_download_url($what_part, $element_info)
213{
214  $url = get_root_url().'action.php';
215  $url = add_url_params($url,
216      array(
217        'id' => $element_info['id'],
218        'part' => $what_part,
219      )
220    );
221  return trigger_event( 'get_download_url', $url, $element_info);
222}
223
224/*
225 * get slideshow default params into array
226 *
227 * @param void
228 *
229 * @return slideshow default values into array
230 */
231function get_default_slideshow_params()
232{
233  global $conf;
234
235  return array(
236    'period' => $conf['slideshow_period'],
237    'repeat' => $conf['slideshow_repeat'],
238    'play' => true,
239    );
240}
241
242/*
243 * check and correct slideshow params from array
244 *
245 * @param array of params
246 *
247 * @return slideshow corrected values into array
248 */
249function correct_slideshow_params($params = array())
250{
251  global $conf;
252
253  if ($params['period'] < $conf['slideshow_period_min'])
254  {
255    $params['period'] = $conf['slideshow_period_min'];
256  }
257  else if ($params['period'] > $conf['slideshow_period_max'])
258  {
259    $params['period'] = $conf['slideshow_period_max'];
260  }
261
262  return $params;
263}
264
265/*
266 * Decode slideshow string params into array
267 *
268 * @param string params like ""
269 *
270 * @return slideshow values into array
271 */
272function decode_slideshow_params($encode_params = null)
273{
274  global $conf;
275
276  $result = get_default_slideshow_params();
277
278  if (is_numeric($encode_params))
279  {
280    $result['period'] = $encode_params;
281  }
282  else
283  {
284    $matches = array();
285    if (preg_match_all('/([a-z]+)-(\d+)/', $encode_params, $matches))
286    {
287      $matchcount = count($matches[1]);
288      for ($i = 0; $i < $matchcount; $i++)
289      {
290        $result[$matches[1][$i]] = $matches[2][$i];
291      }
292    }
293
294    if (preg_match_all('/([a-z]+)-(true|false)/', $encode_params, $matches))
295    {
296      $matchcount = count($matches[1]);
297      for ($i = 0; $i < $matchcount; $i++)
298      {
299        $result[$matches[1][$i]] = get_boolean($matches[2][$i]);
300      }
301    }
302  }
303
304  return correct_slideshow_params($result);
305}
306
307/*
308 * Encode slideshow array params into array
309 *
310 * @param array params
311 *
312 * @return slideshow values into string
313 */
314function encode_slideshow_params($decode_params = array())
315{
316  global $conf;
317
318  $params = array_diff_assoc(correct_slideshow_params($decode_params), get_default_slideshow_params());
319  $result = '';
320
321  foreach ($params as $name => $value)
322  {
323    // boolean_to_string return $value, if it's not a bool
324    $result .= '+'.$name.'-'.boolean_to_string($value);
325  }
326
327  return $result;
328}
329
330// The get_picture_size function return an array containing :
331//      - $picture_size[0] : final width
332//      - $picture_size[1] : final height
333// The final dimensions are calculated thanks to the original dimensions and
334// the maximum dimensions given in parameters.  get_picture_size respects
335// the width/height ratio
336function get_picture_size( $original_width, $original_height,
337                           $max_width, $max_height )
338{
339  $width = $original_width;
340  $height = $original_height;
341  $is_original_size = true;
342
343  if ( $max_width != "" )
344  {
345    if ( $original_width > $max_width )
346    {
347      $width = $max_width;
348      $height = floor( ( $width * $original_height ) / $original_width );
349    }
350  }
351  if ( $max_height != "" )
352  {
353    if ( $original_height > $max_height )
354    {
355      $height = $max_height;
356      $width = floor( ( $height * $original_width ) / $original_height );
357      $is_original_size = false;
358    }
359  }
360  if ( is_numeric( $max_width ) and is_numeric( $max_height )
361       and $max_width != 0 and $max_height != 0 )
362  {
363    $ratioWidth = $original_width / $max_width;
364    $ratioHeight = $original_height / $max_height;
365    if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) )
366    {
367      if ( $ratioWidth < $ratioHeight )
368      {
369        $width = floor( $original_width / $ratioHeight );
370        $height = $max_height;
371      }
372      else
373      {
374        $width = $max_width;
375        $height = floor( $original_height / $ratioWidth );
376      }
377      $is_original_size = false;
378    }
379  }
380  $picture_size = array();
381  $picture_size[0] = $width;
382  $picture_size[1] = $height;
383  return $picture_size;
384}
385
386?>
Note: See TracBrowser for help on using the repository browser.