-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.php
186 lines (155 loc) · 5.54 KB
/
index.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
<?php
###
# @name Generate Missing Medium Photos
# @author Quentin Bramas
# @copyright 2016 by Quentin Bramas
# @description This file creates the missing medium photos.
# maximum number of photos processed (be careful to avoid timeout)
$maxPhoto = 4;
# FROM LYCHEE
# Size of the medium-photo
# When changing these values,
# also change the size detection in the front-end
$newWidth = 1920;
$newHeight = 1080;
$quality = 98;
use Mysqli;
use Lychee\Modules\Database;
use Lychee\Modules\Settings;
$lychee = __DIR__ . '/../../';
$startTime = microtime(true);
require($lychee . 'php/define.php');
require($lychee . 'php/autoload.php');
require($lychee . 'php/helpers/hasPermissions.php');
# Set content
header('content-type: text/plain');
# Load config
if (!file_exists(LYCHEE_CONFIG_FILE)) exit('Error 001: Configuration not found. Please install Lychee first.');
require(LYCHEE_CONFIG_FILE);
# Declare
$result = '';
# Load settings
$settings = new Settings();
$settings = $settings->get();
# Ensure that user is logged in
session_start();
if ((isset($_SESSION['login'])&&$_SESSION['login']===true)&&
(isset($_SESSION['identifier'])&&$_SESSION['identifier']===$settings['identifier'])) {
# Function taken from Lychee Photo Module
function createMedium($url, $filename, $type, $width, $height) {
# Function creates a smaller version of a photo when its size is bigger than a preset size
# Excepts the following:
# (string) $url = Path to the photo-file
# (string) $filename = Name of the photo-file
# (int) $width = Width of the photo
# (int) $height = Height of the photo
# Returns the following
# (boolean) true = Success
# (boolean) false = Failure
# Set to true when creation of medium-photo failed
global $settings;
$error = false;
# Size of the medium-photo
# When changing these values,
# also change the size detection in the front-end
global $newWidth;
global $newHeight;
# Check permissions
if (hasPermissions(LYCHEE_UPLOADS_MEDIUM)===false) {
# Permissions are missing
$error = true;
echo 'Not enough persmission on the medium folder'."\n";
}
# Is photo big enough?
# Is Imagick installed and activated?
if (($error===false)&&($width>$newWidth||$height>$newHeight)){
if((extension_loaded('imagick')&&$settings['imagick']==='1')) {
$newUrl = LYCHEE_UPLOADS_MEDIUM . $filename;
# Read image
$medium = new Imagick();
$medium->readImage(LYCHEE.$url);
# Adjust image
$medium->scaleImage($newWidth, $newHeight, true);
# Save image
try { $medium->writeImage($newUrl); }
catch (ImagickException $err) {
Log::notice($database, __METHOD__, __LINE__, 'Could not save medium-photo: ' . $err->getMessage());
$error = true;
echo 'Imagick Exception:'."\n";
var_dump($e);
}
$medium->clear();
$medium->destroy();
} else {
$newUrl = LYCHEE_UPLOADS_MEDIUM . $filename;
# Read image
$newHeight = $newWidth/($width/$height);
$medium = imagecreatetruecolor($newWidth, $newHeight);
// Create new image
switch($type) {
case 'image/jpeg': $sourceImg = imagecreatefromjpeg(LYCHEE.$url); break;
case 'image/png': $sourceImg = imagecreatefrompng(LYCHEE.$url); break;
case 'image/gif': $sourceImg = imagecreatefromgif(LYCHEE.$url); break;
default: Log::notice($database, __METHOD__, __LINE__, 'Type of photo is not supported: ' . $filename . ' of type ' . $type);
return false;
break;
}
// Create retina thumb
imagecopyresampled($medium, $sourceImg, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagejpeg($medium, $newUrl, $quality);
imagedestroy($medium);
// Free memory
imagedestroy($sourceImg);
}
} else {
# Photo too small or
# Imagick not installed
$error = true;
}
if($error === true) {
return false;
}
return true;
}
function getAllPhotos() {
# Functions returns the list of photos
global $newWidth;
global $newHeight;
# Get photos that do not have a medium size photo
$query = Database::prepare(Database::get(), "SELECT id, width, height, url, medium, type FROM ? WHERE medium=0 AND (width > ? OR height > ?)", array(LYCHEE_TABLE_PHOTOS, $newWidth, $newHeight));
$photos = Database::get()->query($query);
$data = array();
while ($photo = $photos->fetch_assoc()) { # Parse photo
$photo['filename'] = $photo['url'];
$photo['url'] = LYCHEE_URL_UPLOADS_BIG . $photo['url'];
$data[] = $photo;
}
return $data;
}
$photos = getAllPhotos();
if(empty($photos)) {
exit('done :)');
}
# for each photo we create the medium size photo
# when reached the maximum number of photo, we reload the page
foreach($photos as $photo) {
if(createMedium($photo['url'], $photo['filename'], $photo['type'], $photo['width'], $photo['height'])) {
$query = Database::prepare(Database::get(), "UPDATE ? SET medium=1 WHERE id=?", array(LYCHEE_TABLE_PHOTOS, $photo['id']));
$result = Database::get()->query($query);
echo 'success: '.$photo['id']. ' '.$photo['filename'] . "\n";
}
else {
exit('error: '.$photo['id'] . ' '.$photo['filename']);
}
$maxPhoto -= 1;
if($maxPhoto == 0 || count($photos) - (4 - $maxPhoto) == 0) {
header("Refresh:0");
exit((count($photos) - (4 - $maxPhoto)) . ' photos remaining');
}
}
} else {
# Don't go further if the user is not logged in
echo('You have to be logged in to see the log.');
exit();
}
?>