OSDN Git Service

add delete routine condaction_list
[swfed/swfed.git] / www / swfimagereplace.php
1 <?php
2
3 require_once('define.php');
4
5 function multiply_unit($str) {
6    $str = preg_replace('/(\d+)G/ei', "$1*1024*1024*1024", $str);
7    $str = preg_replace('/(\d+)M/ei', "$1*1024*1024", $str);
8    $str = preg_replace('/(\d+)K/ei', "$1*1024", $str);
9    $str = preg_replace('/(\d+)B/ei', "$1", $str);
10    return $str;
11 }
12 $upload_max_filesize  = ini_get('upload_max_filesize');
13 $upload_max_filesize_bytes = multiply_unit($upload_max_filesize);
14
15 function detect_image_ext(&$imagedata) {
16     $image_sig = substr($imagedata, 0, 0x10);
17     if (strpos($image_sig, 'JFIF') === 6) {
18         $ext = '.jpg';
19     } elseif (strpos($image_sig, 'PNG') === 1) {
20         $ext = '.png';
21     } elseif (strpos($image_sig, 'GIF') === 0) {
22         $ext = '.gif';
23     } else {
24         $ext = false;
25     }
26     return $ext;
27 }
28
29 if (! empty($_FILES['imagefile']['tmp_name'])) {
30     $filename = $_FILES['imagefile']['tmp_name'];
31     $imagedata = file_get_contents($filename);
32     if ($imagedata > $upload_max_filesize_bytes) {
33         echo " $upload_max_filesize Bytes 以内のファイルしか受け付けません。\n";
34         exit(0);
35     }
36     $tmp_name = sha1($imagedata, false);
37     $id = $_REQUEST['id'];
38     $image_id = $_REQUEST['image_id'];
39     $ext = detect_image_ext($imagedata);
40     if ($ext == false) {
41         $image_sig = substr($imagedata, 0, 8);
42         echo "unknown image signature: ".bin2hex($image_sig)."\n";
43         exit(1);
44     }
45     $id_image = substr($tmp_name, 0, 16); // XXX
46     $tmp_filename = "$tmp_prefix$id_image$ext";
47     if ((! is_readable($tmp_filename)) &&
48         (! file_put_contents($tmp_filename, $imagedata))) {
49         fprintf(stderr, "swfimagereplace.php: file_put_contents failed. zero size?\n");
50         unlink($tmp_filename);
51         exit(0);
52     }
53 //    header("Location: ./swfimagereplace.php?id=$id&image_id=$image_id&id_image=$id_image&ext=$ext");
54 echo <<< FORM
55 <html>
56 <head>
57 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
58 </head>
59 <body bgcolor="#fff0f0">
60 <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
61 codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="100%" height="100%">
62 <param name="movie" value="./swfimagereplace.php?id=$id&image_id=$image_id&id_image=$id_image&ext=$ext">
63 <param name="quality" value="high">
64 </object>
65 </body>
66 </html>
67 FORM;
68     exit(0);
69 }
70
71 if (empty($_REQUEST['id_image']))  {
72     $id       = $_REQUEST['id'];
73     $image_id = $_REQUEST['image_id'];
74 echo <<< FORM
75 <html>
76 <head>
77 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
78 </head>
79 <body bgcolor="#fff0f0">
80 <form enctype="multipart/form-data" action="" method="POST">
81     <input type="hidden" name="MAX_FILE_SIZE" value="$upload_max_filesize_bytes;
82
83 _bytes" />
84     画像ファイルをアップロード: <input name="imagefile" type="file" />
85     <input type="hidden" name="id" value="$id" />
86     <input type="hidden" name="image_id" value="$image_id" />
87     <input type="submit" name="action" value="replace" />
88 </form>
89           ファイルを指定してください。($upload_max_filesize Bytes 以内に限定してます)
90 </body>
91 </html>
92 FORM;
93     exit(0);
94 }
95
96 $id = $_REQUEST['id'];
97 $image_id = $_REQUEST['image_id'];
98 $id_image = $_REQUEST['id_image'];
99 $ext = $_REQUEST['ext'];
100
101 if (($ext != '.jpg') && ($ext != '.png') && ($ext != '.gif')) {
102     echo "unknown ext=($ext)..\n";
103     exit(1);
104 }
105
106 $swf_filename = "$tmp_prefix$id.swf";
107 $swfdata = file_get_contents($swf_filename);
108 $image_filename = "$tmp_prefix$id_image$ext";
109 $imagedata = file_get_contents($image_filename);
110
111 $swf = new SWFEditor();
112 $swf->setShapeAdjustMode(SWFEditor::SHAPE_BITMAP_RECT_RESIZE);
113
114 if ($swf->input($swfdata) == false) {
115     echo "input failed\n";
116     exit(1);
117 }
118
119 switch ($ext) {
120   case '.jpg':
121     $result = $swf->replaceJpegData(intval($image_id), $imagedata);
122     break;
123   case '.png':
124     $result = $swf->replacePNGData(intval($image_id), $imagedata);
125     break;
126   case '.gif':
127     $result = $swf->replaceGIFData(intval($image_id), $imagedata);
128     break;
129   default:
130     echo "unknown ext($ext)...\n";
131     exit(1);
132 }
133
134 if ($result == false) {
135     echo "replace failed ext=$ext\n";
136     exit(0);
137 }
138
139 header('Content-type: application/x-shockwave-flash');
140 // header('Content-type: application/octet-stream');
141 echo $swf->output();