1 | <?php |
---|
2 | /** |
---|
3 | * @copyright 2005-2008 OpenPNE Project |
---|
4 | * @license http://www.php.net/license/3_01.txt PHP License 3.01 |
---|
5 | */ |
---|
6 | |
---|
7 | /** |
---|
8 | * OpenPNEで画像処理をおこなうGeneratorのスーパークラス |
---|
9 | * |
---|
10 | * @package OpenPNE |
---|
11 | * @author Kousuke Ebihara <ebihara@tejimaya.net> |
---|
12 | */ |
---|
13 | class OpenPNE_Img_Generator |
---|
14 | { |
---|
15 | /** |
---|
16 | * @var int |
---|
17 | * @access protected |
---|
18 | */ |
---|
19 | var $jpeg_quality = 75; |
---|
20 | |
---|
21 | /** |
---|
22 | * @var int |
---|
23 | * @access protected |
---|
24 | */ |
---|
25 | var $width = 0; |
---|
26 | |
---|
27 | /** |
---|
28 | * @var int |
---|
29 | * @access protected |
---|
30 | */ |
---|
31 | var $height = 0; |
---|
32 | |
---|
33 | |
---|
34 | /** |
---|
35 | * @var string |
---|
36 | * @access private |
---|
37 | */ |
---|
38 | var $source_format; |
---|
39 | |
---|
40 | /** |
---|
41 | * @var string |
---|
42 | * @access private |
---|
43 | */ |
---|
44 | var $output_format; |
---|
45 | |
---|
46 | |
---|
47 | /** |
---|
48 | * @var string |
---|
49 | * @access protected |
---|
50 | */ |
---|
51 | var $cache_dir; |
---|
52 | |
---|
53 | /** |
---|
54 | * @var string |
---|
55 | * @access protected |
---|
56 | */ |
---|
57 | var $cache_fullpath; |
---|
58 | |
---|
59 | /** |
---|
60 | * 変換を許可する画像サイズ |
---|
61 | * |
---|
62 | * この配列にあるサイズの画像のみ出力できる。 |
---|
63 | * 空の場合は無制限となる。 |
---|
64 | * |
---|
65 | * @var array |
---|
66 | * @access private |
---|
67 | */ |
---|
68 | var $allowed_size; |
---|
69 | |
---|
70 | /** |
---|
71 | * constructor |
---|
72 | * |
---|
73 | * @param array $options |
---|
74 | */ |
---|
75 | function OpenPNE_Img_Generator($options = array()) |
---|
76 | { |
---|
77 | $this->allowed_size = (array)$GLOBALS['_OPENPNE_IMG_ALLOWED_SIZE']; |
---|
78 | if (isset($options['allowed_size'])) { |
---|
79 | $this->allowed_size = $options['allowed_size']; |
---|
80 | } |
---|
81 | |
---|
82 | if (!empty($options['jpeg_quality'])) { |
---|
83 | $this->jpeg_quality = $options['jpeg_quality']; |
---|
84 | } |
---|
85 | |
---|
86 | if (!empty($options['cache_dir'])) { |
---|
87 | $this->cache_dir = $options['cache_dir']; |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * 画像を生成する |
---|
93 | * |
---|
94 | * @param string $raw_img |
---|
95 | * @return bool |
---|
96 | */ |
---|
97 | function createImage($raw_img) |
---|
98 | { |
---|
99 | if (!$this->isAllowedSize($this->width, $this->height)) { |
---|
100 | return false; |
---|
101 | } |
---|
102 | |
---|
103 | if (!$this->isResizeImage($this->width, $this->height) && !$this->isConvertFormat()) { |
---|
104 | $this->createCacheFromRawImage($raw_img); |
---|
105 | return true; |
---|
106 | } |
---|
107 | |
---|
108 | $output_img = $this->resizeImage($raw_img); |
---|
109 | |
---|
110 | // キャッシュを生成 |
---|
111 | $this->createCacheSubdir($this->cache_dir); |
---|
112 | if ($output_img) { |
---|
113 | $this->createCache($output_img); |
---|
114 | } else { |
---|
115 | $this->createCacheFromRawImage($raw_img); |
---|
116 | } |
---|
117 | |
---|
118 | return true; |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * GDイメージのリサイズ+形式変換 |
---|
123 | * |
---|
124 | * @param resource $raw_img |
---|
125 | * @return resource output GD image |
---|
126 | */ |
---|
127 | function resizeImage($raw_img) |
---|
128 | { |
---|
129 | return $raw_img; |
---|
130 | } |
---|
131 | |
---|
132 | /** |
---|
133 | * RAW画像からキャッシュファイルを作成する |
---|
134 | * |
---|
135 | * @param string $raw_img |
---|
136 | */ |
---|
137 | function createCacheFromRawImage($raw_img) |
---|
138 | { |
---|
139 | if ($this->getOutputFormat() == 'png') { |
---|
140 | $gdimg = imagecreatefromstring($raw_img); |
---|
141 | $this->createCache($gdimg); |
---|
142 | } else { |
---|
143 | $handle = fopen($this->cache_fullpath, 'wb'); |
---|
144 | fwrite($handle, $raw_img); |
---|
145 | fclose($handle); |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | * GD画像からキャッシュファイルを作成する |
---|
151 | * |
---|
152 | * @param resource $output_gdimg |
---|
153 | */ |
---|
154 | function createCache($output_gdimg) |
---|
155 | { |
---|
156 | touch($this->cache_fullpath); |
---|
157 | switch ($this->getOutputFormat()) { |
---|
158 | case 'jpg': |
---|
159 | imagejpeg($output_gdimg, $this->cache_fullpath, $this->jpeg_quality); |
---|
160 | break; |
---|
161 | case 'gif': |
---|
162 | imagegif($output_gdimg, $this->cache_fullpath); |
---|
163 | break; |
---|
164 | case 'png': |
---|
165 | imagepng($output_gdimg, $this->cache_fullpath); |
---|
166 | break; |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | * キャッシュ書き出しのためのディレクトリを作成する |
---|
172 | * |
---|
173 | * @param string $cache_dir |
---|
174 | * @return bool |
---|
175 | */ |
---|
176 | function createCacheSubdir($cache_dir) |
---|
177 | { |
---|
178 | $subdir = dirname($this->cache_fullpath); |
---|
179 | if (!is_dir($subdir)) { |
---|
180 | // mkdir recursive |
---|
181 | $_dir = $cache_dir; |
---|
182 | if (!is_dir($_dir) && !mkdir($_dir)) { |
---|
183 | return false; |
---|
184 | } |
---|
185 | $relative_path = substr($subdir, strlen($cache_dir)+1); |
---|
186 | $parts = explode('/', $relative_path); |
---|
187 | foreach ($parts as $part) { |
---|
188 | $_dir .= '/' . $part; |
---|
189 | if (!is_dir($_dir) && !mkdir($_dir)) { |
---|
190 | return false; |
---|
191 | } |
---|
192 | } |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | /** |
---|
197 | * キャッシュ画像のファイルパスを設定する |
---|
198 | * |
---|
199 | * @param string $filename |
---|
200 | */ |
---|
201 | function setCacheFileName($filename) |
---|
202 | { |
---|
203 | $path = OpenPNE_Img::get_cache_path($filename, $this->width, $this->height, $this->getOutputFormat()); |
---|
204 | $this->cache_fullpath = $this->cache_dir . '/' . $path; |
---|
205 | } |
---|
206 | |
---|
207 | /** |
---|
208 | * 出力する画像フォーマットを設定する |
---|
209 | * |
---|
210 | * @param string $output_filename |
---|
211 | */ |
---|
212 | function setOutputFormat($output_format = '') |
---|
213 | { |
---|
214 | if ($output_format) { |
---|
215 | $this->output_format = $output_format; |
---|
216 | } else { |
---|
217 | $this->output_format = $this->getSourceFormat(); |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | /** |
---|
222 | * ファイルの高さ・幅をセット |
---|
223 | * |
---|
224 | * @param int $width |
---|
225 | * @param int $height |
---|
226 | */ |
---|
227 | function setImageSize($width = 0, $height = 0) |
---|
228 | { |
---|
229 | if (is_numeric($width)) { |
---|
230 | $this->width = $width; |
---|
231 | } |
---|
232 | |
---|
233 | if (is_numeric($height)) { |
---|
234 | $this->height = $height; |
---|
235 | } |
---|
236 | } |
---|
237 | |
---|
238 | /** |
---|
239 | * 入力画像のフォーマットを設定する |
---|
240 | * |
---|
241 | * @param string $source_filename |
---|
242 | */ |
---|
243 | function setSourceFormat($source_format) |
---|
244 | { |
---|
245 | $this->source_format = $source_format; |
---|
246 | } |
---|
247 | |
---|
248 | /** |
---|
249 | * 出力画像のフォーマットを取得する |
---|
250 | * |
---|
251 | * @return string |
---|
252 | */ |
---|
253 | function getOutputFormat() |
---|
254 | { |
---|
255 | if ($this->output_format) { |
---|
256 | return OpenPNE_Img::check_format($this->output_format); |
---|
257 | } |
---|
258 | |
---|
259 | return $this->getSourceFormat(); |
---|
260 | } |
---|
261 | |
---|
262 | /** |
---|
263 | * 入力画像のフォーマットを設定する |
---|
264 | * |
---|
265 | * @return string |
---|
266 | */ |
---|
267 | function getSourceFormat() |
---|
268 | { |
---|
269 | return $this->source_format; |
---|
270 | } |
---|
271 | |
---|
272 | /** |
---|
273 | * キャッシュファイルパスを取得する |
---|
274 | * |
---|
275 | * @return string |
---|
276 | */ |
---|
277 | function getCacheFullpath() |
---|
278 | { |
---|
279 | return $this->cache_fullpath; |
---|
280 | } |
---|
281 | |
---|
282 | /** |
---|
283 | * キャッシュからRAW画像を取得する |
---|
284 | */ |
---|
285 | function getCacheRawImage($filename) |
---|
286 | { |
---|
287 | return false; |
---|
288 | } |
---|
289 | |
---|
290 | /** |
---|
291 | * キャッシュが読み込み可能かどうか |
---|
292 | * |
---|
293 | * @return bool |
---|
294 | */ |
---|
295 | function isCacheReadable() |
---|
296 | { |
---|
297 | return is_readable($this->cache_fullpath); |
---|
298 | } |
---|
299 | |
---|
300 | /** |
---|
301 | * 変換可能な画像サイズかどうか |
---|
302 | * |
---|
303 | * @param int $width |
---|
304 | * @param int $height |
---|
305 | * @return bool |
---|
306 | */ |
---|
307 | function isAllowedSize($width = 0, $height = 0) |
---|
308 | { |
---|
309 | if (!$width && !$height) { |
---|
310 | return true; |
---|
311 | } |
---|
312 | |
---|
313 | $size = sprintf('%dx%d', $width, $height); |
---|
314 | if ($this->allowed_size && !in_array($size, $this->allowed_size)) { |
---|
315 | return false; |
---|
316 | } |
---|
317 | |
---|
318 | return true; |
---|
319 | } |
---|
320 | |
---|
321 | /** |
---|
322 | * 画像フォーマットの変換をおこなうかどうか |
---|
323 | * |
---|
324 | * @return bool |
---|
325 | */ |
---|
326 | function isConvertFormat() |
---|
327 | { |
---|
328 | if ($this->getSourceFormat() != $this->getOutputFormat()) { |
---|
329 | return true; |
---|
330 | } |
---|
331 | |
---|
332 | return false; |
---|
333 | } |
---|
334 | |
---|
335 | /** |
---|
336 | * 画像のリサイズをおこなうかどうか |
---|
337 | * |
---|
338 | * @return bool |
---|
339 | */ |
---|
340 | function isResizeImage($o_width = 0, $o_height = 0, $s_width = 0, $s_height = 0) |
---|
341 | { |
---|
342 | if (!$o_width && !$o_height) { |
---|
343 | return false; |
---|
344 | } |
---|
345 | |
---|
346 | if (!$s_width || !$s_height) { |
---|
347 | return true; |
---|
348 | } |
---|
349 | |
---|
350 | if ($s_width <= $o_width && $s_height <= $o_height) { |
---|
351 | return false; |
---|
352 | } |
---|
353 | |
---|
354 | return true; |
---|
355 | } |
---|
356 | |
---|
357 | /** |
---|
358 | * リサイズ後の画像サイズを算出する |
---|
359 | */ |
---|
360 | function calcResizedImageSize(&$o_width, &$o_height, $s_width, $s_height) |
---|
361 | { |
---|
362 | $h = $s_height; |
---|
363 | if ($o_width < $s_width) { |
---|
364 | $h = $s_height * $o_width / $s_width; |
---|
365 | } |
---|
366 | if ($o_height < $h && $o_height < $s_height) { |
---|
367 | $o_width = $s_width * $o_height / $s_height; |
---|
368 | } |
---|
369 | |
---|
370 | if ($o_height < 1.) { |
---|
371 | $o_height = 1; |
---|
372 | } |
---|
373 | if ($o_width < 1.) { |
---|
374 | $o_width = 1; |
---|
375 | } |
---|
376 | } |
---|
377 | } |
---|
378 | |
---|
379 | ?> |
---|