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