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