|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
defined('PH7') or exit('Restricted access'); |
|
3
|
|
|
if (!\PH7\Admin::auth()) exit('Restricted access'); // Accessible only for admins |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Copyright (c) 2011, oov. All rights reserved. |
|
7
|
|
|
* |
|
8
|
|
|
* Redistribution and use in source and binary forms, with or without modification, |
|
9
|
|
|
* are permitted provided that the following conditions are met: |
|
10
|
|
|
* |
|
11
|
|
|
* - Redistributions of source code must retain the above copyright notice, |
|
12
|
|
|
* this list of conditions and the following disclaimer. |
|
13
|
|
|
* - Redistributions in binary form must reproduce the above copyright notice, |
|
14
|
|
|
* this list of conditions and the following disclaimer in the documentation |
|
15
|
|
|
* and/or other materials provided with the distribution. |
|
16
|
|
|
* - Neither the name of the oov nor the names of its contributors may be used to |
|
17
|
|
|
* endorse or promote products derived from this software without specific prior |
|
18
|
|
|
* written permission. |
|
19
|
|
|
* |
|
20
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|
21
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
22
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
23
|
|
|
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
|
24
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
25
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
|
26
|
|
|
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
27
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
28
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
29
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
|
30
|
|
|
* |
|
31
|
|
|
* bmp ファイルを GD で使えるように |
|
32
|
|
|
* |
|
33
|
|
|
* 使用例: |
|
34
|
|
|
* //ファイルから読み込む場合はGDでPNGなどを読み込むのと同じような方法で可 |
|
35
|
|
|
* $image = imagecreatefrombmp("test.bmp"); |
|
36
|
|
|
* imagedestroy($image); |
|
37
|
|
|
* |
|
38
|
|
|
* //文字列から読み込む場合は以下の方法で可 |
|
39
|
|
|
* $image = GdBmp::loadFromString(file_get_contents("test.bmp")); |
|
40
|
|
|
* //自動判定されるので破損ファイルでなければこれでも上手くいく |
|
41
|
|
|
* //$image = imagecreatefrombmp(file_get_contents("test.bmp")); |
|
42
|
|
|
* imagedestroy($image); |
|
43
|
|
|
* |
|
44
|
|
|
* //その他任意のストリームからの読み込みも可能 |
|
45
|
|
|
* $stream = fopen("http://127.0.0.1/test.bmp"); |
|
46
|
|
|
* $image = GdBmp::loadFromStream($stream); |
|
47
|
|
|
* //自動判定されるのでこれでもいい |
|
48
|
|
|
* //$image = imagecreatefrombmp($stream); |
|
49
|
|
|
* fclose($stream); |
|
50
|
|
|
* imagedestroy($image); |
|
51
|
|
|
* |
|
52
|
|
|
* 対応フォーマット |
|
53
|
|
|
* 1bit |
|
54
|
|
|
* 4bit |
|
55
|
|
|
* 4bitRLE |
|
56
|
|
|
* 8bit |
|
57
|
|
|
* 8bitRLE |
|
58
|
|
|
* 16bit(任意のビットフィールド) |
|
59
|
|
|
* 24bit |
|
60
|
|
|
* 32bit(任意のビットフィールド) |
|
61
|
|
|
* BITMAPINFOHEADER の biCompression が BI_PNG / BI_JPEG の画像 |
|
62
|
|
|
* すべての形式でトップダウン/ボトムアップの両方をサポート |
|
63
|
|
|
* 特殊なビットフィールドでもビットフィールドデータが正常なら読み込み可能 |
|
64
|
|
|
* |
|
65
|
|
|
* 以下のものは非対応 |
|
66
|
|
|
* BITMAPV4HEADER と BITMAPV5HEADER に含まれる色空間に関する様々な機能 |
|
67
|
|
|
* @param $filename_or_stream_or_binary |
|
68
|
|
|
* @return bool|resource |
|
69
|
|
|
*/ |
|
70
|
|
|
|
|
71
|
|
|
function imagecreatefrombmp($filename_or_stream_or_binary){ |
|
72
|
|
|
return elFinderLibGdBmp::load($filename_or_stream_or_binary); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
class elFinderLibGdBmp{ |
|
76
|
|
|
public static function load($filename_or_stream_or_binary){ |
|
77
|
|
|
if (is_resource($filename_or_stream_or_binary)){ |
|
78
|
|
|
return self::loadFromStream($filename_or_stream_or_binary); |
|
79
|
|
|
} else if (is_string($filename_or_stream_or_binary) && strlen($filename_or_stream_or_binary) >= 26){ |
|
80
|
|
|
$bfh = unpack("vtype/Vsize", $filename_or_stream_or_binary); |
|
81
|
|
|
if ($bfh["type"] == 0x4d42 && ($bfh["size"] == 0 || $bfh["size"] == strlen($filename_or_stream_or_binary))){ |
|
82
|
|
|
return self::loadFromString($filename_or_stream_or_binary); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
return self::loadFromFile($filename_or_stream_or_binary); |
|
86
|
|
|
} |
|
87
|
|
|
public static function loadFromFile($filename){ |
|
88
|
|
|
$fp = fopen($filename, "rb"); |
|
89
|
|
|
if ($fp === false){ |
|
90
|
|
|
return false; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$bmp = self::loadFromStream($fp); |
|
94
|
|
|
|
|
95
|
|
|
fclose($fp); |
|
96
|
|
|
return $bmp; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public static function loadFromString($str){ |
|
100
|
|
|
//data scheme より古いバージョンから対応しているようなので php://memory を使う |
|
101
|
|
|
$fp = fopen("php://memory", "r+b"); |
|
102
|
|
|
if ($fp === false){ |
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if (fwrite($fp, $str) != strlen($str)){ |
|
107
|
|
|
fclose($fp); |
|
108
|
|
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
if (fseek($fp, 0) === -1){ |
|
112
|
|
|
fclose($fp); |
|
113
|
|
|
return false; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$bmp = self::loadFromStream($fp); |
|
117
|
|
|
|
|
118
|
|
|
fclose($fp); |
|
119
|
|
|
return $bmp; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public static function loadFromStream($stream){ |
|
123
|
|
|
$buf = fread($stream, 14); //2+4+2+2+4 |
|
|
|
|
|
|
124
|
|
|
if ($buf === false){ |
|
125
|
|
|
return false; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
//シグネチャチェック |
|
129
|
|
|
if ($buf[0] != 'B' || $buf[1] != 'M'){ |
|
130
|
|
|
return false; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$bitmap_file_header = unpack( |
|
134
|
|
|
//BITMAPFILEHEADER構造体 |
|
135
|
|
|
"vtype/". |
|
136
|
|
|
"Vsize/". |
|
137
|
|
|
"vreserved1/". |
|
138
|
|
|
"vreserved2/". |
|
139
|
|
|
"Voffbits", $buf |
|
140
|
|
|
); |
|
141
|
|
|
|
|
142
|
|
|
return self::loadFromStreamAndFileHeader($stream, $bitmap_file_header); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public static function loadFromStreamAndFileHeader($stream, array $bitmap_file_header){ |
|
146
|
|
|
if ($bitmap_file_header["type"] != 0x4d42){ |
|
147
|
|
|
return false; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
//情報ヘッダサイズを元に形式を区別して読み込み |
|
151
|
|
|
$buf = fread($stream, 4); |
|
152
|
|
|
if ($buf === false){ |
|
153
|
|
|
return false; |
|
154
|
|
|
} |
|
155
|
|
|
list(,$header_size) = unpack("V", $buf); |
|
156
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
if ($header_size == 12){ |
|
159
|
|
|
$buf = fread($stream, $header_size - 4); |
|
160
|
|
|
if ($buf === false){ |
|
161
|
|
|
return false; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
extract(unpack( |
|
|
|
|
|
|
165
|
|
|
//BITMAPCOREHEADER構造体 - OS/2 Bitmap |
|
166
|
|
|
"vwidth/". |
|
167
|
|
|
"vheight/". |
|
168
|
|
|
"vplanes/". |
|
169
|
|
|
"vbit_count", $buf |
|
170
|
|
|
)); |
|
171
|
|
|
//飛んでこない分は 0 で初期化しておく |
|
172
|
|
|
$clr_used = $clr_important = $alpha_mask = $compression = 0; |
|
|
|
|
|
|
173
|
|
|
|
|
174
|
|
|
//マスク類は初期化されないのでここで割り当てておく |
|
175
|
|
|
$red_mask = 0x00ff0000; |
|
176
|
|
|
$green_mask = 0x0000ff00; |
|
177
|
|
|
$blue_mask = 0x000000ff; |
|
178
|
|
|
} else if (124 < $header_size || $header_size < 40) { |
|
179
|
|
|
//未知の形式 |
|
180
|
|
|
return false; |
|
181
|
|
|
} else { |
|
182
|
|
|
//この時点で36バイト読めることまではわかっている |
|
183
|
|
|
$buf = fread($stream, 36); //既に読んだ部分は除外しつつBITMAPINFOHEADERのサイズだけ読む |
|
184
|
|
|
if ($buf === false){ |
|
185
|
|
|
return false; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
//BITMAPINFOHEADER構造体 - Windows Bitmap |
|
189
|
|
|
extract(unpack( |
|
|
|
|
|
|
190
|
|
|
"Vwidth/". |
|
191
|
|
|
"Vheight/". |
|
192
|
|
|
"vplanes/". |
|
193
|
|
|
"vbit_count/". |
|
194
|
|
|
"Vcompression/". |
|
195
|
|
|
"Vsize_image/". |
|
196
|
|
|
"Vx_pels_per_meter/". |
|
197
|
|
|
"Vy_pels_per_meter/". |
|
198
|
|
|
"Vclr_used/". |
|
199
|
|
|
"Vclr_important", $buf |
|
200
|
|
|
)); |
|
201
|
|
|
//負の整数を受け取る可能性があるものは自前で変換する |
|
202
|
|
|
if ($width & 0x80000000){ $width = -(~$width & 0xffffffff) - 1; } |
|
203
|
|
|
if ($height & 0x80000000){ $height = -(~$height & 0xffffffff) - 1; } |
|
204
|
|
|
if ($x_pels_per_meter & 0x80000000){ $x_pels_per_meter = -(~$x_pels_per_meter & 0xffffffff) - 1; } |
|
|
|
|
|
|
205
|
|
|
if ($y_pels_per_meter & 0x80000000){ $y_pels_per_meter = -(~$y_pels_per_meter & 0xffffffff) - 1; } |
|
|
|
|
|
|
206
|
|
|
|
|
207
|
|
|
//ファイルによっては BITMAPINFOHEADER のサイズがおかしい(書き込み間違い?)ケースがある |
|
208
|
|
|
//自分でファイルサイズを元に逆算することで回避できることもあるので再計算できそうなら正当性を調べる |
|
209
|
|
|
//シークできないストリームの場合全体のファイルサイズは取得できないので、$bitmap_file_headerにサイズ申告がなければやらない |
|
210
|
|
|
if ($bitmap_file_header["size"] != 0){ |
|
211
|
|
|
$colorsize = $bit_count == 1 || $bit_count == 4 || $bit_count == 8 ? ($clr_used ? $clr_used : pow(2, $bit_count))<<2 : 0; |
|
212
|
|
|
$bodysize = $size_image ? $size_image : ((($width * $bit_count + 31) >> 3) & ~3) * abs($height); |
|
213
|
|
|
$calcsize = $bitmap_file_header["size"] - $bodysize - $colorsize - 14; |
|
214
|
|
|
|
|
215
|
|
|
//本来であれば一致するはずなのに合わない時は、値がおかしくなさそうなら(BITMAPV5HEADERの範囲内なら)計算して求めた値を採用する |
|
216
|
|
|
if ($header_size < $calcsize && 40 <= $header_size && $header_size <= 124){ |
|
217
|
|
|
$header_size = $calcsize; |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
//BITMAPV4HEADER や BITMAPV5HEADER の場合まだ読むべきデータが残っている可能性がある |
|
222
|
|
|
if ($header_size - 40 > 0){ |
|
223
|
|
|
$buf = fread($stream, $header_size - 40); |
|
224
|
|
|
if ($buf === false){ |
|
225
|
|
|
return false; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
extract(unpack( |
|
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.