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

Last change on this file since 12855 was 12855, checked in by rvelices, 12 years ago

feature 2548 multisize - improved picture.php display (original...) + code cleanup

  • Property svn:eol-style set to LF
File size: 6.7 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
[9366]26/*
27 * Returns the name of a photo according to its name and its filename.
28 * @param name string
29 * @param filename string
30 * @return string
31 */
32function get_image_name($name, $filename)
33{
34  if (!empty($name))
35  {
36    return $name;
37  }
38  else
39  {
40    return get_name_from_file($filename);
41  }
42}
43
[1612]44/*
45 * @param element_info array containing element information from db;
46 * at least 'id', 'path', 'has_high' should be present
47 */
48function get_high_path($element_info)
49{
50  $path = get_high_location($element_info);
51  if (!empty($path) and !url_is_remote($path) )
52  {
53    $path = PHPWG_ROOT_PATH.$path;
54  }
55  return $path;
56}
57
58/**
59 * @param element_info array containing element information from db;
60 * at least 'id', 'path', 'has_high' should be present
61 */
62function get_high_url($element_info)
63{
64  $url = get_high_location($element_info);
65  if (!empty($url) and !url_is_remote($url) )
66  {
[2026]67    $url = embellish_url(get_root_url().$url);
[1612]68  }
69  // plugins want another url ?
70  return trigger_event('get_high_url', $url, $element_info);
71}
72
73/**
74 * @param element_info array containing element information from db;
75 * at least 'id', 'path', 'has_high' should be present
76 */
77function get_high_location($element_info)
78{
79  $location = '';
80  if ($element_info['has_high'] == 'true')
81  {
82    $pi = pathinfo($element_info['path']);
83    $location=$pi['dirname'].'/pwg_high/'.$pi['basename'];
84  }
85  return trigger_event( 'get_high_location', $location, $element_info);
86}
87
88
89
[2218]90/*
91 * get slideshow default params into array
92 *
93 * @param void
94 *
95 * @return slideshow default values into array
96 */
97function get_default_slideshow_params()
98{
99  global $conf;
100
101  return array(
102    'period' => $conf['slideshow_period'],
103    'repeat' => $conf['slideshow_repeat'],
104    'play' => true,
105    );
106}
107
108/*
109 * check and correct slideshow params from array
110 *
111 * @param array of params
112 *
113 * @return slideshow corrected values into array
114 */
115function correct_slideshow_params($params = array())
116{
117  global $conf;
118
119  if ($params['period'] < $conf['slideshow_period_min'])
120  {
121    $params['period'] = $conf['slideshow_period_min'];
122  }
123  else if ($params['period'] > $conf['slideshow_period_max'])
124  {
125    $params['period'] = $conf['slideshow_period_max'];
126  }
127
128  return $params;
129}
130
131/*
132 * Decode slideshow string params into array
133 *
134 * @param string params like ""
135 *
136 * @return slideshow values into array
137 */
138function decode_slideshow_params($encode_params = null)
139{
140  global $conf;
141
142  $result = get_default_slideshow_params();
143
144  if (is_numeric($encode_params))
145  {
146    $result['period'] = $encode_params;
147  }
148  else
149  {
150    $matches = array();
151    if (preg_match_all('/([a-z]+)-(\d+)/', $encode_params, $matches))
152    {
153      $matchcount = count($matches[1]);
154      for ($i = 0; $i < $matchcount; $i++)
155      {
156        $result[$matches[1][$i]] = $matches[2][$i];
157      }
158    }
159
160    if (preg_match_all('/([a-z]+)-(true|false)/', $encode_params, $matches))
161    {
162      $matchcount = count($matches[1]);
163      for ($i = 0; $i < $matchcount; $i++)
164      {
165        $result[$matches[1][$i]] = get_boolean($matches[2][$i]);
166      }
167    }
168  }
169
170  return correct_slideshow_params($result);
171}
172
173/*
174 * Encode slideshow array params into array
175 *
176 * @param array params
177 *
178 * @return slideshow values into string
179 */
180function encode_slideshow_params($decode_params = array())
181{
182  global $conf;
183
184  $params = array_diff_assoc(correct_slideshow_params($decode_params), get_default_slideshow_params());
185  $result = '';
186
187  foreach ($params as $name => $value)
188  {
189    // boolean_to_string return $value, if it's not a bool
190    $result .= '+'.$name.'-'.boolean_to_string($value);
191  }
192
193  return $result;
194}
195
[2479]196// The get_picture_size function return an array containing :
197//      - $picture_size[0] : final width
198//      - $picture_size[1] : final height
199// The final dimensions are calculated thanks to the original dimensions and
200// the maximum dimensions given in parameters.  get_picture_size respects
201// the width/height ratio
202function get_picture_size( $original_width, $original_height,
203                           $max_width, $max_height )
204{
205  $width = $original_width;
206  $height = $original_height;
207  $is_original_size = true;
208
209  if ( $max_width != "" )
210  {
211    if ( $original_width > $max_width )
212    {
213      $width = $max_width;
214      $height = floor( ( $width * $original_height ) / $original_width );
215    }
216  }
217  if ( $max_height != "" )
218  {
219    if ( $original_height > $max_height )
220    {
221      $height = $max_height;
222      $width = floor( ( $height * $original_width ) / $original_height );
223      $is_original_size = false;
224    }
225  }
226  if ( is_numeric( $max_width ) and is_numeric( $max_height )
227       and $max_width != 0 and $max_height != 0 )
228  {
229    $ratioWidth = $original_width / $max_width;
230    $ratioHeight = $original_height / $max_height;
231    if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) )
232    {
233      if ( $ratioWidth < $ratioHeight )
234      {
235        $width = floor( $original_width / $ratioHeight );
236        $height = $max_height;
237      }
238      else
239      {
240        $width = $max_width;
241        $height = floor( $original_height / $ratioWidth );
242      }
243      $is_original_size = false;
244    }
245  }
246  $picture_size = array();
247  $picture_size[0] = $width;
248  $picture_size[1] = $height;
249  return $picture_size;
250}
251
[1903]252?>
Note: See TracBrowser for help on using the repository browser.