This repository was archived by the owner on Sep 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdir.php
136 lines (113 loc) · 3.73 KB
/
dir.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
<?php
/**** NOTE: This script only accepts JPG images ****/
ini_set('memory_limit', '-1');
set_time_limit(60*5);
$path = './images/';
$recs = array();
function image_fix_orientation($filename) {
/* some cameras add meta data to change orientation. this corrects and removes that metadata */
if (is_file($filename)) {
$exif = exif_read_data($filename);
if (!empty($exif['Orientation'])) {
switch ($exif['Orientation']) {
case 3:
$image = imagerotate($filename, 180, 0);
break;
case 6:
$image = imagerotate($filename, -90, 0);
break;
case 8:
$image = imagerotate($filename, 90, 0);
break;
}
// replicate and overwrite image to remove exif data
$img = imagecreatefromjpeg ($filename);
imagejpeg ($img, $filename, 100);
imagedestroy ($img);
}
}
}
function check_resize($filename, $nWidth) {
/* resizes big pictures to a max width */
if (is_file($filename)) {
list($width, $height, $type, $attr) = getimagesize($filename);
if ($width > 800) {
$img = imagecreatefromjpeg($filename);
$w = imagesx( $img );
$h = imagesy( $img );
// calculate thumbnail size
$new_width = $nWidth;
$new_height = floor( $h * ( $nWidth / $w ) );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $w, $h );
// save thumbnail into a file with highest quality
imagejpeg( $tmp_img, $filename, 100 );
}
}
}
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth ) {
/* something to create thumbs of bigger pictures */
$dir = opendir( $pathToImages );
while (false !== ($fname = readdir( $dir ))) {
if ($fname == '.' || $fname == '..') continue;
// modify the original pictures if needed with orientation and resizing
// the 800 below is the width of the picture
image_fix_orientation($pathToImages.$fname);
check_resize($pathToImages.$fname, 800);
if (is_file($pathToImages."thumbs/".$fname)) continue; // skip ones already created
$ext = pathinfo($fname, PATHINFO_EXTENSION);
// continue only if this is a JPEG image
if ( strtolower($ext) == 'jpg' ) {
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save thumbnail into a file with highest quality
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}", 100 );
}
}
// close the directory
closedir( $dir );
}
// create any thumbnails for newly added pictures
createThumbs($path,$path."thumbs/",100);
// start getting image data to return to the page
if (is_dir($path)) {
// use scandir vs readdir because it reads alphabetically
$cdir = scandir($path);
foreach ($cdir as $entry) {
if ($entry == '.' || $entry == '..') continue;
$entry = $path . $entry;
if (is_file($entry)) {
$filetype = pathinfo($entry, PATHINFO_EXTENSION);
$rec = array(
"path" => $entry,
"name" => basename($entry),
"filetype" => $filetype,
"thumb" => $path."thumbs/".basename($entry)
);
array_push($recs, $rec);
}
}
}
// build the json result and send it
$result = array();
$success = true;
if (!empty($errorText)) {
$success = false;
$result['error'] = $errorText;
} else {
$result['total'] = count($recs);
$result['payload'] = $recs;
}
$result['success'] = $success;
$result = json_encode($result);
echo $result;