forked from leonardoxc/leonardoxc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CL_image.php
567 lines (451 loc) · 17.7 KB
/
CL_image.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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
<?
//************************************************************************
// Leonardo XC Server, https://github.com/leonardoxc/leonardoxc
//
// Copyright (c) 2004-2010 by Andreadakis Manolis
//
// This program is free software. You can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License.
//
// $Id: CL_image.php,v 1.7 2010/08/20 13:58:28 manolis Exp $
//
//************************************************************************
class CLimage {
function CLimage() {
}
function validJPGfilename($filename) {
$filename=strtolower($filename);
if ( strtolower(substr($filename,-4))==".jpg" || strtolower(substr($filename,-5))==".jpeg" ) return 1;
return 0;
}
function validJPGfile($filename) {
$im = getimagesize($filename);
if ($im[0] && $im[1]) return 1;
else return 0;
}
function getJPG_NewSize($forcedwidth, $forcedheight, $source_width, $source_height)
{
$dest_width_max = $forcedwidth;
$dest_height_max = $forcedheight;
// The two lines beginning with (int) are the super important magic formula part.
(int)$dest_width = ($source_width <= $source_height) ? round(($source_width * $dest_height_max)/$source_height) : $dest_width_max;
(int)$dest_height = ($source_width > $source_height) ? round(($source_height * $dest_width_max) /$source_width) : $dest_height_max;
if ($dest_width > $source_width ) {
$dest_width = $source_width;
$dest_height = $source_height;
}
return array($dest_width,$dest_height);
}
function resizeJPG($forcedwidth, $forcedheight, $sourcefile, $destfile, $imgcomp)
{
$g_imgcomp=100-$imgcomp;
$g_srcfile=$sourcefile;
$g_dstfile=$destfile;
$g_fw=$forcedwidth;
$g_fh=$forcedheight;
if(!file_exists($g_srcfile)) {
return false;
}
$image_details = getimagesize($g_srcfile);
$source_width = $image_details[0];
$source_height = $image_details[1];
$dest_width_max = $forcedwidth;
$dest_height_max = $forcedheight;
// The two lines beginning with (int) are the super important magic formula part.
(int)$dest_width = ($source_width <= $source_height) ? round(($source_width * $dest_height_max)/$source_height) : $dest_width_max;
(int)$dest_height = ($source_width > $source_height) ? round(($source_height * $dest_width_max) /$source_width) : $dest_height_max;
if ($dest_width > $source_width ) {
if ( $sourcefile == $destfile) {
// if the current image is small enough,
// and is an overright of it's self
// dont do anything
return true;
}
$dest_width = $source_width;
$dest_height = $source_height;
}
require_once dirname(__FILE__).'/lib/pel/PelJpeg.php';
/* The input file is now loaded into a PelJpeg object. */
$input_jpeg = new PelJpeg($sourcefile);
/* The input image is already loaded, so we can reuse the bytes stored
* in $input_jpeg when creating the Image resource. */
$img_src = ImageCreateFromString($input_jpeg->getBytes());
// instead of
//$img_src=imagecreatefromjpeg($g_srcfile);
if (function_exists("gd_info")) {
$gdinfo=gd_info();
if ( strpos($gdinfo["GD Version"],"2.") ===false ) $gd2=0;
else $gd2=1;
} else $gd2=false;
if ( $gd2 ) {
$img_dst=imagecreatetruecolor($dest_width,$dest_height);
imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $dest_width, $dest_height, $source_width, $source_height);
} else {
$img_dst=imagecreate($dest_width,$dest_height);
imagecopyresized($img_dst, $img_src, 0, 0, 0, 0, $dest_width, $dest_height, $source_width, $source_height);
}
/* We want the raw JPEG data from $img_dst. Luckily, one can create a
* PelJpeg object from an image resource directly: */
$output_jpeg = new PelJpeg($img_dst,$g_imgcomp);
/* Retrieve the original Exif data in $jpeg (if any). */
$exif = $input_jpeg->getExif();
/* If no Exif data was present, then $exif is null. */
if ($exif != null)
$output_jpeg->setExif($exif);
/* We can now save the scaled image. */
file_put_contents($destfile, $output_jpeg->getBytes());
// instead of
// imagejpeg($img_dst, $g_dstfile, $g_imgcomp);
//$exif->writeImage($g_dstfile.'.exif.jpg');
imagedestroy($img_dst);
return true;
}
/******************************************************************************
*
* Function: get_jpeg_header_data
*
* Description: Reads all the JPEG header segments from an JPEG image file into an
* array
*
* Parameters: filename - the filename of the file to JPEG file to read
*
* Returns: headerdata - Array of JPEG header segments
* FALSE - if headers could not be read
*
******************************************************************************/
function getExifData( $filename )
{
// prevent refresh from aborting file operations and hosing file
ignore_user_abort(true);
// Attempt to open the jpeg file - the at symbol supresses the error message about
// not being able to open files. The file_exists would have been used, but it
// does not work with files fetched over http or ftp.
$filehnd = @fopen($filename, 'rb');
// Check if the file opened successfully
if ( ! $filehnd )
{
// Could't open the file - exit
echo "<p>Could not open file $filename</p>\n";
return FALSE;
}
// Read the first two characters
$data = fread( $filehnd, 2 );
// Check that the first two characters are 0xFF 0xDA (SOI - Start of image)
if ( $data != "\xFF\xD8" )
{
// No SOI (FF D8) at start of file - This probably isn't a JPEG file - close file and return;
echo "<p>This probably is not a JPEG file</p>\n";
fclose($filehnd);
return FALSE;
}
// Read the third character
$data = fread( $filehnd, 2 );
// Check that the third character is 0xFF (Start of first segment header)
if ( $data{0} != "\xFF" )
{
// NO FF found - close file and return - JPEG is probably corrupted
fclose($filehnd);
return FALSE;
}
// Flag that we havent yet hit the compressed image data
$hit_compressed_image_data = FALSE;
// Cycle through the file until, one of: 1) an EOI (End of image) marker is hit,
// 2) we have hit the compressed image data (no more headers are allowed after data)
// 3) or end of file is hit
while ( ( $data{1} != "\xD9" ) && (! $hit_compressed_image_data) && ( ! feof( $filehnd ) ))
{
// Found a segment to look at.
// Check that the segment marker is not a Restart marker - restart markers don't have size or data after them
if ( ( ord($data{1}) < 0xD0 ) || ( ord($data{1}) > 0xD7 ) )
{
// Segment isn't a Restart marker
// Read the next two bytes (size)
$sizestr = fread( $filehnd, 2 );
// convert the size bytes to an integer
$decodedsize = unpack ("nsize", $sizestr);
// Save the start position of the data
$segdatastart = ftell( $filehnd );
// Read the segment data with length indicated by the previously read size
$segdata = fread( $filehnd, $decodedsize['size'] - 2 );
// Store the segment information in the output array
$headerdata[] = array( "SegType" => ord($data{1}),
"SegName" => $GLOBALS[ "JPEG_Segment_Names" ][ ord($data{1}) ],
"SegDesc" => $GLOBALS[ "JPEG_Segment_Descriptions" ][ ord($data{1}) ],
"SegDataStart" => $segdatastart,
"SegData" => $segdata );
}
// If this is a SOS (Start Of Scan) segment, then there is no more header data - the compressed image data follows
if ( $data{1} == "\xDA" )
{
// Flag that we have hit the compressed image data - exit loop as no more headers available.
$hit_compressed_image_data = TRUE;
}
else
{
// Not an SOS - Read the next two bytes - should be the segment marker for the next segment
$data = fread( $filehnd, 2 );
// Check that the first byte of the two is 0xFF as it should be for a marker
if ( $data{0} != "\xFF" )
{
// NO FF found - close file and return - JPEG is probably corrupted
fclose($filehnd);
return FALSE;
}
}
}
// Close File
fclose($filehnd);
// Alow the user to abort from now on
ignore_user_abort(false);
// Return the header data retrieved
return $headerdata;
}
/******************************************************************************
*
* Function: put_jpeg_header_data
*
* Description: Writes JPEG header data into a JPEG file. Takes an array in the
* same format as from get_jpeg_header_data, and combines it with
* the image data of an existing JPEG file, to create a new JPEG file
* WARNING: As this function will replace all JPEG headers,
* including SOF etc, it is best to read the jpeg headers
* from a file, alter them, then put them back on the same
* file. If a SOF segment wer to be transfered from one
* file to another, the image could become unreadable unless
* the images were idenical size and configuration
*
*
* Parameters: old_filename - the JPEG file from which the image data will be retrieved
* new_filename - the name of the new JPEG to create (can be same as old_filename)
* jpeg_header_data - a JPEG header data array in the same format
* as from get_jpeg_header_data
*
* Returns: TRUE - on Success
* FALSE - on Failure
*
******************************************************************************/
function putExifData( $old_filename, $new_filename, $jpeg_header_data )
{
// Change: added check to ensure data exists, as of revision 1.10
// Check if the data to be written exists
if ( $jpeg_header_data == FALSE )
{
// Data to be written not valid - abort
return FALSE;
}
// extract the compressed image data from the old file
$compressed_image_data = CLimage::getJpegData( $old_filename );
// Check if the extraction worked
if ( ( $compressed_image_data === FALSE ) || ( $compressed_image_data === NULL ) )
{
// Couldn't get image data from old file
return FALSE;
}
// Cycle through new headers
foreach ($jpeg_header_data as $segno => $segment)
{
// Check that this header is smaller than the maximum size
if ( strlen($segment['SegData']) > 0xfffd )
{
// Could't open the file - exit
echo "<p>A Header is too large to fit in JPEG segment</p>\n";
return FALSE;
}
}
ignore_user_abort(true); ## prevent refresh from aborting file operations and hosing file
// Attempt to open the new jpeg file
$newfilehnd = @fopen($new_filename, 'wb');
// Check if the file opened successfully
if ( ! $newfilehnd )
{
// Could't open the file - exit
echo "<p>Could not open file $new_filename</p>\n";
return FALSE;
}
// Write SOI
fwrite( $newfilehnd, "\xFF\xD8" );
// Cycle through new headers, writing them to the new file
foreach ($jpeg_header_data as $segno => $segment)
{
// Write segment marker
fwrite( $newfilehnd, sprintf( "\xFF%c", $segment['SegType'] ) );
// Write segment size
fwrite( $newfilehnd, pack( "n", strlen($segment['SegData']) + 2 ) );
// Write segment data
fwrite( $newfilehnd, $segment['SegData'] );
}
// Write the compressed image data
fwrite( $newfilehnd, $compressed_image_data );
// Write EOI
fwrite( $newfilehnd, "\xFF\xD9" );
// Close File
fclose($newfilehnd);
// Alow the user to abort from now on
ignore_user_abort(false);
return TRUE;
}
/******************************************************************************
* End of Function: put_jpeg_header_data
******************************************************************************/
/******************************************************************************
*
* Function: get_jpeg_image_data
*
* Description: Retrieves the compressed image data part of the JPEG file
*
* Parameters: filename - the filename of the JPEG file to read
*
* Returns: compressed_data - A string containing the compressed data
* FALSE - if retrieval failed
*
******************************************************************************/
function getJpegData( $filename )
{
// prevent refresh from aborting file operations and hosing file
ignore_user_abort(true);
// Attempt to open the jpeg file
$filehnd = @fopen($filename, 'rb');
// Check if the file opened successfully
if ( ! $filehnd )
{
// Could't open the file - exit
return FALSE;
}
// Read the first two characters
$data = fread( $filehnd, 2 );
// Check that the first two characters are 0xFF 0xDA (SOI - Start of image)
if ( $data != "\xFF\xD8" )
{
// No SOI (FF D8) at start of file - close file and return;
fclose($filehnd);
return FALSE;
}
// Read the third character
$data = fread( $filehnd, 2 );
// Check that the third character is 0xFF (Start of first segment header)
if ( $data{0} != "\xFF" )
{
// NO FF found - close file and return
fclose($filehnd);
return;
}
// Flag that we havent yet hit the compressed image data
$hit_compressed_image_data = FALSE;
// Cycle through the file until, one of: 1) an EOI (End of image) marker is hit,
// 2) we have hit the compressed image data (no more headers are allowed after data)
// 3) or end of file is hit
while ( ( $data{1} != "\xD9" ) && (! $hit_compressed_image_data) && ( ! feof( $filehnd ) ))
{
// Found a segment to look at.
// Check that the segment marker is not a Restart marker - restart markers don't have size or data after them
if ( ( ord($data{1}) < 0xD0 ) || ( ord($data{1}) > 0xD7 ) )
{
// Segment isn't a Restart marker
// Read the next two bytes (size)
$sizestr = fread( $filehnd, 2 );
// convert the size bytes to an integer
$decodedsize = unpack ("nsize", $sizestr);
// Read the segment data with length indicated by the previously read size
$segdata = fread( $filehnd, $decodedsize['size'] - 2 );
}
// If this is a SOS (Start Of Scan) segment, then there is no more header data - the compressed image data follows
if ( $data{1} == "\xDA" )
{
// Flag that we have hit the compressed image data - exit loop after reading the data
$hit_compressed_image_data = TRUE;
// read the rest of the file in
// Can't use the filesize function to work out
// how much to read, as it won't work for files being read by http or ftp
// So instead read 1Mb at a time till EOF
$compressed_data = "";
do
{
$compressed_data .= fread( $filehnd, 1048576 );
} while( ! feof( $filehnd ) );
// Strip off EOI and anything after
$EOI_pos = strpos( $compressed_data, "\xFF\xD9" );
$compressed_data = substr( $compressed_data, 0, $EOI_pos );
}
else
{
// Not an SOS - Read the next two bytes - should be the segment marker for the next segment
$data = fread( $filehnd, 2 );
// Check that the first byte of the two is 0xFF as it should be for a marker
if ( $data{0} != "\xFF" )
{
// Problem - NO FF foundclose file and return";
fclose($filehnd);
return;
}
}
}
// Close File
fclose($filehnd);
// Alow the user to abort from now on
ignore_user_abort(false);
// Return the compressed data if it was found
if ( $hit_compressed_image_data )
{
return $compressed_data;
}
else
{
return FALSE;
}
}
/**
* Returns GPS latitude & longitude as decimal values
**/
function getGPS($image) {
$exif = exif_read_data($image, 0, true);
if ($exif){
$lat = $exif['GPS']['GPSLatitude'];
$log = $exif['GPS']['GPSLongitude'];
if ($lat && $log) {
// latitude values //
$lat_degrees = CLimage::divide($lat[0]);
$lat_minutes = CLimage::divide($lat[1]);
$lat_seconds = CLimage::divide($lat[2]);
$lat_hemi = $exif['GPS']['GPSLatitudeRef'];
// longitude values //
$log_degrees = CLimage::divide($log[0]);
$log_minutes = CLimage::divide($log[1]);
$log_seconds = CLimage::divide($log[2]);
$log_hemi = $exif['GPS']['GPSLongitudeRef'];
$lat_decimal = CLimage::toDecimal($lat_degrees, $lat_minutes, $lat_seconds, $lat_hemi);
$log_decimal = CLimage::toDecimal($log_degrees, $log_minutes, $log_seconds, $log_hemi);
}
// now the time:
// warning the time is usually in local time....
$tm=$exif['EXIF']['DateTimeOriginal'];
if ($tm)
$tm=strtotime($tm);
else
$tm=$exif['FILE']['FileDateTime'];
return array($lat_decimal, $log_decimal,$tm);
} else{
return array(0,0,0);
}
}
function toDecimal($deg, $min, $sec, $hemi)
{
$d = $deg + $min/60 + $sec/3600;
return ($hemi=='S' || $hemi=='W') ? $d*=-1 : $d;
}
function divide($a)
{
// evaluate the string fraction and return a float //
$e = explode('/', $a);
// prevent division by zero //
if (!$e[0] || !$e[1]) {
return 0;
} else{
return $e[0] / $e[1];
}
}
/******************************************************************************
* End of Function: get_jpeg_image_data
******************************************************************************/
}
?>