| 1 |
<?php
|
| 2 |
|
| 3 |
/**
|
| 4 |
* Menu callback for handling thumb requests.
|
| 5 |
*/
|
| 6 |
function thumb() {
|
| 7 |
$args = func_get_args();
|
| 8 |
$preset = check_plain(array_shift($args));
|
| 9 |
$path = implode('/', $args);
|
| 10 |
_thumb($preset, $path);
|
| 11 |
}
|
| 12 |
|
| 13 |
function _thumb($presetname, $path) {
|
| 14 |
$parts = explode('.', $path);
|
| 15 |
$bare_path = $parts[0];
|
| 16 |
|
| 17 |
$pathinfo = pathinfo($path);
|
| 18 |
|
| 19 |
$presets = thumb_presets();
|
| 20 |
if (!array_key_exists($presetname, $presets)) {
|
| 21 |
// Send a 404 if we don't know of a preset.
|
| 22 |
header("HTTP/1.0 404 Not Found");
|
| 23 |
exit;
|
| 24 |
}
|
| 25 |
|
| 26 |
$preset = $presets[$presetname];
|
| 27 |
|
| 28 |
require_once(drupal_get_path('module', 'thumb') .'/phpThumb/phpthumb.class.php');
|
| 29 |
|
| 30 |
$thumb = new phpThumb();
|
| 31 |
$thumb->src = thumb_decompress_string($bare_path);
|
| 32 |
|
| 33 |
foreach ($preset->data as $setting => $value) {
|
| 34 |
$thumb->{$setting} = $value;
|
| 35 |
}
|
| 36 |
|
| 37 |
$thumb->config_output_format = $pathinfo['extension'];
|
| 38 |
$thumb->config_error_die_on_error = TRUE;
|
| 39 |
$thumb->config_document_root = '';
|
| 40 |
$thumb->config_temp_directory = file_directory_temp();
|
| 41 |
$thumb->config_cache_directory = file_directory_temp() .'/cache/';
|
| 42 |
$thumb->config_cache_disable_warning = TRUE;
|
| 43 |
|
| 44 |
// Build the destination folder tree if it doesn't already exists.
|
| 45 |
$root = realpath((getenv('DOCUMENT_ROOT') && ereg('^'. preg_quote(realpath(getenv('DOCUMENT_ROOT'))), realpath(__FILE__))) ? getenv('DOCUMENT_ROOT') : str_replace(dirname(@$_SERVER['PHP_SELF']), '', str_replace(DIRECTORY_SEPARATOR, '/', dirname(__FILE__))));
|
| 46 |
|
| 47 |
$dir = $root .'/'. file_directory_path() .'/thumb/'. $presetname .'/'. $pathinfo['dirname'];
|
| 48 |
if (!file_check_directory($dir, FILE_CREATE_DIRECTORY) && !mkdir($dir, 0775, TRUE)) {
|
| 49 |
watchdog('imagecache', 'Failed to create imagecache directory: %dir', array('%dir' => $dir), WATCHDOG_ERROR);
|
| 50 |
return FALSE;
|
| 51 |
}
|
| 52 |
|
| 53 |
$thumb->cache_filename = $dir .'/'. $pathinfo['basename'] .'.'. $pathinfo['extension'];
|
| 54 |
|
| 55 |
if ($thumb->GenerateThumbnail()) {
|
| 56 |
$thumb->RenderToFile($thumb->cache_filename);
|
| 57 |
}
|
| 58 |
else {
|
| 59 |
die('Failed: '. $thumb->error);
|
| 60 |
}
|
| 61 |
|
| 62 |
if (is_file($thumb->cache_filename)) {
|
| 63 |
thumb_transfer($thumb->cache_filename);
|
| 64 |
}
|
| 65 |
}
|