-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimagecurdir.php
67 lines (59 loc) · 1.87 KB
/
imagecurdir.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
<?php
/**
* Pico ImageCurDir Plugin
*
* @author TakamiChie
* @link http://onpu-tamago.net/
* @license http://opensource.org/licenses/MIT
* @version 1.1
*/
class ImageCurDir extends AbstractPicoPlugin{
protected $enabled = false;
public function onMetaHeaders(array &$headers)
{
$headers['image'] = 'Image';
}
public function onSinglePageLoaded(array &$pageData)
{
// コンテンツの実URLを導出
$base_url = $this->getBaseUrl();
$content_dir = $this->getConfig('content_dir');
$file_url = substr($pageData["url"], strlen($base_url));
$file_url_len = strlen($file_url);
if(!$file_url_len || $file_url[$file_url_len - 1] == "/")
{
$file_url .= 'index';
}
$rootdir = $this->getPico()->getRootDir();
$dir = substr($content_dir, strlen($rootdir));
$path = pathinfo($file_url);
$path['dirname'] = $path['dirname'] == "." ? "" :
$path['dirname'] . "/";
// イメージパスを展開する内部メソッド
$pathSettlement = function($img) use ($base_url, $dir, $path) {
if (strlen($img) > 0) {
if($img[0] == '.'){
$img = $base_url . "$dir${path['dirname']}${path['filename']}$img";
}else if(!preg_match('/^(https?|ftp)/', $img)){
$img = $base_url . "$dir${path['dirname']}$img";
}
} else {
$img = NULL;
}
return $img;
};
// メタデータ「image」のパス変換
if(isset($pageData['meta']['image'])) {
$pageData['image'] = $pathSettlement($pageData['meta']['image']);
}
// メタデータ「content」のパス変換
if(isset($pageData['content'])) {
$pageData['content'] = preg_replace_callback('|<img([^>]*)src="([^"]*)"|',
function($m) use ($pathSettlement){
$imgpath = $pathSettlement($m[2]);
return "<img$m[1]src=\"$imgpath\"";
}, $pageData['content']);
}
}
}
?>