OSDN Git Service

first commit
[cosmic/source.git] / page.php
1 <?php
2
3 include_once('./config/site.inc.php');
4
5 $request__ix = check_request('ix',CV_MD5  ,'');
6 $request__pg = check_request('pg',CV_DIGIT,0);
7
8 $fid_index = PATH_CACHE.'/'.$request__ix.CACHE_EXT_INDEX;
9 $fid_list  = PATH_CACHE.'/'.$request__ix.CACHE_EXT_LIST;
10
11 if (!file_exists($fid_index)) {
12         die('error:ix not found');
13 }
14
15 $path = file_get_contents($fid_index);
16 $fid_comic = PATH_COMIC.'/'.$path;
17
18 if ($request__ix=='') {
19         die('error:empty ix');
20 }
21
22
23 // zipファイルのエントリリストを取得(画像ファイルに限定)
24 if (!file_exists($fid_list) || true) {
25         $entry_data = array();
26         $entry_name = array();
27         $zip = zip_open($fid_comic);
28         $no = -1;
29         $img = array(NULL,NULL);
30         while(($zip_entry = zip_read($zip)) !== false){
31                 $no++;
32                 $zip_entry_name = zip_entry_name($zip_entry);
33                 $zip_entry_type = common__get_fileext($zip_entry_name);
34                 if ($zip_entry_type=='' || mb_strpos('jpg/jpeg/png/gif',$zip_entry_type)===false) continue;
35                 $entry_data[] = array(
36                         'no'   => $no,
37                         'name' => $zip_entry_name,
38                         'size' => zip_entry_filesize($zip_entry),
39                 );
40                 $entry_name[] = $zip_entry_name;
41         }
42         zip_close($zip);
43         array_multisort($entry_name, SORT_ASC, $entry_data);
44         file_put_contents($fid_list,serialize($entry_data));
45 } else {
46         $entry_data = unserialize(file_get_contents($fid_list));
47 }
48
49 // ページ番号からzip内番号に変換する
50 $zip_no[0] = isset($entry_data[$request__pg  ]) ? $entry_data[$request__pg  ]['no'] : -1;
51 $zip_no[1] = isset($entry_data[$request__pg+1]) ? $entry_data[$request__pg+1]['no'] : -1;
52
53 // zipファイルから指定されたページとその次ページを持ってくる
54 $zip = zip_open($fid_comic);
55 $no = 0;
56 $img = array(NULL,NULL);
57 while(($zip_entry = zip_read($zip)) !== false){
58         foreach ($zip_no as $pgid => $target_no) {
59                 if ($no==$target_no) {
60                         $img[$pgid] = zip_entry_read($zip_entry,zip_entry_filesize($zip_entry));
61                 }
62         }
63         if (!is_null($img[0]) && !is_null($img[1])) break;
64         $no++;
65 }
66 zip_close($zip);
67
68
69 // 画像を作成して出力する
70 if (is_null($img[0])) {
71
72         // ページが無かった場合
73         echo "no image";
74
75 } else if (is_null($img[1])) {
76
77         // 1ページしか無かった場合
78         header("Content-type: image/jpeg");
79         echo $img[0];
80
81 } else {
82
83         // 2ページある
84
85         // GDイメージを作成
86         for ($pgid=0;$pgid<=1;$pgid++) {
87                 $img[$pgid] = imagecreatefromstring($img[$pgid]);
88                 $sx[$pgid] = imagesx($img[$pgid]);
89                 $sy[$pgid] = imagesy($img[$pgid]);
90         }
91
92         // 
93         if ($sx[0]>$sy[0] || $sx[1]>$sy[1]) {
94                 // 横長ページが含まれるときは、1ページ目のみを戻す
95                 header("Content-type: image/jpeg");
96                 imagejpeg($img[0]);
97         } else {
98                 // 2ページを並べる
99                 $vx = $sx[0]+$sx[1];
100                 $vy = ($sy[0]>$sy[1] ? $sy[0] : $sy[1]);
101                 $view = imagecreatetruecolor($vx,$vy);
102                 header("Content-type: image/jpeg");
103                 imagecopy($view,$img[0],$sx[1],($vy-$sy[0])/2,0,0,$sx[0],$sy[0]);
104                 imagecopy($view,$img[1],0,($vy-$sy[1])/2,0,0,$sx[1],$sy[1]);
105                 imagejpeg($view);
106         }
107
108
109 }
110
111 ?>