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 $source_format; |
---|
16 | var $output_format; |
---|
17 | |
---|
18 | function OpenPNE_Img_Generator() |
---|
19 | { |
---|
20 | } |
---|
21 | |
---|
22 | /** |
---|
23 | * GDイメージのリサイズ+形式変換 |
---|
24 | * |
---|
25 | * @param resource $gdimg source GD image |
---|
26 | * @param int $w width |
---|
27 | * @param int $h height |
---|
28 | * @return resource output GD image |
---|
29 | */ |
---|
30 | function resizeImage($source_gdimg, $w, $h) |
---|
31 | { |
---|
32 | $s_width = imagesx($source_gdimg); |
---|
33 | $s_height = imagesy($source_gdimg); |
---|
34 | |
---|
35 | if (!$w) $w = $s_width; |
---|
36 | if (!$h) $h = $s_height; |
---|
37 | |
---|
38 | // リサイズの必要がない場合 |
---|
39 | if ($s_width <= $w && $s_height <= $h) { |
---|
40 | // 形式変換する場合はGDを通す |
---|
41 | if ($this->source_format != $this->output_format) { |
---|
42 | return $source_gdimg; |
---|
43 | } |
---|
44 | return false; |
---|
45 | } |
---|
46 | |
---|
47 | // 出力サイズ |
---|
48 | $o_width = $s_width; |
---|
49 | $o_height = $s_height; |
---|
50 | |
---|
51 | if ($w < $s_width) { |
---|
52 | $o_width = $w; |
---|
53 | $o_height = $s_height * $w / $s_width; |
---|
54 | } |
---|
55 | if ($h < $o_height && $h < $s_height) { |
---|
56 | $o_width = $s_width * $h / $s_height; |
---|
57 | $o_height = $h; |
---|
58 | } |
---|
59 | |
---|
60 | if ($o_height < 1.) { |
---|
61 | $o_height = 1; |
---|
62 | } |
---|
63 | if ($o_width < 1.) { |
---|
64 | $o_width = 1; |
---|
65 | } |
---|
66 | |
---|
67 | $output_gdimg = imagecreatetruecolor($o_width, $o_height); |
---|
68 | |
---|
69 | if (($this->output_format == 'gif') || ($this->output_format == 'png')) { |
---|
70 | $trnprt_idx_s = imagecolortransparent($source_gdimg); |
---|
71 | if ($trnprt_idx_s >= 0) { // 透過色が設定されている |
---|
72 | imagetruecolortopalette($output_gdimg, true, 256); |
---|
73 | |
---|
74 | // 入力画像から透明色取得 |
---|
75 | $trnprt_color = imagecolorsforindex($source_gdimg, $trnprt_idx_s); |
---|
76 | |
---|
77 | // 出力画像に透明色設定 |
---|
78 | imagecolorset($source_gdimg, 0, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); |
---|
79 | imagefill($output_gdimg, 0, 0, 0); |
---|
80 | imagecolortransparent($output_gdimg, 0); |
---|
81 | } elseif ($this->output_format == 'png') { // PNG-24 |
---|
82 | // アルファチャンネル情報を保存するには、アルファブレンディングを解除する必要がある |
---|
83 | imagealphablending($output_gdimg, false); |
---|
84 | imagesavealpha($output_gdimg, true); |
---|
85 | |
---|
86 | // 透過色設定 |
---|
87 | $color = imagecolorallocatealpha($output_gdimg, 0, 0, 0, 127); |
---|
88 | imagefill($output_gdimg, 0, 0, $color); |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | imagecopyresampled($output_gdimg, $source_gdimg, |
---|
93 | 0, 0, 0, 0, $o_width, $o_height, $s_width, $s_height); |
---|
94 | return $output_gdimg; |
---|
95 | } |
---|
96 | |
---|
97 | function setOutputFormat($output_format) |
---|
98 | { |
---|
99 | $this->output_format = $output_format; |
---|
100 | } |
---|
101 | |
---|
102 | function setSourceFormat($source_format) |
---|
103 | { |
---|
104 | $this->source_format = $source_format; |
---|
105 | } |
---|
106 | |
---|
107 | function getOutputFormat() |
---|
108 | { |
---|
109 | return $this->output_format; |
---|
110 | } |
---|
111 | |
---|
112 | function getSourceFormat() |
---|
113 | { |
---|
114 | return $this->source_format; |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | ?> |
---|