回答編集履歴

1

セーフモードとallow_url_fopenを考慮したコードを追記

2017/12/30 06:40

投稿

Tomak
Tomak

スコア1652

test CHANGED
@@ -29,3 +29,181 @@
29
29
  echo $image;
30
30
 
31
31
  ```
32
+
33
+
34
+
35
+ ----
36
+
37
+
38
+
39
+ **= 追記 =**
40
+
41
+
42
+
43
+ コメントでPHP-5.2、セーフモードという情報をいただいたので、回答に追記しておきます。
44
+
45
+ **file_get_contents()**、**file_put_contents()**は、`php-5.2`でも動くのでセーフモードの可能性が高いです。ただし、ファイル書き込みを権限(Webサーバーと同じオーナー)のあるディレクトリに置けば、解決できる可能性があります。そのようなディレクトリがない場合はコマンドなどで作成します。
46
+
47
+
48
+
49
+ セーフモードでは、通常Webサーバーのオーナーをチェックします。設定によって動きが異なるので、`phpinfo()`などでチェックしてください。
50
+
51
+ - [http://php.net/manual/ja/ini.sect.safe-mode.php](http://php.net/manual/ja/ini.sect.safe-mode.php)
52
+
53
+ - [http://php.net/manual/ja/features.safe-mode.functions.php](http://php.net/manual/ja/features.safe-mode.functions.php)
54
+
55
+
56
+
57
+ また、`allow_url_fopen`のオプションも確認してみてください。下記サンプルコードは**allow_url_fopen設定**と**セーフモード**を配慮したコードです。
58
+
59
+
60
+
61
+
62
+
63
+ #### セーフモードでallow_url_fopen = 0ff
64
+
65
+
66
+
67
+ **※注意**
68
+
69
+ - allow_url_fopen = Offで、設定変更できない場合は極端に使える選択肢が狭まります。例えば下記のように`CURL`を使います。
70
+
71
+ - 下記**セーフモード許可ディレクトリ**とは、Webサーバーのオーナー(所有者)と同じオーナーのディレクトリを指します。
72
+
73
+
74
+
75
+ ```php
76
+
77
+ ini_set('display_errors', 1);
78
+
79
+ error_reporting(E_ALL);
80
+
81
+
82
+
83
+ $path = 'セーフモード許可ディレクトリ/image.png';
84
+
85
+
86
+
87
+ $curl = curl_init('http://wabito.maru1244.jp/face_place/image.png');
88
+
89
+ curl_setopt($curl, CURLOPT_HEADER, 0);
90
+
91
+ curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
92
+
93
+ curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
94
+
95
+
96
+
97
+ //CURLで画像をダウンロード
98
+
99
+ $raw = curl_exec($curl);
100
+
101
+
102
+
103
+ if (false !== file_put_contents($path, $raw)) {
104
+
105
+ $image = new Imagick(realpath($path));
106
+
107
+ $image->setImageFormat('png');
108
+
109
+ $image->thumbnailImage(100, 0);
110
+
111
+
112
+
113
+ header('Content-type: image/png');
114
+
115
+ echo $image;
116
+
117
+ }
118
+
119
+
120
+
121
+ //CURLセッションクローズ
122
+
123
+ curl_close($curl);
124
+
125
+ ```
126
+
127
+
128
+
129
+
130
+
131
+ #### セーフモードでallow_url_fopen = On
132
+
133
+
134
+
135
+ **※注意**
136
+
137
+ - 下記どのサンプルでも`allow_url_fopen = On`になっている必要があります。`php.ini`などの設定ファイルでしか変更できないので注意してください。
138
+
139
+ - 下記**セーフモード許可ディレクトリ**とは、Webサーバーのオーナー(所有者)と同じオーナーのディレクトリを指します。
140
+
141
+
142
+
143
+ ```php
144
+
145
+ ini_set('display_errors', 1);
146
+
147
+ error_reporting(E_ALL);
148
+
149
+
150
+
151
+ $path = 'セーフモード許可ディレクトリ/image.png';
152
+
153
+
154
+
155
+ if (copy('http://wabito.maru1244.jp/face_place/image.png', $path)) {
156
+
157
+ $image = new Imagick(realpath($path));
158
+
159
+ $image->setImageFormat('png');
160
+
161
+ $image->thumbnailImage(100, 0);
162
+
163
+
164
+
165
+ header('Content-type: image/png');
166
+
167
+ echo $image;
168
+
169
+ }
170
+
171
+ ```
172
+
173
+
174
+
175
+ 下記は`file_get_contents()`、`file_put_contents()`を使うサンプルです。
176
+
177
+
178
+
179
+ ```php
180
+
181
+ ini_set('display_errors', 1);
182
+
183
+ error_reporting(E_ALL);
184
+
185
+
186
+
187
+ $path = 'セーフモード許可ディレクトリ/image.png';
188
+
189
+ $image = file_get_contents('http://wabito.maru1244.jp/face_place/image.png');
190
+
191
+
192
+
193
+ if(false !== file_put_contents($path, $image)) {
194
+
195
+ $image = new Imagick(realpath($path));
196
+
197
+ $image->setImageFormat('png');
198
+
199
+ $image->thumbnailImage(100, 0);
200
+
201
+
202
+
203
+ header('Content-type: image/png');
204
+
205
+ echo $image;
206
+
207
+ }
208
+
209
+ ```