-
Notifications
You must be signed in to change notification settings - Fork 45
/
ic2_mkthumb.php
149 lines (131 loc) · 3.99 KB
/
ic2_mkthumb.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
<?php
/**
* ImageCache2 - サムネイルの再構築
*/
// {{{ p2基本設定読み込み&認証
require_once './conf/conf.inc.php';
$_login->authorize();
ini_set('display_errors', true);
if (!$_conf['expack.ic2.enabled']) {
p2die('ImageCache2は無効です。', 'conf/conf_admin_ex.inc.php の設定を変えてください。');
}
// }}}
/*if ($GLOBALS['debug']) {
require_once 'Var_Dump.php';
Var_Dump::display($_GET);
exit;
}*/
require_once P2EX_LIB_DIR . '/ic2/bootstrap.php';
// {{{ リクエストパラメータの処理
$uri = $_GET['u'];
$type = $_GET['v'];
$thumb = isset($_GET['t']) ? intval($_GET['t']) : 0;
$options = array();
$options['quality'] = isset($_GET['q']) ? intval($_GET['q']) : null;
$options['rotate'] = isset($_GET['r']) ? intval($_GET['r']) : 0;
$options['trim'] = !empty($_GET['w']);
if (isset($_GET['x']) && $_GET['x'] >= 1 && isset($_GET['y']) && $_GET['y'] >= 1) {
$options['width'] = intval($_GET['x']);
$options['height'] = intval($_GET['y']);
}
$preset = isset($_GET['p']) ? $_GET['p'] : '';
if (!empty($preset)) {
$ini = ic2_loadconfig();
$preset = $_GET['p'];
if (isset($ini['Dynamic']['presets'][$preset])) {
$options['width'] = $ini['Dynamic']['presets'][$preset][0];
$options['height'] = $ini['Dynamic']['presets'][$preset][1];
if (isset($ini['Dynamic']['presets'][$preset][2])) {
$options['quality'] = $ini['Dynamic']['presets'][$preset][2];
}
}
}
$attachment = !empty($_GET['z']);
// }}}
// {{{ 画像を検索・サムネイルを作成
$search = new IC2_DataObject_Images;
switch ($type) {
case 'id':
$search->whereAddQuoted('id', '=', $uri);
break;
case 'file':
preg_match('/^([1-9][0-9]*)_([0-9a-f]{32})(?:\.(jpg|png|gif))?$/', $uri, $fdata);
$search->whereAddQuoted('size', '=', $fdata[0]);
$search->whereAddQuoted('md5', '=', $fdata[1]);
break;
default:
$search->whereAddQuoted('uri', '=', $uri);
}
if ($search->find(true)) {
if (!empty($_GET['o'])) {
$thumb = new IC2_Thumbnailer(IC2_Thumbnailer::SIZE_DEFAULT);
$src = $thumb->srcPath($search->size, $search->md5, $search->mime);
if (!file_exists($src)) {
ic2_mkthumb_error(""{$uri}"のローカルキャッシュがありません。");
} else {
ic2_mkthumb_success(basename($src), $search->mime, $src, true, $attachment);
}
} else {
$thumb = new IC2_Thumbnailer($thumb, $options);
$result = $thumb->convert($search->size, $search->md5, $search->mime, $search->width, $search->height);
if (PEAR::isError($result)) {
ic2_mkthumb_error($result->getMessage());
} else {
$mime = ($thumb->type == '.png') ? 'image/png' : 'image/jpeg';
ic2_mkthumb_success(basename($result), $mime, $thumb->buf, false, $attachment);
}
}
} else {
ic2_mkthumb_error(""{$uri}"はキャッシュされていません。");
}
// }}}
// {{{ ic2_mkthumb_success()
/**
* サムネイルの作成に成功した場合
*/
function ic2_mkthumb_success($name, $mime, $data, $is_file, $attachment)
{
while (ob_get_level()) {
ob_end_clean();
}
header(sprintf('Content-Type: %s; filename="%s"', $mime, $name));
if ($attachment) {
header(sprintf('Content-Disposition: attachment; filename="%s"', $name));
} else {
header(sprintf('Content-Disposition: inline; filename="%s"', $name));
}
if ($is_file) {
header(sprintf('Content-Length: %d', filesize($data)));
readfile($data);
} else {
header(sprintf('Content-Length: %d', strlen($data)));
echo $data;
}
}
// }}}
// {{{ ic2_mkthumb_error()
/**
* サムネイルの作成に失敗した場合
*/
function ic2_mkthumb_error($msg)
{
echo <<<EOF
<html>
<head><title>ImageCache::Error</title></head>
<body>
<p>{$msg}</p>
</body>
</html>
EOF;
}
// }}}
/*
* Local Variables:
* mode: php
* coding: cp932
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/
// vim: set syn=php fenc=cp932 ai et ts=4 sw=4 sts=4 fdm=marker: