source: extensions/header_manager/include/functions.inc.php @ 16687

Last change on this file since 16687 was 16687, checked in by mistic100, 12 years ago

don't crash of banners folder doesn't exist

File size: 3.3 KB
Line 
1<?php
2if (!defined('HEADER_MANAGER_PATH')) die('Hacking attempt!');
3
4/**
5 * give a list of available banners
6 * @param: bool delete_orphans (from unachieved cropping process)
7 */
8function list_banners($delete_orphans=false)
9{ 
10  if (!file_exists(HEADER_MANAGER_DIR)) return array();
11  $dir = scandir(HEADER_MANAGER_DIR);
12  $banners = array();
13 
14  foreach ($dir as $file)
15  {
16    if ( in_array($file, array('.','..','index.php','.svn')) ) continue;
17    if ( !in_array(strtolower(get_extension($file)), array('jpg','jpeg','png','gif')) ) continue;
18    if ( strpos($file, '-thumbnail')!==false ) continue;
19
20    array_push($banners, get_banner($file));
21   
22    if ( $delete_orphans and !file_exists($banners[ count($banners)-1 ]['THUMB']) )
23    {
24      @unlink($banners[ count($banners)-1 ]['PATH']);
25      array_pop($banners);
26    }
27  }
28 
29  return $banners;
30}
31
32/**
33 * get full size and thumbnail urls and size for a banner
34 * @param: string filename
35 */
36function get_banner($file)
37{
38  if (file_exists(HEADER_MANAGER_DIR . $file))
39  {
40    return array(
41      'NAME' => $file,
42      'PATH' => HEADER_MANAGER_DIR . $file,
43      'THUMB' => HEADER_MANAGER_DIR . get_filename_wo_extension($file) . '-thumbnail.'. get_extension($file),
44      'SIZE' => getimagesize(HEADER_MANAGER_DIR . $file),
45      );
46  }
47  else
48  {
49    return false;
50  }
51}
52
53/**
54 * get properties of the jCrop window
55 * @param: array picture(width, height[, coi])
56 * @return: array crop(display_width, display_height, l, r, t, b, coi(x, y))
57 */
58function get_crop_display($picture)
59{
60  global $conf;
61 
62  // find coi
63  if (!empty($picture['coi']))
64  {
65    $picture['coi'] = array(
66      'l' => char_to_fraction($picture['coi'][0])*$picture['width'],
67      't' => char_to_fraction($picture['coi'][1])*$picture['height'],
68      'r' => char_to_fraction($picture['coi'][2])*$picture['width'],
69      'b' => char_to_fraction($picture['coi'][3])*$picture['height'],
70      );
71  }
72  else
73  {
74    $picture['coi'] = array(
75      'l' => 0,
76      't' => 0,
77      'r' => $picture['width'],
78      'b' => $picture['height'],
79      );
80  }
81  $crop['coi']['x'] = ($picture['coi']['r']+$picture['coi']['l'])/2;
82  $crop['coi']['y'] = ($picture['coi']['b']+$picture['coi']['t'])/2;
83 
84  // define default crop frame
85  if ($picture['width'] > $conf['header_manager']['width'])
86  {
87    $crop['display_width'] = $conf['header_manager']['width'];
88    $crop['display_height'] = round($picture['height']*$crop['display_width']/$picture['width']);
89   
90    $crop['coi']['x'] = round($crop['coi']['x']*$crop['display_width']/$picture['width']);
91    $crop['coi']['y'] = round($crop['coi']['y']*$crop['display_height']/$picture['height']);
92   
93    $crop['l'] = 0;
94    $crop['r'] = $conf['header_manager']['width'];
95    $crop['t'] = max(0, $crop['coi']['y']-$conf['header_manager']['height']/2);
96    $crop['b'] = min($crop['display_height'], $crop['t']+$conf['header_manager']['height']);
97  }
98  else
99  {
100    $crop['display_width'] = $picture['width'];
101    $crop['display_height'] = $picture['height'];
102   
103    $adapted_crop_height = round($conf['header_manager']['height']*$picture['width']/$conf['header_manager']['width']);
104   
105    $crop['l'] = 0;
106    $crop['r'] = $picture['width'];
107    $crop['t'] = max(0, $crop['coi']['y']-$adapted_crop_height/2);
108    $crop['b'] = min($crop['display_height'], $crop['t']+$adapted_crop_height);
109  }
110 
111  return $crop;
112}
113
114?>
Note: See TracBrowser for help on using the repository browser.