-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimages.php
134 lines (112 loc) · 3.26 KB
/
images.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
<?php
require 'KLogger.php';
$pathToImages = 'images/';
$pathToThumbs = 'thumbnails/';
$thumbWidth = 200;
$log = KLogger::instance('.',KLogger::INFO);
function delete_folder($path)
{
global $log;
$log->logInfo("About to delete folder " . $path);
$files = glob($path . '*'); // get all file names
foreach($files as $file){ // iterate files
if(is_file($file))
unlink($file); // delete file
}
}
function images_delete( $request )
{
global $pathToImages;
global $pathToThumbs;
delete_folder($pathToThumbs);
delete_folder($pathToImages);
}
function images_get( $request){
global $pathToImages;
global $pathToThumbs;
global $thumbWidth;
create_thumbs($pathToImages, $pathToThumbs, $thumbWidth);
create_gallery($pathToImages, $pathToThumbs);
}
function images_error($request){
echo "Error 501";
}
function create_thumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
global $log;
$log->logInfo('About to create thumbs');
// open the directory
$dir = opendir( $pathToImages );
//Find all existing thumbs
$existingThumbs = glob($pathToThumbs . '*.jpg');
$log->logDebug("Thumbs found in thumbs folder " . implode(',',$existingThumbs));
// loop through it, looking for any/all JPG files:
while (false !== ($fname = readdir( $dir ))) {
if (in_array($pathToThumbs . $fname,$existingThumbs)){
$log->logDebug("Thumbnail with name " . $fname . " already exists. Skipping");
//The thumbnail was already created
continue;
}
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'jpg' )
{
// load image and get image size
$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
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
$log->logDebug('Creating thumb with name ' . $fname);
}
}
// close the directory
closedir( $dir );
}
class Image{
public $thumbnail;
public $image;
}
function create_gallery( $pathToImages, $pathToThumbs )
{
$images = [];
$dir = opendir( $pathToThumbs );
$counter = 0;
while (false !== ($fname = readdir($dir)))
{
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'jpg' && $fname != '.' && $fname != '..')
{
$img = new Image();
$img->image = $pathToImages . $fname;
$img->thumbnail = $pathToThumbs . $fname;
$counter += 1;
array_push($images,$img);
}
}
closedir( $dir );
echo json_encode($images);
}
$method = $_SERVER['REQUEST_METHOD'];
$request = explode("/",substr(@$_SERVER['PATH_INFO'],1));
switch ($method){
case 'GET':
images_get($request);
break;
case 'DELETE':
images_delete($request);
break;
default:
images_error($request);
break;
}
?>