-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_thumbnail.php
More file actions
executable file
·145 lines (126 loc) · 2.94 KB
/
create_thumbnail.php
File metadata and controls
executable file
·145 lines (126 loc) · 2.94 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
<?php
include 'settings.inc.php';
require "{$composer_dir}autoload.php";
include 'aws_cache.php';
ksort($aws_cache);
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
$credentials = new Aws\Credentials\Credentials($aws_key, $aws_secret);
// Instantiate the client.
$s3 = new S3Client([
'credentials' => $credentials,
'version' => 'latest',
'region' => $aws_region,
]);
/**
* ensure we have a file
*/
if (
(empty($_GET['file']))
||
(empty($aws_cache[ $_GET['file'] ]))
){
echo "<h1>Sorry, looks like I don't have anything to show you.";
die;
}
/**
* get cache directory
*/
$cache_dir = dirname(__FILE__) . '/.cache';
/**
* get file info
* //note we save file_name for easy use and we want to name each thumbnail-in-progress a different name so as not to overwrite
*/
$file_name = $aws_cache[ $_GET['file'] ]['file_name'];
if (
(!$file_extension = pathinfo($file_name))
||
(!is_array($file_extension))
||
(empty($file_extension['extension']))
||
(!$file_extension = strtolower($file_extension['extension']))
){
echo '<h1>Failed to get file path info.</h1>';
die;
}
/**
* convert jpeg to jpg
*/
$file_extension = str_ireplace('e', '', $file_extension);
if (
($file_extension != 'jpg')
&&
($file_extension != 'png')
){
echo "{$file_extension} is an unsupported format.";
die;
}
/**
* ensure we have .cache dir
*/
if (
(!is_dir($cache_dir))
){
echo '<h1>Cache directory does not exist.</h1>';
die;
}
/**
* check if thumbnail already exists
*/
if (
(empty($_GET['force']))
&&
(file_exists("{$cache_dir}/{$file_name}"))
){
echo '<h1>Thumbnail alrady exists, use force if you wish to force the reload.</h1>';
die;
}
/**
* try/catch to get file content
*/
try {
$file = $s3->getObject([
'Bucket' => $aws_bucket,
'Key' => $aws_cache[ $_GET['file'] ]['file_name'],
'SaveAs' => "{$cache_dir}/{$file_name}",
]);
} catch (Exception $exception) {
echo "Failed to download $file_name from $bucket_name with error: " . $exception->getMessage();
exit("Please fix error with file downloading before continuing.");
}
// Maximum width and height
$width = 250;
$height = 250;
// Get new dimensions
list($width_orig, $height_orig) = getimagesize("{$cache_dir}/{$file_name}");
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
/**
* init and resamble image
*/
$image_p = imagecreatetruecolor($width, $height);
if ($file_extension == 'jpg'){
$image = imagecreatefromjpeg("{$cache_dir}/{$file_name}");
}
if ($file_extension == 'png'){
$image = imagecreatefrompng("{$cache_dir}/{$file_name}");
}
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
/**
* write image to file
*/
if ($file_extension == 'jpg'){
imagejpeg($image_p, "{$cache_dir}/{$file_name}", 90);
}
if ($file_extension == 'png'){
imagepng($image_p, "{$cache_dir}/{$file_name}", 90);
}
/**
* done
*/
echo 'Done';