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

Last change on this file since 12920 was 12920, checked in by rvelices, 13 years ago

feature 2548 multisize - code cleanup + better usage in category_cats + i.php logs memory usage peak

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