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

Last change on this file since 9366 was 9366, checked in by plg, 13 years ago

new function to get image name depending on name and filename

  • Property svn:eol-style set to LF
File size: 11.1 KB
RevLine 
[1612]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[8728]5// | Copyright(C) 2008-2011 Piwigo Team                  http://piwigo.org |
[2297]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// +-----------------------------------------------------------------------+
[1612]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  {
[2026]47    $url = embellish_url(get_root_url().$url);
[1612]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
[9366]68/*
69 * Returns the name of a photo according to its name and its filename.
70 * @param name string
71 * @param filename string
72 * @return string
73 */
74function get_image_name($name, $filename)
75{
76  if (!empty($name))
77  {
78    return $name;
79  }
80  else
81  {
82    return get_name_from_file($filename);
83  }
84}
85
86
[1612]87/**
88 * Returns the PATH to the image to be displayed in the picture page. If the
89 * element is not a picture, then the representative image or the default
90 * mime image. The path can be used in the php script, but not sent to the
91 * browser.
92 * @param element_info array containing element information from db;
93 * at least 'id', 'path', 'representative_ext' should be present
94 */
95function get_image_path($element_info)
96{
97  global $conf;
98  $ext = get_extension($element_info['path']);
99  if (in_array($ext, $conf['picture_ext']))
100  {
101    if (isset($element_info['element_path']) )
102    {
103      return $element_info['element_path'];
104    }
105    return get_element_path($element_info);
106  }
107
108  $path = get_image_location($element_info);
109  if ( !url_is_remote($path) )
110  {
111    $path = PHPWG_ROOT_PATH.$path;
112  }
113  return $path;
114}
115
116/**
117 * Returns the URL of the image to be displayed in the picture page. If the
118 * element is not a picture, then the representative image or the default
119 * mime image. The URL can't be used in the php script, but can be sent to the
120 * browser.
121 * @param element_info array containing element information from db;
122 * at least 'id', 'path', 'representative_ext' should be present
123 */
124function get_image_url($element_info)
125{
126  global $conf;
127  $ext = get_extension($element_info['path']);
128  if (in_array($ext, $conf['picture_ext']))
129  {
130    if (isset($element_info['element_url']) )
131    {
132      return $element_info['element_url'];
133    }
134    return get_element_url($element_info);
135  }
136
137  $url = get_image_location($element_info);
138  if ( !url_is_remote($url) )
139  {
[2026]140    $url = embellish_url(get_root_url().$url);
[1612]141  }
142  return $url;
143}
144
145/**
146 * Returns the relative path of the image (element/representative/mimetype)
147 * with regards to the root of PWG (not the current page). This function
148 * is not intended to be called directly from code.
149 * @param element_info array containing element information from db;
150 * at least 'id', 'path', 'representative_ext' should be present
151 */
152function get_image_location($element_info)
153{
154    if (isset($element_info['representative_ext'])
155          and $element_info['representative_ext'] != '')
156    {
157      $pi = pathinfo($element_info['path']);
158      $file_wo_ext = get_filename_wo_extension($pi['basename']);
159      $path =
160        $pi['dirname'].'/pwg_representative/'
161        .$file_wo_ext.'.'.$element_info['representative_ext'];
162    }
163    else
164    {
165      $ext = get_extension($element_info['path']);
166      $path = get_themeconf('mime_icon_dir');
167      $path.= strtolower($ext).'.png';
[2206]168      if ( !file_exists(PHPWG_ROOT_PATH.$path)
169          and !empty($element_info['tn_ext']) )
170      {
171        $path = get_thumbnail_location($element_info);
172      }
[1612]173    }
174
175  // plugins want another location ?
176  return trigger_event( 'get_image_location', $path, $element_info);
177}
178
179
180/*
181 * @param element_info array containing element information from db;
182 * at least 'id', 'path', 'has_high' should be present
183 */
184function get_high_path($element_info)
185{
186  $path = get_high_location($element_info);
187  if (!empty($path) and !url_is_remote($path) )
188  {
189    $path = PHPWG_ROOT_PATH.$path;
190  }
191  return $path;
192}
193
194/**
195 * @param element_info array containing element information from db;
196 * at least 'id', 'path', 'has_high' should be present
197 */
198function get_high_url($element_info)
199{
200  $url = get_high_location($element_info);
201  if (!empty($url) and !url_is_remote($url) )
202  {
[2026]203    $url = embellish_url(get_root_url().$url);
[1612]204  }
205  // plugins want another url ?
206  return trigger_event('get_high_url', $url, $element_info);
207}
208
209/**
210 * @param element_info array containing element information from db;
211 * at least 'id', 'path', 'has_high' should be present
212 */
213function get_high_location($element_info)
214{
215  $location = '';
216  if ($element_info['has_high'] == 'true')
217  {
218    $pi = pathinfo($element_info['path']);
219    $location=$pi['dirname'].'/pwg_high/'.$pi['basename'];
220  }
221  return trigger_event( 'get_high_location', $location, $element_info);
222}
223
224
225/**
226 * @param what_part string one of 't' (thumbnail), 'e' (element), 'i' (image),
227 *   'h' (high resolution image)
228 * @param element_info array containing element information from db;
229 * at least 'id', 'path' should be present
230 */
231function get_download_url($what_part, $element_info)
232{
233  $url = get_root_url().'action.php';
234  $url = add_url_params($url,
235      array(
236        'id' => $element_info['id'],
237        'part' => $what_part,
238      )
239    );
240  return trigger_event( 'get_download_url', $url, $element_info);
241}
242
[2218]243/*
244 * get slideshow default params into array
245 *
246 * @param void
247 *
248 * @return slideshow default values into array
249 */
250function get_default_slideshow_params()
251{
252  global $conf;
253
254  return array(
255    'period' => $conf['slideshow_period'],
256    'repeat' => $conf['slideshow_repeat'],
257    'play' => true,
258    );
259}
260
261/*
262 * check and correct slideshow params from array
263 *
264 * @param array of params
265 *
266 * @return slideshow corrected values into array
267 */
268function correct_slideshow_params($params = array())
269{
270  global $conf;
271
272  if ($params['period'] < $conf['slideshow_period_min'])
273  {
274    $params['period'] = $conf['slideshow_period_min'];
275  }
276  else if ($params['period'] > $conf['slideshow_period_max'])
277  {
278    $params['period'] = $conf['slideshow_period_max'];
279  }
280
281  return $params;
282}
283
284/*
285 * Decode slideshow string params into array
286 *
287 * @param string params like ""
288 *
289 * @return slideshow values into array
290 */
291function decode_slideshow_params($encode_params = null)
292{
293  global $conf;
294
295  $result = get_default_slideshow_params();
296
297  if (is_numeric($encode_params))
298  {
299    $result['period'] = $encode_params;
300  }
301  else
302  {
303    $matches = array();
304    if (preg_match_all('/([a-z]+)-(\d+)/', $encode_params, $matches))
305    {
306      $matchcount = count($matches[1]);
307      for ($i = 0; $i < $matchcount; $i++)
308      {
309        $result[$matches[1][$i]] = $matches[2][$i];
310      }
311    }
312
313    if (preg_match_all('/([a-z]+)-(true|false)/', $encode_params, $matches))
314    {
315      $matchcount = count($matches[1]);
316      for ($i = 0; $i < $matchcount; $i++)
317      {
318        $result[$matches[1][$i]] = get_boolean($matches[2][$i]);
319      }
320    }
321  }
322
323  return correct_slideshow_params($result);
324}
325
326/*
327 * Encode slideshow array params into array
328 *
329 * @param array params
330 *
331 * @return slideshow values into string
332 */
333function encode_slideshow_params($decode_params = array())
334{
335  global $conf;
336
337  $params = array_diff_assoc(correct_slideshow_params($decode_params), get_default_slideshow_params());
338  $result = '';
339
340  foreach ($params as $name => $value)
341  {
342    // boolean_to_string return $value, if it's not a bool
343    $result .= '+'.$name.'-'.boolean_to_string($value);
344  }
345
346  return $result;
347}
348
[2479]349// The get_picture_size function return an array containing :
350//      - $picture_size[0] : final width
351//      - $picture_size[1] : final height
352// The final dimensions are calculated thanks to the original dimensions and
353// the maximum dimensions given in parameters.  get_picture_size respects
354// the width/height ratio
355function get_picture_size( $original_width, $original_height,
356                           $max_width, $max_height )
357{
358  $width = $original_width;
359  $height = $original_height;
360  $is_original_size = true;
361
362  if ( $max_width != "" )
363  {
364    if ( $original_width > $max_width )
365    {
366      $width = $max_width;
367      $height = floor( ( $width * $original_height ) / $original_width );
368    }
369  }
370  if ( $max_height != "" )
371  {
372    if ( $original_height > $max_height )
373    {
374      $height = $max_height;
375      $width = floor( ( $height * $original_width ) / $original_height );
376      $is_original_size = false;
377    }
378  }
379  if ( is_numeric( $max_width ) and is_numeric( $max_height )
380       and $max_width != 0 and $max_height != 0 )
381  {
382    $ratioWidth = $original_width / $max_width;
383    $ratioHeight = $original_height / $max_height;
384    if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) )
385    {
386      if ( $ratioWidth < $ratioHeight )
387      {
388        $width = floor( $original_width / $ratioHeight );
389        $height = $max_height;
390      }
391      else
392      {
393        $width = $max_width;
394        $height = floor( $original_height / $ratioWidth );
395      }
396      $is_original_size = false;
397    }
398  }
399  $picture_size = array();
400  $picture_size[0] = $width;
401  $picture_size[1] = $height;
402  return $picture_size;
403}
404
[1903]405?>
Note: See TracBrowser for help on using the repository browser.