| 1 |
<?php
|
| 2 |
// $Id: thumb.module,v 1.1 2008/11/24 09:28:45 davyvandenbremt Exp $
|
| 3 |
|
| 4 |
// @todo allow local files (without compress/decompress)
|
| 5 |
// @todo allow more phpthumb settings
|
| 6 |
|
| 7 |
/**
|
| 8 |
* Implementation of hook_menu().
|
| 9 |
*/
|
| 10 |
function thumb_menu() {
|
| 11 |
$items = array();
|
| 12 |
|
| 13 |
$items[file_directory_path() .'/thumb'] = array(
|
| 14 |
'page callback' => 'thumb',
|
| 15 |
'access callback' => TRUE,
|
| 16 |
'type' => MENU_CALLBACK,
|
| 17 |
'file' => 'thumb.pages.inc',
|
| 18 |
);
|
| 19 |
$items['admin/build/thumb'] = array(
|
| 20 |
'title' => 'Thumb presets',
|
| 21 |
'description' => 'Administer thumb presets.',
|
| 22 |
'page callback' => 'thumb_presets_list',
|
| 23 |
'access arguments' => array('administer thumb'),
|
| 24 |
'file' => 'thumb.admin.inc',
|
| 25 |
);
|
| 26 |
$items['admin/build/thumb/list'] = array(
|
| 27 |
'title' => 'List',
|
| 28 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 29 |
'weight' => -10,
|
| 30 |
);
|
| 31 |
$items['admin/build/thumb/add'] = array(
|
| 32 |
'title' => 'Add new preset',
|
| 33 |
'page callback' => 'drupal_get_form',
|
| 34 |
'page arguments' => array('thumb_presets_form'),
|
| 35 |
'access arguments' => array('administer thumb'),
|
| 36 |
'type' => MENU_LOCAL_TASK,
|
| 37 |
'file' => 'thumb.admin.inc',
|
| 38 |
);
|
| 39 |
$items['admin/build/thumb/%thumb_preset/edit'] = array(
|
| 40 |
'title' => 'Edit',
|
| 41 |
'page callback' => 'drupal_get_form',
|
| 42 |
'page arguments' => array('thumb_presets_form', 3),
|
| 43 |
'access arguments' => array('administer thumb'),
|
| 44 |
'type' => MENU_CALLBACK,
|
| 45 |
'file' => 'thumb.admin.inc',
|
| 46 |
);
|
| 47 |
$items['admin/build/thumb/%thumb_preset/flush'] = array(
|
| 48 |
'title' => 'Flush',
|
| 49 |
'page callback' => 'drupal_get_form',
|
| 50 |
'page arguments' => array('thumb_flush_confirm_form', 3),
|
| 51 |
'access arguments' => array('administer thumb'),
|
| 52 |
'type' => MENU_CALLBACK,
|
| 53 |
'file' => 'thumb.admin.inc',
|
| 54 |
);
|
| 55 |
$items['admin/build/thumb/%thumb_preset/delete'] = array(
|
| 56 |
'title' => 'Flush',
|
| 57 |
'page callback' => 'drupal_get_form',
|
| 58 |
'page arguments' => array('thumb_delete_confirm_form', 3),
|
| 59 |
'access arguments' => array('administer thumb'),
|
| 60 |
'type' => MENU_CALLBACK,
|
| 61 |
'file' => 'thumb.admin.inc',
|
| 62 |
);
|
| 63 |
|
| 64 |
return $items;
|
| 65 |
}
|
| 66 |
|
| 67 |
/**
|
| 68 |
* Fetch a preset.
|
| 69 |
*
|
| 70 |
* @param $pid
|
| 71 |
* The pid of the preset you want to fetch.
|
| 72 |
*
|
| 73 |
* @return
|
| 74 |
* A preset object.
|
| 75 |
*/
|
| 76 |
function thumb_preset_load($pid = NULL) {
|
| 77 |
if (!$pid) return FALSE;
|
| 78 |
|
| 79 |
$preset = db_fetch_object(db_query("SELECT pid, identifier, description, data FROM {thumb_preset}", $pid));
|
| 80 |
if ($preset) {
|
| 81 |
$preset->data = unserialize($preset->data);
|
| 82 |
return $preset;
|
| 83 |
}
|
| 84 |
|
| 85 |
return FALSE;
|
| 86 |
}
|
| 87 |
|
| 88 |
/**
|
| 89 |
* Get a list of all presets.
|
| 90 |
*
|
| 91 |
* @return
|
| 92 |
* An array of presets indexed by the preset identifier.
|
| 93 |
*/
|
| 94 |
function thumb_presets() {
|
| 95 |
$presets = array();
|
| 96 |
$result = db_query("SELECT pid, identifier, description, data FROM {thumb_preset}");
|
| 97 |
while ($row = db_fetch_object($result)) {
|
| 98 |
$row->data = unserialize($row->data);
|
| 99 |
$presets[$row->identifier] = $row;
|
| 100 |
}
|
| 101 |
return $presets;
|
| 102 |
}
|
| 103 |
|
| 104 |
/**
|
| 105 |
* Generate the url of a thumbnail of an image for a certain preset.
|
| 106 |
*
|
| 107 |
* @param $presetname
|
| 108 |
* The preset the thumbnail was generated for.
|
| 109 |
* @param $path
|
| 110 |
* The path of the original image.
|
| 111 |
* @return
|
| 112 |
* The url of the thumbnail.
|
| 113 |
*/
|
| 114 |
function thumb_url($presetname, $path) {
|
| 115 |
$pathinfo = pathinfo($path);
|
| 116 |
$ext = $pathinfo['extension'];
|
| 117 |
$url = file_directory_path() .'/thumb/'. $presetname .'/'. thumb_compress_string($path);
|
| 118 |
if (!empty($ext)) {
|
| 119 |
$url .= '.'. $ext;
|
| 120 |
}
|
| 121 |
return url($url);
|
| 122 |
}
|
| 123 |
|
| 124 |
/**
|
| 125 |
* Send an image as a response to the client.
|
| 126 |
*
|
| 127 |
* @param $path
|
| 128 |
* A path of the image to send.
|
| 129 |
*/
|
| 130 |
function thumb_transfer($path) {
|
| 131 |
if (function_exists('mime_content_type')) {
|
| 132 |
$mime = mime_content_type($path);
|
| 133 |
}
|
| 134 |
else {
|
| 135 |
$size = getimagesize($path);
|
| 136 |
$mime = $size['mime'];
|
| 137 |
}
|
| 138 |
$headers = array('Content-Type: '. mime_header_encode($mime));
|
| 139 |
|
| 140 |
if ($fileinfo = stat($path)) {
|
| 141 |
$headers[] = 'Content-Length: '. $fileinfo[7];
|
| 142 |
_thumb_cache_set_cache_headers($fileinfo, $headers);
|
| 143 |
}
|
| 144 |
file_transfer($path, $headers);
|
| 145 |
exit;
|
| 146 |
}
|
| 147 |
|
| 148 |
/**
|
| 149 |
* Encode an image path.
|
| 150 |
*
|
| 151 |
* @param $string
|
| 152 |
* A path to encode.
|
| 153 |
* @return
|
| 154 |
* The encoded path.
|
| 155 |
*/
|
| 156 |
function thumb_compress_string($string) {
|
| 157 |
$prefix = substr(md5($string), 0, 4);
|
| 158 |
$prefix = implode('/', array($prefix[0], $prefix[1], $prefix[2], $prefix[3]));
|
| 159 |
return $prefix .'/'. thumb_urlsafe_base64_encode($string);
|
| 160 |
}
|
| 161 |
|
| 162 |
/**
|
| 163 |
* Decode an encoded image path.
|
| 164 |
*
|
| 165 |
* @param $string
|
| 166 |
* A path to decode.
|
| 167 |
* @return
|
| 168 |
* The decoded path.
|
| 169 |
*/
|
| 170 |
function thumb_decompress_string($string) {
|
| 171 |
return substr(thumb_urlsafe_base64_decode($string), 6, strlen($string));
|
| 172 |
}
|
| 173 |
|
| 174 |
/**
|
| 175 |
* Encode a string with URL safe base 64.
|
| 176 |
*
|
| 177 |
* @param $string
|
| 178 |
* A string to encode.
|
| 179 |
* @return
|
| 180 |
* The encoded string.
|
| 181 |
*/
|
| 182 |
function thumb_urlsafe_base64_encode($string) {
|
| 183 |
$data = base64_encode($string);
|
| 184 |
$data = str_replace(array('+', '/', '='), array('-', '_', ''), $data);
|
| 185 |
return $data;
|
| 186 |
}
|
| 187 |
|
| 188 |
/**
|
| 189 |
* Decode a string encoded with URL safe base 64.
|
| 190 |
*
|
| 191 |
* @param $string
|
| 192 |
* The encoded string.
|
| 193 |
* @return
|
| 194 |
* The decoded string.
|
| 195 |
*/
|
| 196 |
function thumb_urlsafe_base64_decode($string) {
|
| 197 |
$data = str_replace(array('-', '_'), array('+', '/'), $string);
|
| 198 |
$mod4 = strlen($data) % 4;
|
| 199 |
if ($mod4) {
|
| 200 |
$data .= substr('====', $mod4);
|
| 201 |
}
|
| 202 |
return base64_decode($data);
|
| 203 |
}
|
| 204 |
|
| 205 |
/**
|
| 206 |
* Set file headers that handle "If-Modified-Since" correctly for the
|
| 207 |
* given fileinfo. Most code has been taken from drupal_page_cache_header().
|
| 208 |
*/
|
| 209 |
function _thumb_cache_set_cache_headers($fileinfo, &$headers) {
|
| 210 |
// Set default values:
|
| 211 |
$last_modified = gmdate('D, d M Y H:i:s', $fileinfo[9]) .' GMT';
|
| 212 |
$etag = '"'. md5($last_modified) .'"';
|
| 213 |
|
| 214 |
// See if the client has provided the required HTTP headers:
|
| 215 |
$if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
|
| 216 |
? stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE'])
|
| 217 |
: FALSE;
|
| 218 |
$if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH'])
|
| 219 |
? stripslashes($_SERVER['HTTP_IF_NONE_MATCH'])
|
| 220 |
: FALSE;
|
| 221 |
|
| 222 |
if ($if_modified_since && $if_none_match
|
| 223 |
&& $if_none_match == $etag // etag must match
|
| 224 |
&& $if_modified_since == $last_modified) { // if-modified-since must match
|
| 225 |
header('HTTP/1.1 304 Not Modified');
|
| 226 |
// All 304 responses must send an etag if the 200 response
|
| 227 |
// for the same object contained an etag
|
| 228 |
header('Etag: '. $etag);
|
| 229 |
// We must also set Last-Modified again, so that we overwrite Drupal's
|
| 230 |
// default Last-Modified header with the right one
|
| 231 |
header('Last-Modified: '. $last_modified);
|
| 232 |
exit;
|
| 233 |
}
|
| 234 |
|
| 235 |
// Send appropriate response:
|
| 236 |
$headers[] = 'Last-Modified: '. $last_modified;
|
| 237 |
$headers[] = 'ETag: '. $etag;
|
| 238 |
}
|