-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_next.php
More file actions
390 lines (296 loc) · 13 KB
/
Copy pathprocess_next.php
File metadata and controls
390 lines (296 loc) · 13 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
///TO DO :
// Clean up could by adding SWITCH / CASE for file types
//See if you can fix thumbnail brightness for .dng raws
//Make sure moving raws without .XMLs isn't creating permanent breaks
//Handle .pngs
// Think through and implement how to migrate META data from current DB for Cell Photos
//Move Colors into function
//Think through and implement HEX colors to HLS or HLV for better Search
//Consider Preview Option sizes
//Deal with "invalid" .JPG files - Leave them where they are probably .PSDs
//MAKE JPGS FROM RAWS AND PRESENT THEM IN SINGLE.PHP
// BROKE THINGS WHEN I CHANGED APACHE ROOT TO
// REPOS IN ORDER TO INCLUDE THUMBNAIL DIRECTORY. GOT ALL THUMBS WORKING
// ACCEPT IMAGICK JPEGS. SOMETHING WRONG WITH LINE 76 or 319
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
//THIS IS ABOUT TO TAKE A LOT OF MEMORY
ini_set("memory_limit","10000M");
ini_set('max_execution_time', 3000); //300 seconds = 5 minutes
//WE WANT TO SEE ALL THE ERRORS
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
//GET MYSQL CREDS
require "../../mysql_creds.php";
//SECURITY
require_once("security.php");
//COLOR EXTRACTION CLASS
include_once("colors.inc.php");
//RECIEVE SINGLE FILE FROM BEGIN_PROCESSING.PHP
$file = $_GET['file_name'];
$size = $_GET['size'];
$quality = $_GET['quality'];
$pre_tags = $_GET['pre_tags'];
if(isset($_GET['rate'])){
$rate = $_GET['rate'];
}else{
$rate= "";
}
//SET A SKIP VARIABLE
$skip = 0;
function returnNewSerial($link){
//Get current Serial Number from DB for naming
$serialsql = "SELECT ID FROM media ORDER BY ID DESC limit 1";
$serialraw = mysqli_query($link, $serialsql );
if (mysqli_num_rows($serialraw) > 0) {
$row = mysqli_fetch_assoc($serialraw);
$serial = $row["ID"] + 1;
} else {
$serial = 1;
}
return $serial;
}
//JPEG THUMBNAIL FUNCTION
function makeAThumbFromJpeg($file,$serial,$size,$quality){
global $status;
//$filename_safe = str_replace("/","|", $file);
$thumbname=$serial."_thumb_".$size.".jpg";
$status .= "<B>CREATING THUMBNAIL: </B> " . $thumbname."<BR>";
$save_path = '/Users/benconnors/Bens Things/Code/Repos/thumbs/';
echo getcwd();
$im = imagecreatefromjpeg($file);
$new_x = $size;
$factor = $size / imagesx($im);
$new_y = imagesy($im) * $factor;
$small = imagecreatetruecolor($new_x,$new_y);
imagecopyresampled($small,$im,0,0,0,0,$new_x,$new_y,imagesx($im),imagesy($im));
imagejpeg($small,$save_path.$thumbname,$quality);
imagedestroy($im);
imagedestroy($small);
}
//RAW THUMBNAIL FUNCTION
function makeAThumbFromRaw($file,$serial,$size,$quality){
global $status;
$ufraw_path = "/usr/local/Cellar/ufraw/0.22_2/bin/ufraw-batch";
$raw_file_path ="/Users/benconnors/Bens\ Things/Code/Repos/Photos/".str_replace(" ","\ ",$file);
$output_filename="../thumbs/".$serial."_thumb_".$size.".jpg";
$maxsize=$size;
$quality=$quality;
$command= $ufraw_path . " ". $raw_file_path ." --out-type=jpeg --compression=". $quality ." --exposure=auto --size=". $maxsize .",". $maxsize ." --output=" . $output_filename ;
system($command);
$status .= "<B>command:</B>" . $command . "<br>";
}
//VIDEO THUMBNAIL FUNCTION
function makeAThumbFromVideo($file,$serial,$size,$quality){
$ffmpeg_path = "/usr/local/bin/ffmpeg";
$loop_legnth_seconds="2.5";
$start_at_seconds="2";
$video_file_path ="/Users/benconnors/Bens\ Things/Code/Repos/Photos/".str_replace(" ","\ ",$file);
$output_file_path="../thumbs/".$serial."_thumb_".$size.".gif";
$width=$size;
$fps="12";
$gif_command= $ffmpeg_path . " -ss ". $start_at_seconds ." -t " . $loop_legnth_seconds ." -i ". $video_file_path ." -filter_complex '[0:v] fps=".$fps.",scale=w=".$width.":h=-1,split [a][b];[a] palettegen=stats_mode=single [p];[b][p] paletteuse=new=1' ". $output_file_path ;
//system($gif_command);
exec($gif_command);
$jpg_output_filename="../thumbs/".$serial."_thumb_".$size.".jpg";
$jpg_command= $ffmpeg_path . " -i ". $video_file_path ." -vf 'thumbnail,scale=".$width.":-1' -qscale:v 4 -frames:v 1 ".$jpg_output_filename;
exec($jpg_command);
}
//CREATE TAGS BASED ON NAME OF FILE
function getTagsFromDirectory($file){
global $tags, $batch_tags,$status,$rate;
//If in a "Select" Directory give it a 4 rating
if (strpos(strtolower($file),'select') !== false) {
$tags .= 'select,aesthetic';
$rate = '4';
$status .= '<B>IMPORTED AS A SELECT!</B><BR>';
}
//GET ADDITIONAL INFO FROM FILE STRUCTURE AND SAVE IT TO TAGS
$name_tags = str_replace("/",",", $file);
$tags .= strtolower($name_tags);
}
//GET GPS FROM EXIF DATA
function getGps($exifCoord, $hemi) {
$degrees = count($exifCoord) > 0 ? gps2Num($exifCoord[0]) : 0;
$minutes = count($exifCoord) > 1 ? gps2Num($exifCoord[1]) : 0;
$seconds = count($exifCoord) > 2 ? gps2Num($exifCoord[2]) : 0;
$flip = ($hemi == 'W' or $hemi == 'S') ? -1 : 1;
return $flip * ($degrees + $minutes / 60 + $seconds / 3600);
}
//CONVERT GPS INTO NUMERIC DATA
function gps2Num($coordPart) {
$parts = explode('/', $coordPart);
if (count($parts) <= 0)
return 0;
if (count($parts) == 1)
return $parts[0];
return floatval($parts[0]) / floatval($parts[1]);
}
//GET LAT LON & TIME FROM EXIF
function getEXIFdata($file){
global $status,$lat,$lon,$time;
//READ EXIF IF POSSIBLE & IGNORE ERRORS IF NOT
set_error_handler(function() { /* ignore errors */ });
$exif = exif_read_data($file);
restore_error_handler();
//GET LAT LON IF EXISTS OR FALLBACK
if(isset($exif["GPSLongitude"])){
$lon = getGps($exif["GPSLongitude"], $exif['GPSLongitudeRef']);
$lat = getGps($exif["GPSLatitude"], $exif['GPSLatitudeRef']);
}else{
$lon = '';
$lat = '';
}
//GET DATE TIME DIGITIZED IF EXISTS OR FALL BACK
if(isset($exif["DateTimeDigitized"])){
$time = strtotime($exif["DateTimeDigitized"]);
}else{
//OR FALLBACK TO USE DOB AS DEFAULT TIME
$time = strtotime("1983-8-23");
}
//return array($lat,$lon,$time);
}
//LOOK FOR TIMESTAMP IN DB. IF DUP PUT ASIDE
//Proceed as normal for dateless / aka 8-23-83 Photos
function dupCheck(){
global $status,$time,$link,$dupOf;
if ($time != strtotime("1983-8-23")){
$result = mysqli_query($link , "SELECT ID FROM media WHERE time = $time");
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$status .= "<BR> <B style='color:#FAA;'>SUSPECTED DUPLICATE OF: </B>". $row["ID"] ."<BR>";
$status .= "<a target='_blank' href='single.php?i=".$row["ID"]."'><img width='50px' src='../thumbs/" . $row["ID"]. "_thumb_495.jpg'></a><BR>";
$dupOf = $row["ID"];
}
}
}else{
$dupOf = "";
}
}
//GET LAT LON EXIF, GET TIME FROM EXIF OR FALLBACK OPTIONS
function getMovieMetadata($file){
global $status,$lat,$lon,$time;
$ffmpeg_path = "/usr/local/bin/ffprobe";
$movie_meta_command= $ffmpeg_path . " -show_streams -show_format ".$file;
$movie_meta_output = shell_exec($movie_meta_command);
//Parse movie meta for created
preg_match_all('/creation_time=(.*?)Z/', $movie_meta_output, $matches);
if(!empty($matches[1])) {
$time = strtotime( $matches[1][0]);
}else{
//OR FALLBACK TO USE DOB AS DEFAULT TIME
$time = strtotime("1983-8-23");
}
}
//GRAB NEXT AVAILABLE SERIAL
$serial =returnNewSerial($link);
//CLEAR VARIABLES FOR LOOP REPEATS
$status='<B>URL: </B>'. "$_SERVER[REQUEST_URI]" .'<BR>';
$tags="";
//Break filename structure apart
$filename = explode('.',$file);
$type = array_pop($filename);
//Commit file & process details to Status
$status .= "<B>SERIAL:</B> ". $serial . "<BR>";
$status .= '<B>PROCESSING:</B> ' . $file . ' <BR>';
getTagsFromDirectory($file);
//READ EXIF DATA OR FALL BACK TO NAMING CONVENTION FOR TIME & LAT LON
//list($lat,$lon,$time) = getLatLonTime($file);
///////////////////////////////
//FIGURE OUT FILE TYPE AND START PERFORMING OPERATIONS
///////////////////////////////
if (strtolower($type) == 'jpg' )
{
$preview_ext_type =".jpg";
getEXIFdata($file);
dupCheck();
makeAThumbFromJpeg($file,$serial,$size,$quality);
}elseif(strtolower($type) == 'cr2'){
$preview_ext_type =".jpg";
getEXIFdata($file);
dupCheck();
makeAThumbFromRaw($file,$serial,$size,$quality);
//Big Thumbs for RAWs
makeAThumbFromRaw($file,$serial,'2880',$quality);
}elseif(strtolower($type) == 'dng'){
getEXIFdata($file);
dupCheck();
$preview_ext_type =".jpg";
makeAThumbFromRaw($file,$serial,$size,$quality);
//Big Thumbs for RAWs
makeAThumbFromRaw($file,$serial,'2880',$quality);
}elseif(strtolower($type) == 'nef'){
getEXIFdata($file);
dupCheck();
$preview_ext_type =".jpg";
makeAThumbFromRaw($file,$serial,$size,$quality);
//Big Thumbs for RAWs
makeAThumbFromRaw($file,$serial,'2880',$quality);
}elseif(strtolower($type) == 'mp4'){
getMovieMetadata($file);
dupCheck();
$preview_ext_type =".gif";
makeAThumbFromVideo($file,$serial,$size,$quality);
}elseif(strtolower($type) == 'mov'){
getMovieMetadata($file);
dupCheck();
$preview_ext_type =".gif";
makeAThumbFromVideo($file,$serial,$size,$quality);
}elseif(strtolower($type) == '3gp'){
$preview_ext_type =".gif";
getMovieMetadata($file);
dupCheck();
makeAThumbFromVideo($file,$serial,$size,$quality);
}else{
//////////////////////////
// HANDLE ALL OTHER RANDOM FILES
///////////////////////
$skip=1;
$status = "<B>SKIPPED: </B>." . $file. "<BR>";
}
//WRITE TYPE TO STATUS
$status .= "<B>TYPE:</B> " . $type . "<BR>";
//COLLECT ALL THE TAGS
$tags .= ','.$pre_tags;
$status .= '<B>TAGS</B>: ' . $tags .'<BR>';
// ANYTHING THAT'S AN IDENTIFIABLE FILE TYPE MOVE & ADD TO DB ALO PRINT STATUS
if ($skip == 0) {
//COLORS
$num_results = 8;
$delta = 24;
$reduce_brightness = 1;
$reduce_gradients = 1;
$ex=new GetMostCommonColors();
$colors=$ex->Get_Color('../thumbs/'. $serial. '_thumb_'.$size.'.jpg', $num_results, $reduce_brightness, $reduce_gradients, $delta);
$color_array = json_encode($colors);
$status .= "<B>COLORS :</B> ";
foreach ( $colors as $hex => $count )
{
$status .= "<span style='font-size:15px; font-family:Tahoma; padding-left:15px; border:1px solid white; overflow:hidden; background-color:#".$hex.";'></span> ";
}
$status .="<BR>";
// BUILD PREVIEW IMAGE TAG BASED ON TYPE
$preview_img = '<img src="../thumbs/'. $serial. '_thumb_'.$size. $preview_ext_type. '" style="float:left; margin-right:12px;">';
$status.= "<B>LAT:</B> ". $lat ."<BR><B> LON: </B>". $lon . "<BR><B>TIME: </B>" . date("Y-m-d h:i:sa", $time). "<BR>" ;
//PRINT STATUS W PIC
echo "<div style='border:1px solid #ccc; margin-bottom:20px;'>" . $preview_img . $status . "<div style='clear:both;'></div></div>";
//echo '{"serial":"'.$serial.'", "block":"<div style=\'border:1px solid #ccc; margin-bottom:20px;\'>' . $preview_img . $status . '<div style=\'clear:both;\'></div></div>"}';
//MOVE AND RENAME FILE
rename($file, '../image_archive/'. $serial.'.'. $type);
$status .= '<B>MOVED TO</B> image_archive/'.$serial.'.'.$type.'<br>';
//INSERT INTO DB
mysqli_query($link , "INSERT INTO media (lat, lon, time, type, tag, rate, colors,dup_of) VALUES( '$lat', '$lon', '$time', '$type', '$tags' ,'$rate', '$color_array','$dupOf') ") or die(mysql_error());
}else{
//VISIBILY NOTE SKIPPED Files
//PRINT STATUS W PIC
echo "<div style='border:1px solid #ccc; margin-bottom:20px;'><B>SKIPPED:" . $file. "<div style='clear:both;'></div></div>";
//echo '{"serial":"'.$serial.'", "block":"<div style=\'border:1px solid #ccc; margin-bottom:20px;\'><B>SKIPPED:' . $file. '<div style=\'clear:both;\'></div></div>"}';
}
$skip=0;
?>