-
Notifications
You must be signed in to change notification settings - Fork 2
/
resize.php
279 lines (217 loc) · 9.64 KB
/
resize.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/*
* ----------------------------------------------------------------------------
* Decoro Urbano version 0.4.0
* ----------------------------------------------------------------------------
* Copyright Maiora Labs Srl (c) 2012
* ----------------------------------------------------------------------------
*
* This file is part of Decoro Urbano.
*
* Decoro Urbano is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Decoro Urbano is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Decoro Urbano. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Script con funzionalità di ridimensionamento delle immagini con meccanismo di caching integrato.
*/
require_once('include/config.php');
ini_set("session.cookie_domain", ".".$settings['sito']['dominio']);
session_start();
// Impostazione politiche di error reporting in funzione del flag di debug
if ($settings['sito']['debug']) {
ini_set('display_errors', 1);
error_reporting(E_ALL);
} else {
ini_set('display_errors', 0);
error_reporting(0);
}
function clean_string($string){
$slug = trim($string); // trim the string
$slug = accents($slug, true);
$slug = str_replace('/','_', $slug); // replace spaces by dashes
$slug = preg_replace('/[^a-zA-Z0-9 -._]/','-',$slug ); // only take alphanumerical characters, but keep the spaces and dashes too...
$slug = str_replace(' ','-', $slug); // replace spaces by dashes
return $slug;
}
function mb_str_split($str, $length = 1, $encoding = 'utf-8') {
if ($length < 1) {
return false;
}
$result = array();
for ($i = 0; $i < mb_strlen($str, $encoding); $i += $length) {
$result[] = mb_substr($str, $i, $length, $encoding);
}
return $result;
}
function accents($str, $normalize = false) {
$str = mb_strtolower($str, 'utf-8');
//accented characters from Wikipedia's alphabet pages at http://en.wikipedia.org/wiki/Basic_modern_Latin_alphabet
$chars = array(
'a' => 'AaÁáÀàĂăẮắẰằẴẵẲẳÂâẤấẦầẪẫẨẩǍǎÅåǺǻÄäǞǟÃãȦȧǠǡĄąĀāẢảȀȁȂȃẠạẶặẬậḀḁȺⱥᶏⱯɐⱭɑ',
'b' => 'BbḂḃḄḅḆḇɃƀƁɓƂƃᵬᶀ',
'c' => 'CcĆćĈĉČčĊċÇçḈḉȻȼƇƈɕ',
'd' => 'DdĎďḊḋḐḑḌḍḒḓḎḏĐđƉɖƊɗƋƌᵭᶁᶑȡ∂',
'e' => 'EeÉéÈèĔĕÊêẾếỀềỄễỂểĚěËëẼẽĖėȨȩḜḝĘęĒēḖḗḔḕẺẻȄȅȆȇẸẹỆệḘḙḚḛɆɇᶒⱸ',
'f' => 'FfḞḟƑƒᵮᶂ',
'g' => 'GgǴǵĞğĜĝǦǧĠġĢģḠḡǤǥƓɠᶃ',
'h' => 'HhĤĥȞȟḦḧḢḣḨḩḤḥḪḫH̱ẖĦħⱧⱨ',
'i' => 'IiÍíÌìĬĭÎîǏǐÏïḮḯĨĩĮįĪīỈỉȈȉȊȋỊịḬḭƗɨᵻᶖİiIı',
'j' => 'JjĴĵɈɉJ̌ǰȷʝɟʄ',
'k' => 'KkḰḱǨǩĶķḲḳḴḵƘƙⱩⱪᶄꝀꝁ',
'l' => 'LlĹ弾ĻļḶḷḸḹḼḽḺḻŁłĿŀȽƚⱠⱡⱢɫɬᶅɭȴ',
'm' => 'MmḾḿṀṁṂṃᵯᶆⱮɱ',
'n' => 'NnŃńǸǹŇňÑñṄṅŅņṆṇṊṋṈṉN̈n̈ƝɲȠƞᵰᶇɳȵ',
'o' => 'OoÓóÒòŎŏÔôỐốỒồỖỗỔổǑǒÖöȪȫŐőÕõṌṍṎṏȬȭȮȯȰȱØøǾǿǪǫǬǭŌōṒṓṐṑỎỏȌȍȎȏƠơỚớỜờỠỡỞởỢợỌọỘộƟɵⱺ',
'p' => 'PpṔṕṖṗⱣᵽƤƥP̃p̃ᵱᶈ',
'q' => 'QqɊɋʠ',
'r' => 'RrŔŕŘřṘṙŖŗȐȑȒȓṚṛṜṝṞṟɌɍⱤɽᵲᶉɼɾᵳ',
's' => 'SsŚśṤṥŜŝŠšṦṧṠṡẛŞşṢṣṨṩȘșS̩s̩ᵴᶊʂȿ',
't' => 'TtŤťṪṫŢţṬṭȚțṰṱṮṯŦŧȾⱦƬƭƮʈT̈ẗᵵƫȶ',
'u' => 'UuÚúÙùŬŭÛûǓǔŮůÜüǗǘǛǜǙǚǕǖŰűŨũṸṹŲųŪūṺṻỦủȔȕȖȗƯưỨứỪừỮữỬửỰựỤụṲṳṶṷṴṵɄʉᵾᶙ',
'v' => 'VvṼṽṾṿƲʋᶌⱱⱴ',
'w' => 'WwẂẃẀẁŴŵẄẅẆẇẈẉW̊ẘⱲⱳ',
'x' => 'XxẌẍẊẋᶍ',
'y' => 'YyÝýỲỳŶŷY̊ẙŸÿỸỹẎẏȲȳỶỷỴỵɎɏƳƴʏ',
'z' => 'ZzŹźẐẑŽžŻżẒẓẔẕƵƶȤȥⱫⱬᵶᶎʐʑɀ'
);
if ($normalize) {
foreach ($chars as $normal => $accents) {
$str = str_replace(mb_str_split($accents), $normal, $str);
}
return $str;
} else {
if (!array_key_exists($str, $chars)) {
return false;
}
return mb_str_split($chars[$str]);
}
}
function resize_crop($original_image_gd,$crop_height,$crop_width) {
$original_width = imagesx ($original_image_gd);
$original_height = imagesy ($original_image_gd);
if ($crop_height && !$crop_width) {
$crop_width = $crop_height*$original_width/$original_height;
}
if ($crop_width && !$crop_height) {
$crop_height = $crop_width*$original_height/$original_width;
}
$cropped_image_gd = imagecreatetruecolor($crop_width, $crop_height);
imagealphablending($cropped_image_gd, false);
imagesavealpha($cropped_image_gd,true);
$wm = $original_width /$crop_width;
$hm = $original_height /$crop_height;
$h_height = $crop_height/2;
$w_height = $crop_width/2;
if($original_width > $original_height ) {
$adjusted_width =$original_width / $hm;
$half_width = $adjusted_width / 2;
$int_width = $half_width - $w_height;
imagecopyresampled($cropped_image_gd ,$original_image_gd ,-$int_width,0,0,0, $adjusted_width, $crop_height, $original_width , $original_height );
} elseif(($original_width < $original_height ) || ($original_width == $original_height )) {
$adjusted_height = $original_height / $wm;
$half_height = $adjusted_height / 2;
$int_height = $half_height - $h_height;
imagecopyresampled($cropped_image_gd , $original_image_gd ,0,-$int_height,0,0, $crop_width, $adjusted_height, $original_width , $original_height );
} else {
imagecopyresampled($cropped_image_gd , $original_image_gd ,0,0,0,0, $crop_width, $crop_height, $original_width , $original_height );
}
return $cropped_image_gd;
}
if (isset($_GET['s'])) {
$el = explode('-',$_GET['s']);
$len = count($el);
$idu = $el[$len-4];
$ids = $el[$len-3];
$source_file_name = 'images/segnalazioni/'.$idu.'/'.$ids.'/1.jpeg';
if (!is_file($settings['sito']['percorso'].$source_file_name)) {
switch ($el[0]) {
case 'buona':
$source_file_name = 'images/placeholder_bp.png';
break;
default:
$source_file_name = '';
break;
}
}
$target_width = $el[$len-2];
$target_height = $el[$len-1];
$crop_width=isset($target_width)?$target_width:0;
$crop_height=isset($target_height)?$target_height:0;
} else {
$crop_width=isset($_GET['w'])?$_GET['w']:0;
$crop_height=isset($_GET['h'])?$_GET['h']:0;
$source_file_name=$_GET['f'];
}
$cache_file = clean_string($crop_width.'_'.$crop_height.'_'.$source_file_name);
$cache_path = 'images/cache/';
$cache_full_path = $cache_path.$cache_file;
if (substr($source_file_name,0,7) == 'http://' || substr($source_file_name,0,8) == 'https://') {
if (is_file($cache_full_path)) {
header ('Content-length: ' .filesize($cache_full_path));
header ("Content-type: image/jpeg");
readfile($cache_full_path);
} else {
$data = file_get_contents($source_file_name);
$original_image_gd = imagecreatefromstring($data);
if ($crop_width != 0 || $crop_height != 0) $cropped_image_gd = resize_crop($original_image_gd,$crop_height,$crop_width);
else $cropped_image_gd = $original_image_gd;
imagejpeg($cropped_image_gd,$cache_full_path);
header ('Content-length: ' .filesize($cache_full_path));
header ("Content-type: image/jpeg");
readfile($cache_full_path);
}
} else {
if (substr($source_file_name,0,1) == '/') $source_file_name = substr($source_file_name,1);
if ($crop_width == 0 && $crop_height == 0 && is_file($source_file_name)) {
header ('Content-length: ' .filesize($source_file_name));
header ("Content-type: image/jpeg");
readfile($source_file_name);
} elseif (is_file($cache_full_path) && is_file($source_file_name) && filemtime($cache_full_path) > filemtime($source_file_name)) {
header ('Content-length: ' .filesize($cache_full_path));
header ("Content-type: image/jpeg");
readfile($cache_full_path);
} else {
$file_name=$source_file_name;
if (!is_file($file_name)) {
header("HTTP/1.0 404 Not Found");
exit;
}
$image_data = getimagesize($file_name);
$file_type = $image_data['mime'];
if($file_type=='jpg' || $file_type=='jpeg' || $file_type == 'image/jpeg') {
$original_image_gd = imagecreatefromjpeg($file_name);
} else if($file_type=='gif' || $file_type == 'image/gif') {
$original_image_gd = imagecreatefromgif($file_name);
} else if($file_type=='png' || $file_type == 'image/png') {
$original_image_gd = imagecreatefrompng($file_name);
}
$cropped_image_gd = resize_crop($original_image_gd,$crop_height,$crop_width);
if($file_type=='jpg' || $file_type=='jpeg' || $file_type == 'image/jpeg') {
imagejpeg($cropped_image_gd,$cache_full_path);
header ('Content-length: ' .filesize($cache_full_path));
header ("Content-type: image/jpeg");
readfile($cache_full_path);
} else if($file_type=='gif' || $file_type == 'image/gif') {
imagegif($cropped_image_gd,$cache_full_path);
header ('Content-length: ' .filesize($cache_full_path));
header ("Content-type: image/gif");
readfile($cache_full_path);
} else if($file_type=='png' || $file_type == 'image/png') {
imagepng($cropped_image_gd,$cache_full_path);
header ('Content-length: ' .filesize($cache_full_path));
header ("Content-type: image/png");
readfile($cache_full_path);
}
}
}
?>