source: branches/version-2_12-dev/data/include/image_converter.inc @ 22567

Revision 22567, 8.4 KB checked in by shutta, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.12.4)
Zend Framework PHP 標準コーディング規約のコーディングスタイルへ準拠。
classおよびfunctionの開始波括弧「{」のスタイルを修正。

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
Line 
1<?php
2/**
3 * 画像ファイルの変換を行う
4 */
5class ImageConverter
6{
7    var $outputImageDir;         // 変換後の画像の保存先
8    var $outputImageType;        // 変換後の画像の形式
9    var $outputImageWidth;       // 変換後の画像の横幅
10    var $outputImageHeight;      // 変換後の画像の高さ
11    var $outputImageFileSize;    // 変換後の画像のファイルサイズ
12
13    // コンストラクタ
14    function ImageConverter()
15    {
16        $this->outputImageDir    = realpath(realpath(dirname(__FILE__)));
17        $this->outputImageType   = 'jpg';
18        $this->outputImageWidth  = 320;
19        $this->outputImageHeight = NULL;
20        $this->outputFileSize    = 20000;
21    }
22
23    // 変換実行
24    function execute($inputImagePath)
25    {
26        // 前処理
27        $filestat         = @stat($inputImagePath);
28        $imagesize        = getimagesize($inputImagePath);
29        $inputImageWidth  = $imagesize[0];
30        $inputImageHeight = $imagesize[1];
31        $inputImageType   = $imagesize[2];
32
33        // 今回実行用の一時変数をセット
34        $output_image_width = $this->outputImageWidth;
35        $output_image_height = is_null($this->outputImageHeight)
36            ? $inputImageHeight * ($output_image_width / $inputImageWidth)
37            : $this->outputImageHeight;
38        // GIF 画像は縮小後も GIF 画像で出力する。旧機種対応などで、機種用の画像形式に変換したい場合、expr3 に固定する。
39        $output_image_type = $inputImageType == IMAGETYPE_GIF ? 'gif' : $this->outputImageType;
40
41        $outputImageName  = sha1($inputImagePath . '_' . $this->outputImageWidth . '_' . $this->outputFileSize . '_' . $filestat['mtime']) . '.' . $output_image_type;
42        $outputImagePath  = $this->outputImageDir . '/' . $outputImageName;
43
44        if ($inputImageWidth <= $output_image_width) {
45            if ($inputImageHeight <= $output_image_height) {
46                $output_image_width  = $inputImageWidth;
47                $output_image_height = $inputImageHeight;
48            } else {
49                $output_image_width = $inputImageWidth * ($output_image_height / $inputImageHeight);
50            }
51        } else {
52            if ($inputImageHeight <= $output_image_height) {
53                $output_image_height = $inputImageHeight * ($output_image_width / $inputImageWidth);
54            } else {
55                if ($output_image_width / $inputImageWidth < $output_image_height / $inputImageHeight) {
56                    $output_image_height = $inputImageHeight * ($output_image_width / $inputImageWidth);
57                } else {
58                    $output_image_width = $inputImageWidth * ($output_image_height / $inputImageHeight);
59                }
60            }
61        }
62
63        // ファイルが存在するか確認し、存在しない場合のみ作成する
64        if (file_exists($outputImagePath)) {
65            $info['convert'] = FALSE;
66        } else {
67            // 元ファイル作成
68            switch ($inputImageType) {
69                case IMAGETYPE_GIF:
70                    $tempImage = imagecreatefromgif($inputImagePath);
71                    $arrTransparentColor = $this->getTransparentColor($tempImage);
72                    if (!empty($arrTransparentColor)) {
73                        imagecolortransparent($tempImage, $arrTransparentColor);
74                    }
75                    break;
76
77                case IMAGETYPE_JPEG:
78                    $tempImage = imagecreatefromjpeg($inputImagePath);
79                    break;
80
81                case IMAGETYPE_PNG:
82                    $tempImage = imagecreatefrompng($inputImagePath);
83                    break;
84
85                case IMAGETYPE_WBMP:
86                    $tempImage = imagecreatefromwbmp($inputImagePath);
87                    break;
88            }
89
90            if (!$tempImage) {
91                return false;
92            }
93
94            $scale = 1.0;
95            $outputImagePathTemp = $outputImagePath . '.tmp-' . rand();
96            do {
97                // 空ファイル作成
98                if ($output_image_type == 'gif') {
99                    // 縮小時のノイズ防止のためインデックスカラーで処理する。特に透過色を扱う上で重要。
100                    $outputImage = ImageCreate($output_image_width * $scale, $output_image_height * $scale);
101                } else {
102                    $outputImage = ImageCreateTruecolor($output_image_width * $scale, $output_image_height * $scale);
103                }
104
105                ImageCopyResampled($outputImage, $tempImage, 0, 0, 0, 0, $output_image_width * $scale, $output_image_height * $scale, $inputImageWidth, $inputImageHeight);
106
107                // ファイル出力
108
109                @unlink($outputImagePathTemp);
110
111                switch ($output_image_type) {
112                    case 'gif':
113                        if (!empty($arrTransparentColor)) {
114                            $transparent_color_id = imagecolorexact($outputImage, $arrTransparentColor['red'], $arrTransparentColor['green'], $arrTransparentColor['blue']);
115                            imagecolortransparent($outputImage, $transparent_color_id);
116                        }
117                        imagegif($outputImage, $outputImagePathTemp);
118                        break;
119
120                    case 'jpg':
121                        $quality = 75;
122                        // 表示可能なファイルサイズ以下になるまで、10%ずつクオリティを調整する
123                        do {
124                            @unlink($outputImagePathTemp);
125                            imagejpeg($outputImage, $outputImagePathTemp, $quality);
126                            $quality -= 10;
127                            clearstatcache();
128                        } while (filesize($outputImagePathTemp) > $this->outputFileSize && $quality > 0);
129                        break;
130
131                    case 'png':
132                        imagepng($outputImage, $outputImagePathTemp);
133                        break;
134
135                    case 'bmp':
136                        imagewbmp($outputImage, $outputImagePathTemp);
137                        break;
138
139                    default:
140                        GC_Utils_Ex::gfPrintLog('不正な画像タイプ: ');
141                        break;
142                }
143
144                // メモリ開放
145                imagedestroy($outputImage);
146
147                $scale -= 0.1;
148                clearstatcache();
149            } while (filesize($outputImagePathTemp) > $this->outputFileSize && $scale >= 0.5);
150
151            rename($outputImagePathTemp, $outputImagePath);
152
153            // メモリ開放
154            imagedestroy($tempImage);
155
156            $info['convert'] = TRUE;
157        }
158
159        $info['outputImagePath']  = $outputImagePath;
160        $info['outputImageName']  = $outputImageName;
161        return $info;
162
163    }
164
165    // Setter
166    function setOutputDir($outputDir)
167    {
168        $this->outputImageDir   = $outputDir;
169    }
170    function setImageType($imageType)
171    {
172        $this->outputImageType  = $imageType;
173    }
174    function setImageWidth($imageWidth)
175    {
176        $this->outputImageWidth = $imageWidth;
177    }
178    function setImageHeight($imageHeight)
179    {
180        $this->outputImageHeight = $imageHeight;
181    }
182    function setFileSize($fileSize)
183    {
184        $this->outputFileSize   = $fileSize;
185    }
186
187    // Getter
188    function getOutputDir()
189    {
190        return $this->outputDir;
191    }
192    function getImageType()
193    {
194        return $this->outputImageType;
195    }
196    function getImageWidth()
197    {
198        return $this->outputImageWidth;
199    }
200    function getImageHeight()
201    {
202        return $this->outputImageHeight;
203    }
204
205    /*
206     * PrivateMethod
207     */
208    function beforeExecute()
209    {
210    }
211
212    /**
213     * 透過GIFの色情報を返す
214     *
215     * @access private
216     * @param resource $image imagecreatetruecolor() のような画像作成関数が返す画像リソース。
217     * @return array 色情報
218     */
219    function getTransparentColor($image)
220    {
221        $max_x = imagesx($image) - 1;
222        $max_y = imagesy($image) - 1;
223        for ($x = 0; $x <= $max_x; $x++) {
224            for ($y = 0; $y <= $max_y; $y++) {
225                $color_index = imagecolorat($image, $x, $y);
226                $arrColors = imagecolorsforindex($image, $color_index);
227                if ($arrColors['alpha'] !== 0) {
228                    return $arrColors;
229                }
230            }
231        }
232        return array();
233    }
234}
Note: See TracBrowser for help on using the repository browser.