-
Notifications
You must be signed in to change notification settings - Fork 3
/
functions.php
158 lines (134 loc) · 3.93 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
require 'definitions.php';
/**
* returns array of friendly name => filename
* of fonts present in the system
*
* @return array
*/
function getFonts()
{
$fonts = array();
foreach (scandir(FONTS_PATH) as $file) {
if (!is_dir($file) && strtolower(substr($file, -4)) == '.ttf') {
$fonts[htmlspecialchars(basename($file, '.ttf'), ENT_QUOTES, 'UTF-8')] = $file;
}
}
return $fonts;
}
/**
* returns all available backgrounds as paths
* in an array
*
* @return array
*/
function getBackgrounds()
{
$backgrounds = array();
foreach (scandir(BACKGROUNDS_PATH) as $file) {
if (!is_dir($file) && (stripos($file, '.jpg') || stripos($file, '.png')) && getimagesize(BACKGROUNDS_PATH . $file)) {
$backgrounds[] = str_replace(BASE_PATH, '', BACKGROUNDS_PATH) . htmlspecialchars($file, ENT_QUOTES, 'UTF-8');
}
}
return $backgrounds;
}
/**
* returns all available icons as paths
* in an array
*
* @return array
*/
function getIcons()
{
$icons = array();
foreach (scandir(ICONS_PATH) as $file) {
if (!is_dir($file) && (stripos($file, '.jpg') || stripos($file, '.png')) && getimagesize(ICONS_PATH . $file)) {
$icons[] = str_replace(BASE_PATH, '', ICONS_PATH) . htmlspecialchars($file, ENT_QUOTES, 'UTF-8');
}
}
return $icons;
}
/**
* returns css strings with browser specific prefixes
* as well as the real css string
*
* @param string $css CSS bit to create browser specific bits for
*
* @return string
*/
function browserify($css)
{
$return = '';
foreach (array('-webkit', '-moz', '-ms', '-o') as $browser) {
$return .= $browser . $css . PHP_EOL;
}
return $return . $css . PHP_EOL;
}
/**
* returns the path of a thumbnail for the
* image path provided
*
* @param string $path Path to file to get/make thumbnail for
*
* @return string
*/
function getIconThumbnail($path)
{
$basename = basename($path);
if (!is_file(ICONS_PATH . $basename)) {
return '';
}
$thumbnail_path = ICONTHUMBNAILS_PATH . $basename;
if (is_file($thumbnail_path) && getimagesize($thumbnail_path)) {
return str_replace(BASE_PATH, '', $thumbnail_path);
}
return makeThumbnail($basename);
}
/**
* creates a thumbnail for the icon path provided
*
* @param string $basename Base name of icon to make thumbnail of
*
* @return string
*/
function makeThumbnail($basename)
{
$image_path = ICONS_PATH . $basename;
$image_info = getimagesize($image_path);
if (!$image_info) {
return '';
}
switch ($image_info[2]) {
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($image_path);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($image_path);
break;
default:
return '';
}
if ($image_info[0] <= 20 && $image_info[1] <= 20) {
$destination_height = $image_info[1];
$destination_width = $image_info[0];
} else {
if ($image_info[0] < $image_info[1]) {
$destination_height = THUMBNAILSIZE;
$destination_width = $image_info[0] / ($image_info[1] / THUMBNAILSIZE);
} else {
$destination_width = THUMBNAILSIZE;
$destination_height = $image_info[1] / ($image_info[0] / THUMBNAILSIZE);
}
}
$output_image = imagecreatetruecolor($destination_width, $destination_height);
imagesavealpha($output_image, true);
imagealphablending($output_image, true);
$trans_colour = imagecolorallocatealpha($output_image, 255, 255, 255, 127);
imagefill($output_image, 0, 0, $trans_colour);
imagealphablending($output_image, true);
imagecopyresampled($output_image, $image, 0, 0, 0, 0, $destination_width, $destination_height, $image_info[0], $image_info[1]);
if (imagepng($output_image, ICONTHUMBNAILS_PATH . $basename, 9)) {
return str_replace(BASE_PATH, '', ICONTHUMBNAILS_PATH . $basename);
}
return '';
}