-
Notifications
You must be signed in to change notification settings - Fork 259
/
graphics.php
413 lines (370 loc) · 14.1 KB
/
graphics.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
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2009 Bharat Mediratta
*
* 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, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class graphics_Core {
private static $init;
/**
* Add a new graphics rule.
*
* Rules are applied to targets (thumbnails and resizes) in priority order. Rules are functions
* in the graphics class. So for example, the following rule:
*
* graphics::add_rule("gallery", "thumb", "resize",
* array("width" => 200, "height" => 200, "master" => Image::AUTO), 100);
*
* Specifies that "gallery" is adding a rule to resize thumbnails down to a max of 200px on
* the longest side. The gallery module adds default rules at a priority of 100. You can set
* higher and lower priorities to perform operations before or after this fires.
*
* @param string $module_name the module that added the rule
* @param string $target the target for this operation ("thumb" or "resize")
* @param string $operation the name of the operation
* @param array $args arguments to the operation
* @param integer $priority the priority for this rule (lower priorities are run first)
*/
static function add_rule($module_name, $target, $operation, $args, $priority) {
$rule = ORM::factory("graphics_rule");
$rule->module_name = $module_name;
$rule->target = $target;
$rule->operation = $operation;
$rule->priority = $priority;
$rule->args = serialize($args);
$rule->active = true;
$rule->save();
self::mark_dirty($target == "thumb", $target == "resize");
}
/**
* Remove any matching graphics rules
* @param string $module_name the module that added the rule
* @param string $target the target for this operation ("thumb" or "resize")
* @param string $operation the name of the operation
*/
static function remove_rule($module_name, $target, $operation) {
ORM::factory("graphics_rule")
->where("module_name", $module_name)
->where("target", $target)
->where("operation", $operation)
->delete_all();
self::mark_dirty($target == "thumb", $target == "resize");
}
/**
* Remove all rules for this module
* @param string $module_name
*/
static function remove_rules($module_name) {
$status = Database::instance()->delete("graphics_rules", array("module_name" => $module_name));
if (count($status)) {
self::mark_dirty(true, true);
}
}
/**
* Activate the rules for this module, typically done when the module itself is deactivated.
* Note that this does not mark images as dirty so that if you deactivate and reactivate a
* module it won't cause all of your images to suddenly require a rebuild.
*/
static function activate_rules($module_name) {
Database::instance()
->update("graphics_rules",array("active" => true), array("module_name" => $module_name));
}
/**
* Deactivate the rules for this module, typically done when the module itself is deactivated.
* Note that this does not mark images as dirty so that if you deactivate and reactivate a
* module it won't cause all of your images to suddenly require a rebuild.
*/
static function deactivate_rules($module_name) {
Database::instance()
->update("graphics_rules",array("active" => false), array("module_name" => $module_name));
}
/**
* Rebuild the thumb and resize for the given item.
* @param Item_Model $item
* @return true on successful generation
*/
static function generate($item) {
if ($item->is_album()) {
if (!$cover = $item->album_cover()) {
return false;
}
$input_file = $cover->file_path();
$input_item = $cover;
} else {
$input_file = $item->file_path();
$input_item = $item;
}
if ($item->thumb_dirty) {
$ops["thumb"] = $item->thumb_path();
}
if ($item->resize_dirty && !$item->is_album() && !$item->is_movie()) {
$ops["resize"] = $item->resize_path();
}
if (empty($ops)) {
$item->thumb_dirty = 0;
$item->resize_dirty = 0;
$item->save();
return true;
}
try {
foreach ($ops as $target => $output_file) {
if ($input_item->is_movie()) {
// Convert the movie to a JPG first
$output_file = preg_replace("/...$/", "jpg", $output_file);
try {
movie::extract_frame($input_file, $output_file);
} catch (Exception $e) {
// Assuming this is MISSING_FFMPEG for now
copy(MODPATH . "gallery/images/missing_movie.png", $output_file);
}
$working_file = $output_file;
} else {
$working_file = $input_file;
}
foreach (ORM::factory("graphics_rule")
->where("target", $target)
->where("active", true)
->orderby("priority", "asc")
->find_all() as $rule) {
$args = array($working_file, $output_file, unserialize($rule->args));
call_user_func_array(array("graphics", $rule->operation), $args);
$working_file = $output_file;
}
}
if (!empty($ops["thumb"])) {
$dims = getimagesize($item->thumb_path());
$item->thumb_width = $dims[0];
$item->thumb_height = $dims[1];
$item->thumb_dirty = 0;
}
if (!empty($ops["resize"])) {
$dims = getimagesize($item->resize_path());
$item->resize_width = $dims[0];
$item->resize_height = $dims[1];
$item->resize_dirty = 0;
}
$item->save();
} catch (Exception $e) {
// Something went wrong rebuilding the image. Leave it dirty and move on.
// @todo we should handle this better.
Kohana::log("error", "Caught exception rebuilding image: {$item->title}\n" .
$e->getMessage() . "\n" . $e->getTraceAsString());
return false;
}
return true;
}
/**
* Resize an image. Valid options are width, height and master. Master is one of the Image
* master dimension constants.
*
* @param string $input_file
* @param string $output_file
* @param array $options
*/
static function resize($input_file, $output_file, $options) {
if (!self::$init) {
self::init_toolkit();
}
module::event("graphics_resize", $input_file, $output_file, $options);
if (@filesize($input_file) == 0) {
throw new Exception("@todo EMPTY_INPUT_FILE");
}
$dims = getimagesize($input_file);
if (max($dims[0], $dims[1]) < min($options["width"], $options["height"])) {
// Image would get upscaled; do nothing
copy($input_file, $output_file);
} else {
Image::factory($input_file)
->resize($options["width"], $options["height"], $options["master"])
->quality(module::get_var("gallery", "image_quality"))
->save($output_file);
}
module::event("graphics_resize_completed", $input_file, $output_file, $options);
}
/**
* Rotate an image. Valid options are degrees
*
* @param string $input_file
* @param string $output_file
* @param array $options
*/
static function rotate($input_file, $output_file, $options) {
if (!self::$init) {
self::init_toolkit();
}
module::event("graphics_rotate", $input_file, $output_file, $options);
Image::factory($input_file)
->quality(module::get_var("gallery", "image_quality"))
->rotate($options["degrees"])
->save($output_file);
module::event("graphics_rotate_completed", $input_file, $output_file, $options);
}
/**
* Overlay an image on top of the input file.
*
* Valid options are: file, mime_type, position, transparency_percent, padding
*
* Valid positions: northwest, north, northeast,
* west, center, east,
* southwest, south, southeast
*
* padding is in pixels
*
* @param string $input_file
* @param string $output_file
* @param array $options
*/
static function composite($input_file, $output_file, $options) {
if (!self::$init) {
self::init_toolkit();
}
module::event("graphics_composite", $input_file, $output_file, $options);
list ($width, $height) = getimagesize($input_file);
list ($w_width, $w_height) = getimagesize($options["file"]);
$pad = isset($options["padding"]) ? $options["padding"] : 10;
$top = $pad;
$left = $pad;
$y_center = max($height / 2 - $w_height / 2, $pad);
$x_center = max($width / 2 - $w_width / 2, $pad);
$bottom = max($height - $w_height - $pad, $pad);
$right = max($width - $w_width - $pad, $pad);
switch ($options["position"]) {
case "northwest": $x = $left; $y = $top; break;
case "north": $x = $x_center; $y = $top; break;
case "northeast": $x = $right; $y = $top; break;
case "west": $x = $left; $y = $y_center; break;
case "center": $x = $x_center; $y = $y_center; break;
case "east": $x = $right; $y = $y_center; break;
case "southwest": $x = $left; $y = $bottom; break;
case "south": $x = $x_center; $y = $bottom; break;
case "southeast": $x = $right; $y = $bottom; break;
}
Image::factory($input_file)
->composite($options["file"], $x, $y, $options["transparency"])
->quality(module::get_var("gallery", "image_quality"))
->save($output_file);
module::event("graphics_composite_completed", $input_file, $output_file, $options);
}
/**
* Return a query result that locates all items with dirty images.
* @return Database_Result Query result
*/
static function find_dirty_images_query() {
return Database::instance()->query(
"SELECT `id` FROM {items} " .
"WHERE ((`thumb_dirty` = 1 AND (`type` <> 'album' OR `album_cover_item_id` IS NOT NULL))" .
" OR (`resize_dirty` = 1 AND `type` = 'photo')) " .
" AND `id` != 1");
}
/**
* Mark thumbnails and resizes as dirty. They will have to be rebuilt.
*/
static function mark_dirty($thumbs, $resizes) {
if ($thumbs || $resizes) {
$db = Database::instance();
$fields = array();
if ($thumbs) {
$fields["thumb_dirty"] = 1;
}
if ($resizes) {
$fields["resize_dirty"] = 1;
}
$db->update("items", $fields, true);
}
$count = self::find_dirty_images_query()->count();
if ($count) {
site_status::warning(
t2("One of your photos is out of date. <a %attrs>Click here to fix it</a>",
"%count of your photos are out of date. <a %attrs>Click here to fix them</a>",
$count,
array("attrs" => sprintf(
'href="%s" class="gDialogLink"',
url::site("admin/maintenance/start/gallery_task::rebuild_dirty_images?csrf=__CSRF__")))),
"graphics_dirty");
}
}
/**
* Detect which graphics toolkits are available on this system. Return an array of key value
* pairs where the key is one of gd, imagemagick, graphicsmagick and the value is information
* about that toolkit. For GD we return the version string, and for ImageMagick and
* GraphicsMagick we return the path to the directory containing the appropriate binaries.
*/
static function detect_toolkits() {
$gd = function_exists("gd_info") ? gd_info() : array();
$exec = function_exists("exec");
if (!isset($gd["GD Version"])) {
$gd["GD Version"] = false;
}
putenv("PATH=" . getenv("PATH") . ":/usr/local/bin:/opt/local/bin:/opt/bin");
return array("gd" => $gd,
"imagemagick" => $exec ? dirname(exec("which convert")) : false,
"graphicsmagick" => $exec ? dirname(exec("which gm")) : false);
}
/**
* This needs to be run once, after the initial install, to choose a graphics toolkit.
*/
static function choose_default_toolkit() {
// Detect a graphics toolkit
$toolkits = graphics::detect_toolkits();
foreach (array("imagemagick", "graphicsmagick", "gd") as $tk) {
if ($toolkits[$tk]) {
module::set_var("gallery", "graphics_toolkit", $tk);
module::set_var("gallery", "graphics_toolkit_path", $tk == "gd" ? "" : $toolkits[$tk]);
break;
}
}
if (!module::get_var("gallery", "graphics_toolkit")) {
site_status::warning(
t("Graphics toolkit missing! Please <a href=\"%url\">choose a toolkit</a>",
array("url" => url::site("admin/graphics"))),
"missing_graphics_toolkit");
}
}
/**
* Choose which driver the Kohana Image library uses.
*/
static function init_toolkit() {
switch(module::get_var("gallery", "graphics_toolkit")) {
case "gd":
Kohana::config_set("image.driver", "GD");
break;
case "imagemagick":
Kohana::config_set("image.driver", "ImageMagick");
Kohana::config_set(
"image.params.directory", module::get_var("gallery", "graphics_toolkit_path"));
break;
case "graphicsmagick":
Kohana::config_set("image.driver", "GraphicsMagick");
Kohana::config_set(
"image.params.directory", module::get_var("gallery", "graphics_toolkit_path"));
break;
}
self::$init = 1;
}
/**
* Verify that a specific graphics function is available with the active toolkit.
* @param string $func (eg rotate, resize)
* @return boolean
*/
static function can($func) {
if (module::get_var("gallery", "graphics_toolkit") == "gd" &&
$func == "rotate" &&
!function_exists("imagerotate")) {
return false;
}
return true;
}
}