Subversion Repositories svnkaklik

Rev

Details | Last modification | View Log

Rev Author Line No. Line
6 kaklik 1
<?php
2
/*************************
3
  Coppermine Photo Gallery
4
  ************************
5
  Copyright (c) 2003-2005 Coppermine Dev Team
6
  v1.1 originaly written by Gregory DEMAR
7
 
8
  This program is free software; you can redistribute it and/or modify
9
  it under the terms of the GNU General Public License as published by
10
  the Free Software Foundation; either version 2 of the License, or
11
  (at your option) any later version.
12
  ********************************************
13
  Coppermine version: 1.3.3
14
  $Source: /cvsroot/coppermine/stable/image_processor.php,v $
15
  $Revision: 1.5 $
16
  $Author: gaugau $
17
  $Date: 2005/04/19 03:17:10 $
18
**********************************************/
19
 
20
// To use this module, one must place a transitory directory in the primary
21
// CPG folder. Then one must change the form action of upload.php to this
22
// script instead of db_imput.
23
 
24
// Let all the libraries know that this program is part of Coppermine.
25
define('IN_COPPERMINE', true);
26
 
27
// Let the libraries know this is the image_processor_php file.
28
define('IMAGE_PROCESSOR_PHP', true);
29
 
30
//         Require include/init.inc.php, so we may have
31
// access to Coppermine's configuration information.
32
 
33
require('include/init.inc.php');
34
 
35
// Declare global variables.
36
global $CONFIG, $lang_image_processor_php;
37
 
38
 
39
//-----------------------------FUNCTION BLOCK---------------------------------
40
 
41
function rotate_image($path_to_primary_image, $degrees) {
42
 
43
// This function takes $path_to_primary_image and $degrees
44
//as arguments.  It returns nothing.
45
 
46
//Globalize appropriate variables.
47
global $CONFIG, $lang_image_processor_php;
48
 
49
//        The $method varaible should contain gd1, gd2,
50
// or im, which indicates GD Library v1, GD Library v2,
51
// and ImageMagick respectively. We will use a switch
52
// statement to control the program flow based on the
53
// value of the $method variable.
54
 
55
$method = $CONFIG['thumb_method'];
56
 
57
// Detect if user has the function imageistruecolor(). This function is
58
// available in GD2 in PHP 4.3.2 and up.  It allows GD to distinguish between
59
// palette images and true color images, which allows GD to make the appropriate
60
// canvas.
61
 
62
if (($method == 'gd2') and (!function_exists('imageistruecolor'))) {
63
 
64
        //Set ignore imageistruecolor to false.
65
        $ignore = 0;
66
} else {
67
        //Set $ignore imageistruecolor to true.
68
        $ignore = 1;
69
}
70
 
71
switch ($method) {
72
 
73
        case "im" :
74
 
75
                $real_path_to_primary_image = realpath($path_to_primary_image);
76
 
77
                $output = array();
78
 
79
                // Set IM path.
80
                $im_path = $CONFIG['impath'];
81
 
82
                //Check the IM path for the final slash.
83
                if (eregi('/$',$im_path) or empty($im_path)) {
84
                        $trailing_slash = "";
85
                    } else {
86
                        $trailing_slash = "/";
87
                    }
88
 
89
                //Select degree measure.
90
 
91
                if (($degrees == 90) or ($degrees == 180) or ($degrees == 270)) {
92
 
93
                        //Form the command to rotate the image.
94
                        $cmd = "{$CONFIG['impath']}".$trailing_slash."mogrify -quality \"100\" -rotate \"$degrees\" \"$real_path_to_primary_image\"";
95
 
96
                }
97
 
98
                exec ($cmd, $output, $retval);
99
 
100
                if ($retval) {
101
                        $ERROR = $lang_image_processor_php['IM_Error'] . $retval;
102
                        if ($CONFIG['debug_mode']) {
103
                                // Re-execute the command with the backtit operator in order to get all outputs
104
                                // will not work is safe mode is enabled
105
                                $output = `$cmd 2>&1`;
106
                                $ERROR .= "<br /><br /><div align=\"left\">{$lang_image_processor_php['cmd_line']}<br /><font size=\"2\">".nl2br(htmlspecialchars($cmd))."</font></div>";
107
                                $ERROR .= "<br /><br /><div align=\"left\">{$lang_image_processor_php['mog_said']}<br /><font size=\"2\">";
108
                                $ERROR .= nl2br(htmlspecialchars($output));
109
                                $ERROR .= "</font></div>";
110
                        }
111
                        die($ERROR);
112
 
113
                }
114
 
115
                break;
116
 
117
        case "gd2" :
118
 
119
                if ($ignore) {
120
 
121
                        $image_handle = get_handle($path_to_primary_image);
122
                        if (imageistruecolor($image_handle)) {
123
                                imagedestroy($image_handle);
124
                                true_color_rotate($path_to_primary_image, $degrees);
125
                        } else {
126
                                imagedestroy($image_handle);
127
                                palette_rotate($path_to_primary_image, $degrees);
128
                        }
129
                } else {
130
                        true_color_rotate($path_to_primary_image, $degrees);
131
                }
132
 
133
                break;
134
 
135
        case "gd1" :
136
 
137
                palette_rotate($path_to_primary_image, $degrees);
138
 
139
                break;
140
 
141
}
142
 
143
}
144
 
145
//*********************************************************************************************
146
 
147
function true_color_rotate($path_to_primary_image, $degrees) {
148
 
149
global $lang_image_processor_php;
150
 
151
// Get image info.
152
$source_image_size_and_type = getimagesize($path_to_primary_image) or die($lang_image_processor_php['file_corrupt']);
153
$source_image_width = $source_image_size_and_type[0];
154
$source_image_height = $source_image_size_and_type[1];
155
$source_image_type = $source_image_size_and_type[2];
156
 
157
//Set new canvas size.
158
if ( $source_image_width > $source_image_height ) {
159
    $new_maximum_dimension = $source_image_width;
160
} else {
161
    $new_maximum_dimension = $source_image_height;
162
}
163
 
164
 
165
$image_handle = get_handle($path_to_primary_image);
166
 
167
        if ($degrees == "90") {
168
                $destination_image_handle = ImageCreateTrueColor($new_maximum_dimension,$new_maximum_dimension);
169
                imagecopy($destination_image_handle,$image_handle,0,0,0,0,$source_image_width,$source_image_height);
170
                imagedestroy($image_handle);
171
                $rotated_image_handle = imagerotate($destination_image_handle,$degrees,0);
172
                $final_image_handle = ImageCreateTrueColor($source_image_height,$source_image_width);
173
 
174
                // Determine orientation.
175
 
176
                if ($source_image_height > $source_image_width) {
177
 
178
                        $difference = $source_image_height-$source_image_width;
179
                        imagecopy($final_image_handle,$rotated_image_handle,0,0,0,$difference,$source_image_height,$source_image_width);
180
 
181
                } else {
182
 
183
                        imagecopy($final_image_handle,$rotated_image_handle,0,0,0,0,$source_image_height,$source_image_width);
184
 
185
                }
186
 
187
                imagedestroy($rotated_image_handle);
188
 
189
        } elseif ($degrees == "270") {
190
 
191
                $new_dimension = $source_image_width + $source_image_height;
192
                $destination_image_handle = ImageCreateTrueColor($new_dimension,$new_dimension);
193
                imagecopy($destination_image_handle,$image_handle,$source_image_height,$source_image_width,0,0,$source_image_width,$source_image_height);
194
                imagedestroy($image_handle);
195
                $final_image_handle = ImageCreateTrueColor($source_image_height,$source_image_width);
196
                $rotated_image_handle = imagerotate($destination_image_handle,$degrees,0);
197
                imagecopy($final_image_handle,$rotated_image_handle,0,0,0,$source_image_height,$source_image_height,$source_image_width);
198
                imagedestroy($rotated_image_handle);
199
 
200
        } elseif ($degrees == "180") {
201
 
202
                $destination_image_handle = ImageCreateTrueColor(2*$source_image_width,$source_image_height);
203
                imagecopy($destination_image_handle,$image_handle,$source_image_width,0,0,0,$source_image_width,$source_image_height);
204
                imagedestroy($image_handle);
205
                $final_image_handle = ImageCreateTrueColor($source_image_width,$source_image_height);
206
                $rotated_image_handle = imagerotate($destination_image_handle,$degrees,0);
207
                imagecopy($final_image_handle,$rotated_image_handle,0,0,0,0,$source_image_width,$source_image_height);
208
                imagedestroy($rotated_image_handle);
209
 
210
        }
211
 
212
 
213
// Now let's write the image to disk.
214
 
215
if ($source_image_type == "2") {
216
 
217
        imagejpeg($final_image_handle, $path_to_primary_image, 100) or die($lang_image_processor_php['no_write']);
218
 
219
} elseif ($source_image_type == "3") {
220
 
221
        imagepng($final_image_handle, $path_to_primary_image) or die($lang_image_processor_php['no_write']);
222
 
223
}
224
 
225
// Destroy the final image handle.
226
 
227
imagedestroy($final_image_handle);
228
 
229
}
230
 
231
//******************************************************************************************
232
 
233
function palette_rotate($path_to_primary_image, $degrees) {
234
 
235
global $lang_image_processor_php;
236
 
237
// Get image info.
238
$source_image_size_and_type = getimagesize($path_to_primary_image) or die($lang_image_processor_php['file_corrupt']);
239
$source_image_width = $source_image_size_and_type[0];
240
$source_image_height = $source_image_size_and_type[1];
241
$source_image_type = $source_image_size_and_type[2];
242
 
243
//Set new canvas size.
244
if ( $source_image_width > $source_image_height ) {
245
    $new_maximum_dimension = $source_image_width;
246
} else {
247
    $new_maximum_dimension = $source_image_height;
248
}
249
 
250
$image_handle = get_handle($path_to_primary_image);
251
 
252
if ($degrees == "90") {
253
 
254
        $destination_image_handle = ImageCreate($new_maximum_dimension,$new_maximum_dimension);
255
        imagecopy($destination_image_handle,$image_handle,0,0,0,0,$source_image_width,$source_image_height);
256
        imagedestroy($image_handle);
257
        $rotated_image_handle = imagerotate($destination_image_handle,$degrees,0);
258
        $final_image_handle = ImageCreate($source_image_height,$source_image_width);
259
 
260
        // Determine orientation.
261
 
262
        if ($source_image_height > $source_image_width) {
263
 
264
                $difference = $source_image_height-$source_image_width;
265
                imagecopy($final_image_handle,$rotated_image_handle,0,0,0,$difference,$source_image_height,$source_image_width);
266
 
267
        } else {
268
 
269
                imagecopy($final_image_handle,$rotated_image_handle,0,0,0,0,$source_image_height,$source_image_width);
270
 
271
        }
272
 
273
        imagedestroy($rotated_image_handle);
274
 
275
} elseif ($degrees == "270") {
276
 
277
        $new_dimension = $source_image_width + $source_image_height;
278
        $destination_image_handle = ImageCreate($new_dimension,$new_dimension);
279
        imagecopy($destination_image_handle,$image_handle,$source_image_height,$source_image_width,0,0,$source_image_width,$source_image_height);
280
        imagedestroy($image_handle);
281
        $final_image_handle = ImageCreate($source_image_height,$source_image_width);
282
        $rotated_image_handle = imagerotate($destination_image_handle,$degrees,0);
283
        imagecopy($final_image_handle,$rotated_image_handle,0,0,0,$source_image_height,$source_image_height,$source_image_width);
284
        imagedestroy($rotated_image_handle);
285
 
286
 
287
} elseif ($degrees == "180") {
288
 
289
        $destination_image_handle = ImageCreate(2*$source_image_width,$source_image_height);
290
        imagecopy($destination_image_handle,$image_handle,$source_image_width,0,0,0,$source_image_width,$source_image_height);
291
        imagedestroy($image_handle);
292
        $final_image_handle = ImageCreate($source_image_width,$source_image_height);
293
        $rotated_image_handle = imagerotate($destination_image_handle,$degrees,0);
294
        imagecopy($final_image_handle,$rotated_image_handle,0,0,0,0,$source_image_width,$source_image_height);
295
        imagedestroy($rotated_image_handle);
296
 
297
}
298
 
299
// Now let's write the image to disk.
300
 
301
if ($source_image_type == "2") {
302
 
303
        imagejpeg($final_image_handle, $path_to_primary_image, 100) or die($lang_image_processor_php['no_write']);
304
 
305
} elseif ($source_image_type == "3") {
306
 
307
        imagepng($final_image_handle, $path_to_primary_image) or die($lang_image_processor_php['no_write']);
308
 
309
}
310
 
311
// Destroy the final image handle.
312
 
313
imagedestroy($final_image_handle);
314
 
315
 
316
}
317
 
318
//**********************************************
319
 
320
function get_handle($path_to_primary_image) {
321
 
322
global $lang_image_processor_php;
323
 
324
// Let's use this information to create the handle with which to hold our lovely
325
// image. The variable $image_handle is the handle that points to the image's
326
// location in memory. Other handle creating functions are available (wireless
327
// bitmap, for example), but they are very rarely needed. You can learn about
328
// them at php.net in the function library. Look under image functions. If you
329
// desire, you could add these types to the following if-then-else statements.
330
 
331
$source_image_size_and_type = getimagesize ($path_to_primary_image) or die($lang_image_processor_php['file_corrupt']);
332
$source_image_width = $source_image_size_and_type[0];
333
$source_image_height = $source_image_size_and_type[1];
334
$source_image_type = $source_image_size_and_type[2];
335
 
336
if ($source_image_type == "1") {
337
 
338
        // The image is a GIF file, which we cannot use due to the Unisys patent.
339
        // The image can be read by GD, but GD cannot create it again. It is
340
        // possible to convert a GIF to PNG format using a command line call to
341
        // the appropriate program (i.e. GIF2PNG), but the installation of this
342
        // program on servers is by no means consistent. Therefore, we will
343
        // forgo GIF support. We will return an error.
344
 
345
        cpg_die(CRITICAL_ERROR, $lang_image_processor_php['GD_GIF_Warning'], __FILE__, __LINE__);
346
 
347
} elseif ($source_image_type == "2") {
348
 
349
        // The image is a JPG file, so we must use the function
350
        // imagecreatefromjpeg.
351
 
352
        $image_handle = imagecreatefromjpeg($path_to_primary_image);
353
 
354
} elseif ($source_image_type == "3") {
355
 
356
        // The image is a PNG file, so we must use the function
357
        // imagecreatefrompng.
358
 
359
        $image_handle = imagecreatefrompng($path_to_primary_image);
360
 
361
} else {
362
 
363
        // The user has given us an image we do not wish to work with. We return an
364
        // error.
365
 
366
 
367
cpg_die(CRITICAL_ERROR, $lang_image_processor_php['not_supported'], __FILE__, __LINE__);
368
 
369
}
370
 
371
return $image_handle;
372
 
373
}
374
 
375
//******************************************************
376
 
377
function image_preview($path_to_primary_image, $maximum_width) {
378
 
379
//Globalize appropriate variables.
380
global $CONFIG, $lang_image_processor_php, $preview_image_directory;
381
 
382
//Determine thumbnail method.
383
$method = $CONFIG['thumb_method'];
384
 
385
if (($method == 'gd2') and (!function_exists('imageistruecolor'))) {
386
 
387
        //Set ignore imageistruecolor to false.
388
        $ignore = 0;
389
} else {
390
        //Set $ignore image is true color to true.
391
        $ignore = 1;
392
}
393
 
394
// Get image info.
395
$source_image_size_and_type = getimagesize($path_to_primary_image) or die($lang_image_processor_php['file_corrupt']);
396
$source_image_width = $source_image_size_and_type[0];
397
$source_image_height = $source_image_size_and_type[1];
398
$source_image_type = $source_image_size_and_type[2];
399
 
400
//We need specify the path for the transitory file.
401
 
402
        // Create a prefix for easier human recognition.
403
        $prefix = "pre_";
404
 
405
        //Set the correct file extension.
406
 
407
        if ($source_image_type == '1') {
408
                $suffix = '.gif';
409
        } elseif ($source_image_type == '2') {
410
                $suffix = '.jpg';
411
        } elseif ($source_image_type == '3') {
412
                $suffix = '.png';
413
        }
414
 
415
        // Generate the unique name.
416
 
417
        do {
418
                $seed = substr(md5(microtime().getmypid()), 0, 8);
419
                $path_to_preview_image = $preview_image_directory . $prefix . $seed . $suffix;
420
            } while (file_exists($path_to_preview_image));
421
 
422
//Now we can upload the file.
423
 
424
 
425
// Calculate dimensions.
426
 
427
if ($source_image_width > $maximum_width) {
428
 
429
        $new_width = (INTEGER) $maximum_width;
430
        $new_height = (INTEGER) ($source_image_height * ($maximum_width / $source_image_width));
431
 
432
} else {
433
 
434
        $new_width  = $source_image_width;
435
        $new_height = $source_image_height;
436
 
437
}
438
 
439
 
440
//Begin processing if GD is used.
441
 
442
if ($method == "gd2" or $method =="gd1") {
443
 
444
// Get image handle
445
$image_handle = get_handle($path_to_primary_image);
446
 
447
// Create the destination image handle.
448
 
449
if ($method == "gd2") {
450
 
451
        if ($ignore) {
452
                if (ImageIsTrueColor($image_handle)) {
453
 
454
                        $destination_image_handle = ImageCreateTrueColor($new_width, $new_height);
455
 
456
                } else {
457
 
458
                        $destination_image_handle = ImageCreate($new_width, $new_height);
459
 
460
                }
461
        } else {
462
 
463
                $destination_image_handle = ImageCreate($new_width, $new_height);
464
 
465
        }
466
 
467
 
468
} elseif ($method == "gd1") {
469
 
470
        $destination_image_handle = ImageCreate($new_width, $new_height);
471
 
472
}
473
 
474
// Resize the image
475
 
476
if ($method == "gd2") {
477
 
478
        //Use the higher quality function imagecopyresampled.
479
        imagecopyresampled($destination_image_handle, $image_handle, 0, 0, 0, 0, $new_width, $new_height, $source_image_width, $source_image_height);
480
 
481
} elseif ($method == "gd1") {
482
 
483
        //Use the lower quality imagecopyresized.
484
        imagecopyresized($destination_image_handle, $image_handle, 0, 0, 0, 0, $new_width, $new_height, $source_image_width, $source_image_height);
485
 
486
}
487
 
488
//Destroy $image_handle
489
imagedestroy($image_handle);
490
 
491
// Write the image to disk.
492
 
493
        if ($source_image_type == "2") {
494
 
495
                imagejpeg($destination_image_handle, $path_to_preview_image) or die($lang_image_processor_php['no_write']);
496
 
497
        } elseif ($source_image_type == "3") {
498
 
499
                imagepng($destination_image_handle, $path_to_preview_image) or die($lang_image_processor_php['no_write']);
500
 
501
        }
502
 
503
// Destroy $destination_image_handle.
504
imagedestroy($destination_image_handle);
505
 
506
} elseif ($method == "im") {
507
 
508
        // Set IM path.
509
        $im_path = $CONFIG['impath'];
510
 
511
        //Check the IM path for the final slash.
512
        if (eregi('/$',$im_path) or empty($im_path)) {
513
                $trailing_slash = "";
514
            } else {
515
                $trailing_slash = "/";
516
            }
517
 
518
        //Determine real paths to files.
519
        $real_path_to_primary_image = realpath($path_to_primary_image);
520
        $real_path_to_preview_image = realpath($path_to_preview_image);
521
 
522
        // Prevent the user from creating a process zombie by aborting while IM does its work.
523
        ignore_user_abort(true);
524
 
525
        // Issue the command for resizing to IM.  Have ImageMagick write the image to disk.
526
 
527
        $output = array();
528
 
529
        $cmd = "{$CONFIG['impath']}".$trailing_slash."convert -geometry {$new_width}x{$new_height} \"$real_path_to_primary_image\" \"$real_path_to_preview_image\"";
530
 
531
        exec ($cmd, $output, $retval);
532
 
533
        // Restore the user abort setting.
534
        ignore_user_abort(false);
535
 
536
        if ($retval) {
537
                $ERROR = $lang_image_processor_php['IM_Error'] . $retval;
538
                if ($CONFIG['debug_mode']) {
539
                        // Re-execute the command with the backtit operator in order to get all outputs
540
                        // will not work is safe mode is enabled
541
                        $output = `$cmd 2>&1`;
542
                        $ERROR .= "<br /><br /><div align=\"left\">{$lang_image_processor_php['cmd_line']}<br /><font size=\"2\">".nl2br(htmlspecialchars($cmd))."</font></div>";
543
                        $ERROR .= "<br /><br /><div align=\"left\">{$lang_image_processor_php['mog_said']}<br /><font size=\"2\">";
544
                        $ERROR .= nl2br(htmlspecialchars($output));
545
                        $ERROR .= "</font></div>";
546
                }
547
                die($ERROR);
548
 
549
        }
550
 
551
}
552
 
553
return $path_to_preview_image;
554
 
555
}
556
 
557
//**************************************************************************
558
 
559
function spring_cleaning($directory_path) {
560
 
561
global $lang_image_processor_php;
562
 
563
//First we get the transitory directory handle.
564
$directory_handle = opendir($directory_path) or die($lang_image_processor_php['no_open_trans_dir']);
565
 
566
// Now let's read through the directory contents.
567
while (!(($file = readdir($directory_handle)) === false)) {
568
 
569
        $dir_path = "".$directory_path."/".$file."";
570
 
571
        if (is_dir($dir_path)) {
572
 
573
                // This is a directory, so we move on.
574
                continue;
575
 
576
        }
577
 
578
        // We find out when the file was last accessed.
579
        $access_time = fileatime($dir_path);
580
 
581
        // We find out the current time.
582
        $current_time = time();
583
 
584
        // We calculate the the delete time. We will delete anything one hour old or older.
585
        $delete_time = $current_time - 3600;
586
 
587
        // Now we compare the two.
588
        if ($access_time <= $delete_time) {
589
 
590
                // The file is old. We delete it.
591
                unlink($dir_path);
592
        }
593
 
594
}
595
 
596
// Don't forget to close the directory.
597
closedir($directory_handle);
598
 
599
}
600
 
601
//**********************************************************************************
602
 
603
function make_form($next_form_action, $path_to_preview_image, $path_to_primary_image, $file_name) {
604
 
605
global $event;
606
global $album;
607
global $title;
608
global $caption;
609
global $keywords;
610
global $user1;
611
global $user2;
612
global $user3;
613
global $user4;
614
global $lang_image_processor_php;
615
 
616
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
617
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
618
header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
619
header("Cache-Control: post-check=0, pre-check=0", false);
620
header("Pragma: no-cache"); // HTTP/1.0
621
 
622
pageheader($lang_image_processor_php['page_title']);
623
 
624
print "<br><br><br>";
625
 
626
print "<center>";
627
 
628
print "<img src=\"$path_to_preview_image\" alt=\"{$lang_image_processor_php['preview_image_alt_text']}\">";
629
 
630
print "</center>";
631
 
632
print "<br>";
633
print "<br>";
634
print "<form action=\"$next_form_action\" method=\"post\">";
635
print "<input type=\"hidden\" name=\"album\" value=\"$album\">";
636
print "<input type=\"hidden\" name=\"title\" value=\"$title\">";
637
print "<input type=\"hidden\" name=\"caption\" value=\"$caption\">";
638
print "<input type=\"hidden\" name=\"keywords\" value=\"$keywords\">";
639
print "<input type=\"hidden\" name=\"user1\" value=\"$user1\">";
640
print "<input type=\"hidden\" name=\"user2\" value=\"$user2\">";
641
print "<input type=\"hidden\" name=\"user3\" value=\"$user3\">";
642
print "<input type=\"hidden\" name=\"user4\" value=\"$user4\">";
643
print "<input type=\"hidden\" name=\"event\" value=\"$event\">";
644
print "<input type=\"hidden\" name=\"file_name\" value=\"$file_name\">";
645
print "<input type=\"hidden\" name=\"transitory_image_path\" value=\"$path_to_primary_image\">";
646
print "<input type=\"hidden\" name=\"preview_image_path\" value=\"$path_to_preview_image\">";
647
 
648
print "<p>{$lang_image_processor_php['manipulation_query']}</p>";
649
 
650
print "<br>";
651
print "<input type=\"radio\" name=\"degrees\" value=\"no\" checked>{$lang_image_processor_php['no_manipulation']}&nbsp;&nbsp;&nbsp;&nbsp;";
652
print "<input type=\"radio\" name=\"degrees\" value=\"90\">90&#176;&nbsp;&nbsp;&nbsp;&nbsp;";
653
print "<input type=\"radio\" name=\"degrees\" value=\"180\">180&#176;&nbsp;&nbsp;&nbsp;&nbsp;";
654
print "<input type=\"radio\" name=\"degrees\" value=\"270\">270&#176;&nbsp;&nbsp;&nbsp;&nbsp;";
655
print "<br><br>";
656
print "<input type=\"submit\" value=\"Continue\">";
657
print "</form>";
658
 
659
pagefooter();
660
 
661
}
662
 
663
//------------------------------MAIN CODE BLOCK---------------------------
664
 
665
// Check to see if the uploader has permission to upload. close the script if he doesn't.
666
 
667
if (!USER_CAN_UPLOAD_PICTURES) cpg_die(ERROR, $lang_errors['perm_denied'], __FILE__, __LINE__);
668
 
669
// Let us define the directories where images will be temporarily stored.
670
 
671
$transitory_file_directory = "./{$CONFIG['fullpath']}manipulation/transitory/";
672
$preview_image_directory = "./{$CONFIG['fullpath']}transitory/previews/";
673
 
674
// We can also create a rudimentary language array to make integration into CPG easier at a later date.
675
$lang_image_processor_php = array('finished_manipulation'=>'You have finished manipulating the image. Please click the Proceed button to finish uploading the image.',
676
                                  'finished_manipulation_button'=>'Proceed',
677
                                  'page_title'=>'Image Manipulation',
678
                                  'preview_image_alt_text'=>'Thumbnail preview of image.',
679
                                  'manipulation_query'=>'This is how the image you selected for upload currently appears. Do you wish to rotate the image? All rotations are counterclockwise. Please note, your image has not been uploaded yet.',
680
                                  'no_manipulation'=>'No',
681
                                  'IM_Error'=>'Error executing ImageMagick - Return value: ',
682
                                  'cmd_line'=>'Command Line :',
683
                                  'mog_said'=>'The mogrify program said:',
684
                                  'file_corrupt'=>'The file is corrupt or not accessible.',
685
                                  'no_write'=>'Could not write image to disk.',
686
                                  'GD_GIF_Warning'=>'The image you submitted is in GIF format. Unfortunately, GIF images use the Unisys patented LZW compression scheme, so this program cannot work with them. Please convert your image to PNG or JPG. Then try uploading it again.',
687
                                  'not_supported'=>'The uploaded image type is not supported. Please upload JPG or PNG images.',
688
                                  'no_open_trans_dir'=>'Could not open transitory directory.',
689
                                  'no_move'=>'Couldn\'t copy',
690
                                  'bad_angle'=>'The angle submitted is unacceptable. Please try again.',
691
                                  'tampering' =>'The preview image is missing or its path has been altered.',
692
                                  'primary_tampering' => 'The primary image path is not valid, or the file does not exist.',
693
                                  'err_invalid_fext' => 'Your file extension is not valid.'
694
 
695
);
696
 
697
// Let's inspect the directories for old files, and delete them if they are too old.
698
// Old files might appear if a user upload was interrupted.
699
 
700
spring_cleaning($transitory_file_directory);
701
 
702
spring_cleaning($preview_image_directory);
703
 
704
// The directories have been tidied.
705
 
706
 
707
// We also need to set the preview thumbnail width.
708
$maximum_width = $CONFIG['thumb_width'];
709
 
710
// First, we test for the variable $degrees to determine script action.
711
 
712
if (!isset($HTTP_POST_VARS['degrees'])) {
713
 
714
        // Display initial form.
715
 
716
        // First, we must capture all the data sent to us by upload.php.
717
 
718
        $event    = $HTTP_POST_VARS['event'];
719
        $album    = (int)$HTTP_POST_VARS['album'];
720
        $title    = $HTTP_POST_VARS['title'];
721
        $caption  = $HTTP_POST_VARS['caption'];
722
        $keywords = $HTTP_POST_VARS['keywords'];
723
        $user1    = $HTTP_POST_VARS['user1'];
724
        $user2    = $HTTP_POST_VARS['user2'];
725
        $user3    = $HTTP_POST_VARS['user3'];
726
        $user4    = $HTTP_POST_VARS['user4'];
727
 
728
        // First things first. Let's analyze the image file.
729
 
730
        // We already have the file size in bytes and  the temporary name in the file
731
        // upload global array.
732
 
733
        // The file size is $HTTP_POST_FILES['userpicture']['size'].
734
 
735
        $file_size = $HTTP_POST_FILES['userpicture']['size'];
736
 
737
        // The temporary name is $HTTP_POST_FILES['userpicture']['tmp_name'].
738
 
739
        $temporary_name = $HTTP_POST_FILES['userpicture']['tmp_name'];
740
 
741
        // The file name is $HTTP_POST_FILES['userpicture']['name'].
742
 
743
        $file_name = $HTTP_POST_FILES['userpicture']['name'];
744
 
745
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
746
 
747
        // We must check the file name for security reasons.
748
 
749
        // Replace forbidden chars with underscores
750
        $matches = array();
751
        $forbidden_chars = strtr($CONFIG['forbiden_fname_char'], array('&amp;' => '&', '&quot;' => '"', '&lt;' => '<', '&gt;' => '>'));
752
 
753
        // Check that the file uploaded has a valid extension
754
        if (get_magic_quotes_gpc()) $file_name = stripslashes($file_name);
755
        $picture_name = strtr($file_name, $forbidden_chars, str_repeat('_', strlen($CONFIG['forbiden_fname_char'])));
756
        if (!preg_match("/(.+)\.(.*?)\Z/", $picture_name, $matches)){
757
                $matches[1] = 'invalid_fname';
758
                $matches[2] = 'xxx';
759
        }
760
        if ($matches[2]=='' || !stristr($CONFIG['allowed_file_extensions'], $matches[2])) {
761
            cpg_die(ERROR, $lang_image_processor_php['err_invalid_fext'], __FILE__, __LINE__);
762
        }
763
 
764
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
765
 
766
        // Now for some validation.
767
        // We check the file size of the user's file to end the program if it has no data.
768
        // We also check the temporary name to make sure it uploaded properly.
769
 
770
        if (($file_size <= 0) or ($temporary_name == '')) {
771
 
772
                // The user has submitted a corrupted file, a file without data, or the
773
                // upload failed. We return an error.
774
 
775
                cpg_die(ERROR, $lang_errors['invalid_image'], __FILE__, __LINE__);
776
        }
777
 
778
        // Now we can extract other information into the array $source_image_size_and_type.
779
        // getimagesize returns an array with 4 elements. Index 0 contains the width of
780
        // the image in pixels. Index 1 contains the height. Index 2 is a flag indicating
781
        // the type of the image: 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP,
782
        // 7 = TIFF(intel byte order), 8 = TIFF(motorola byte order), 9 = JPC, 10 = JP2,
783
        // 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF, 15 = WBMP, 16 = XBM. Index 3 is a
784
        // string for use in HTML img tags. The funtion returns FALSE if it totally fails,
785
        // so we may use a die statement to generate the error message.
786
 
787
        $source_image_size_and_type = getimagesize($temporary_name) or die($lang_image_processor_php['file_corrupt']);
788
        $source_image_width = $source_image_size_and_type[0];
789
        $source_image_height = $source_image_size_and_type[1];
790
        $source_image_type = $source_image_size_and_type[2];
791
 
792
        // getimagesize returns a width or height of zero if there is no image in the file
793
        // or if it cannot tell what it is looking at.  Let's check the width and height.
794
 
795
        if (($source_image_width <= 0) or ($source_image_height <= 0)) {
796
 
797
                // The image supplied is either not an image or is not usable. We return
798
                // an error.
799
 
800
                cpg_die(ERROR, $lang_errors['invalid_image'], __FILE__, __LINE__);
801
        }
802
 
803
        // Now that we are pretty sure this image is legitimate, we need to move it into
804
        // the transitory directory, so that we may continue to work with it.
805
 
806
        //We need specify the path for the transitory file.
807
 
808
                // Create a prefix for easier human recognition.
809
                $prefix = "trans_";
810
 
811
                //Set the correct file extension.
812
 
813
                if ($source_image_type == '1') {
814
                        $suffix = '.gif';
815
                } elseif ($source_image_type == '2') {
816
                        $suffix = '.jpg';
817
                } elseif ($source_image_type == '3') {
818
                        $suffix = '.png';
819
                }
820
 
821
                // Generate the unique name.
822
 
823
                do {
824
                        $seed = substr(md5(microtime().getmypid()), 0, 8);
825
                        $path_to_primary_image = $transitory_file_directory . $prefix . $seed . $suffix;
826
                    } while (file_exists($path_to_primary_image));
827
 
828
        //Now we can upload the file.
829
 
830
        if (is_uploaded_file($temporary_name)) {
831
                        move_uploaded_file($temporary_name, $path_to_primary_image) or die ($lang_image_processor_php['no_move']);
832
                }
833
 
834
        // The file has been uploaded to the transitory directory. At this point, we
835
        // are ready to create the form that will allow the user to rotate the image.
836
        // It requires a preview thumbnail to be loaded. We will create the thumbnail
837
        // with the image preview function, which will return the file path.
838
 
839
        $path_to_preview_image = image_preview($path_to_primary_image, $maximum_width);
840
 
841
        // Our preview thumbnail is now stored on the server. Let's create the
842
        // rotation form.
843
 
844
        make_form($_SERVER[PHP_SELF], $path_to_preview_image, $path_to_primary_image, $file_name);
845
 
846
} else {
847
 
848
        //Display secondary form.
849
 
850
        // First, we must capture all the data sent to us by the initial form.
851
 
852
        $degrees  = $HTTP_POST_VARS['degrees'];
853
        $event    = $HTTP_POST_VARS['event'];
854
        $path_to_primary_image = $HTTP_POST_VARS['transitory_image_path'];
855
        $path_to_preview_image = $HTTP_POST_VARS['preview_image_path'];
856
        $transitory_file_name = $HTTP_POST_VARS['file_name'];
857
        $album    = (int)$HTTP_POST_VARS['album'];
858
        $title    = $HTTP_POST_VARS['title'];
859
        $caption  = $HTTP_POST_VARS['caption'];
860
        $keywords = $HTTP_POST_VARS['keywords'];
861
        $user1    = $HTTP_POST_VARS['user1'];
862
        $user2    = $HTTP_POST_VARS['user2'];
863
        $user3    = $HTTP_POST_VARS['user3'];
864
        $user4    = $HTTP_POST_VARS['user4'];
865
 
866
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
867
 
868
        // We must check the file name for security reasons.
869
 
870
        // Replace forbidden chars with underscores
871
        $matches = array();
872
        $forbidden_chars = strtr($CONFIG['forbiden_fname_char'], array('&amp;' => '&', '&quot;' => '"', '&lt;' => '<', '&gt;' => '>'));
873
 
874
        // Check that the file uploaded has a valid extension
875
        if (get_magic_quotes_gpc()) $transitory_file_name = stripslashes($transitory_file_name);
876
        $picture_name = strtr($transitory_file_name, $forbidden_chars, str_repeat('_', strlen($CONFIG['forbiden_fname_char'])));
877
        if (!preg_match("/(.+)\.(.*?)\Z/", $picture_name, $matches)){
878
                $matches[1] = 'invalid_fname';
879
                $matches[2] = 'xxx';
880
        }
881
        if ($matches[2]=='' || !stristr($CONFIG['allowed_file_extensions'], $matches[2])) {
882
            cpg_die(ERROR, $lang_image_processor_php['err_invalid_fext'], __FILE__, __LINE__);
883
        }
884
 
885
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
886
 
887
        // Now let us delete the old preview image. First, we must verify the image path and preview path
888
        // have not been altered too severely.
889
 
890
        //Create the search strings.
891
 
892
        $preview_search_string = "^".$preview_image_directory."";
893
        $primary_search_string = "^".$transitory_file_directory."";
894
 
895
        //Check the preview image path and delete if it passes the test.
896
 
897
        if ((ereg($preview_search_string, $path_to_preview_image)) and (file_exists($path_to_preview_image))) {
898
 
899
                // It is safe to delete the preview image.
900
                unlink($path_to_preview_image);
901
 
902
        } else {
903
 
904
                // The supplied preview path is invalid. The image is either missing, or someone has tampered with the HTML request.
905
                cpg_die(ERROR, $lang_image_processor_php['tampering'], __FILE__, __LINE__);
906
 
907
        }
908
 
909
        //Check the primary image path.
910
 
911
        if ((!(ereg($primary_search_string, $path_to_primary_image))) or (!(file_exists($path_to_primary_image)))) {
912
 
913
                // The primary image path is not valid, or the file does not exist.
914
                cpg_die(ERROR, $lang_image_processor_php['primary_tampering'], __FILE__, __LINE__);
915
 
916
        }
917
 
918
 
919
        // In this instance, $degrees will tell us everything we wish to know about what
920
        // to do next. Let's validate it.
921
 
922
        // We will only accept four values: 90, 180, 270, and no. Let's check
923
        // the user input:
924
 
925
        if (!(($degrees == "90") or ($degrees == "180") or ($degrees == "270") or ($degrees == "no"))) {
926
 
927
                //The user is submitting incorrect values. We generate an error.
928
 
929
                cpg_die(CRITICAL_ERROR, $lang_image_processor_php['bad_angle'], __FILE__, __LINE__);
930
 
931
        }
932
 
933
        // Now that we have validated, let's analyze what we have.
934
 
935
        if ($degrees == "no") {
936
 
937
                // The user has finished modifiying the image.
938
 
939
                // We create a hidden form that thanks the user, and passes
940
                // the information to db_input.
941
 
942
                header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
943
                header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
944
                header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
945
                header("Cache-Control: post-check=0, pre-check=0", false);
946
                header("Pragma: no-cache"); // HTTP/1.0
947
 
948
                pageheader($lang_image_processor_php['page_title']);
949
 
950
                print "<br><br><br>";
951
 
952
                print "<form action=\"db_input.php\" method=\"post\">";
953
                print "<input type=\"hidden\" name=\"album\" value=\"$album\">";
954
                print "<input type=\"hidden\" name=\"title\" value=\"$title\">";
955
                print "<input type=\"hidden\" name=\"caption\" value=\"$caption\">";
956
                print "<input type=\"hidden\" name=\"keywords\" value=\"$keywords\">";
957
                print "<input type=\"hidden\" name=\"user1\" value=\"$user1\">";
958
                print "<input type=\"hidden\" name=\"user2\" value=\"$user2\">";
959
                print "<input type=\"hidden\" name=\"user3\" value=\"$user3\">";
960
                print "<input type=\"hidden\" name=\"user4\" value=\"$user4\">";
961
                print "<input type=\"hidden\" name=\"event\" value=\"$event\">";
962
                print "<input type=\"hidden\" name=\"transitory_image_path\" value=\"$path_to_primary_image\">";
963
                print "<input type=\"hidden\" name=\"file_name\" value=\"$transitory_file_name\">";
964
 
965
                print "<p>{$lang_image_processor_php['finished_manipulation']}</p>";
966
 
967
                print "<br>";
968
 
969
                print "<input type=\"submit\" value=\"{$lang_image_processor_php['finished_manipulation_button']}\">";
970
                print "</form>";
971
 
972
                pagefooter();
973
 
974
        } else {
975
 
976
                // The user desires to rotate the image.
977
 
978
                // We use the rotate image function.
979
                rotate_image($path_to_primary_image, $degrees);
980
 
981
                // Get width and height here.
982
 
983
                $source_image_size_and_type = getimagesize($path_to_primary_image) or die($lang_image_processor_php['file_corrupt']);
984
                $source_image_width = $source_image_size_and_type[0];
985
                $source_image_height = $source_image_size_and_type[1];
986
                $source_image_type = $source_image_size_and_type[2];
987
 
988
                // Now we have to create the preview thumbnail.
989
 
990
                // The file has been rotated in the transitory directory. At this point, we
991
                // are ready to create the form that will allow the user to rotate the image.
992
                // It requires a preview thumbnail to be loaded. We will use the image_preview
993
                // function.
994
 
995
                $path_to_preview_image = image_preview($path_to_primary_image, $maximum_width);
996
 
997
                // Our preview thumbnail is now on the server. Let's create the
998
                // rotation form.
999
 
1000
                make_form($_SERVER[PHP_SELF], $path_to_preview_image, $path_to_primary_image, $transitory_file_name);
1001
 
1002
        }
1003
 
1004
}
1005
 
1006
?>