1 | <?php
|
---|
2 | /**
|
---|
3 | * 画像ファイルの変換を行う
|
---|
4 | */
|
---|
5 | class ImageConverter {
|
---|
6 | var $outputImageDir; // 変換後の画像の保存先
|
---|
7 | var $outputImageType; // 変換後の画像の形式
|
---|
8 | var $outputImageWidth; // 変換後の画像の横幅
|
---|
9 | var $outputImageHeight; // 変換後の画像の高さ
|
---|
10 | var $outputImageFileSize; // 変換後の画像のファイルサイズ
|
---|
11 |
|
---|
12 | // コンストラクタ
|
---|
13 | function ImageConverter() {
|
---|
14 | $this->outputImageDir = realpath(realpath(dirname(__FILE__)));
|
---|
15 | $this->outputImageType = 'jpeg';
|
---|
16 | $this->outputImageWidth = 320;
|
---|
17 | $this->outputImageHeight = NULL;
|
---|
18 | $this->outputFileSize = 20000;
|
---|
19 | }
|
---|
20 |
|
---|
21 | // 変換実行
|
---|
22 | function execute($inputImagePath) {
|
---|
23 | // 前処理
|
---|
24 | $filestat = @stat($inputImagePath);
|
---|
25 | $imagesize = getimagesize($inputImagePath);
|
---|
26 | $inputImageWidth = $imagesize[0];
|
---|
27 | $inputImageHeight = $imagesize[1];
|
---|
28 | $inputImageType = $imagesize[2];
|
---|
29 | $outputImageName = sha1($inputImagePath . '_' . $this->outputImageWidth . '_' . $this->outputFileSize . '_' . $filestat['mtime']) . '.' . $this->outputImageType;
|
---|
30 | $outputImagePath = $this->outputImageDir . '/' . $outputImageName;
|
---|
31 |
|
---|
32 | if (is_null($this->outputImageHeight)) {
|
---|
33 | $height_was_null = TRUE;
|
---|
34 | $this->outputImageHeight = $inputImageHeight * ($this->outputImageWidth / $inputImageWidth);
|
---|
35 | } else {
|
---|
36 | $height_was_null = FALSE;
|
---|
37 | }
|
---|
38 | if ($inputImageWidth <= $this->outputImageWidth) {
|
---|
39 | if ($inputImageHeight <= $this->outputImageHeight) {
|
---|
40 | $this->outputImageWidth = $inputImageWidth;
|
---|
41 | $this->outputImageHeight = $inputImageHeight;
|
---|
42 | } else {
|
---|
43 | $this->outputImageWidth = $inputImageWidth * ($this->outputImageHeight / $inputImageHeight);
|
---|
44 | }
|
---|
45 | } else {
|
---|
46 | if ($inputImageHeight <= $this->outputImageHeight) {
|
---|
47 | $this->outputImageHeight = $inputImageHeight * ($this->outputImageWidth / $inputImageWidth);
|
---|
48 | } else {
|
---|
49 | if ($this->outputImageWidth / $inputImageWidth < $this->outputImageHeight / $inputImageHeight) {
|
---|
50 | $this->outputImageHeight = $inputImageHeight * ($this->outputImageWidth / $inputImageWidth);
|
---|
51 | } else {
|
---|
52 | $this->outputImageWidth = $inputImageWidth * ($this->outputImageHeight / $inputImageHeight);
|
---|
53 | }
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | // ファイルが存在するか確認し、存在しない場合のみ作成する
|
---|
58 | if (file_exists($outputImagePath)) {
|
---|
59 | $info['convert'] = FALSE;
|
---|
60 | } else {
|
---|
61 | // 元ファイル作成
|
---|
62 | switch($inputImageType) {
|
---|
63 | case 1:
|
---|
64 | // gif
|
---|
65 | $tempImage = imagecreatefromgif($inputImagePath);
|
---|
66 | break;
|
---|
67 | case 2:
|
---|
68 | // jpeg
|
---|
69 | $tempImage = imagecreatefromjpeg($inputImagePath);
|
---|
70 | break;
|
---|
71 | case 3:
|
---|
72 | // png
|
---|
73 | $tempImage = imagecreatefrompng($inputImagePath);
|
---|
74 | break;
|
---|
75 | case 6:
|
---|
76 | // bmp
|
---|
77 | $tempImage = imagecreatefromwbmp($inputImagePath);
|
---|
78 | break;
|
---|
79 | }
|
---|
80 |
|
---|
81 | if (!$tempImage) {
|
---|
82 | return false;
|
---|
83 | }
|
---|
84 |
|
---|
85 | $scale = 1.0;
|
---|
86 | $outputImagePathTemp = $outputImagePath . '.tmp-' . rand();
|
---|
87 | do {
|
---|
88 | // 空ファイル作成
|
---|
89 | $outputImage = ImageCreateTruecolor($this->outputImageWidth * $scale, $this->outputImageHeight * $scale);
|
---|
90 | ImageCopyResampled($outputImage, $tempImage, 0, 0, 0, 0, $this->outputImageWidth * $scale, $this->outputImageHeight * $scale, $inputImageWidth, $inputImageHeight);
|
---|
91 |
|
---|
92 | // ファイル出力
|
---|
93 |
|
---|
94 | @unlink($outputImagePathTemp);
|
---|
95 |
|
---|
96 | switch ($this->outputImageType) {
|
---|
97 | case 1:
|
---|
98 | case 'gif':
|
---|
99 | imagegif($outputImage, $outputImagePathTemp);
|
---|
100 | break;
|
---|
101 | default:
|
---|
102 | case 2:
|
---|
103 | case 'jpg':
|
---|
104 | case 'jpeg':
|
---|
105 | $quality = 75;
|
---|
106 | // 表示可能なファイルサイズ以下になるまで、10%ずつクオリティを調整する
|
---|
107 | do {
|
---|
108 | @unlink($outputImagePathTemp);
|
---|
109 | imagejpeg($outputImage, $outputImagePathTemp, $quality);
|
---|
110 | $quality -= 10;
|
---|
111 | clearstatcache();
|
---|
112 | } while (filesize($outputImagePathTemp) > $this->outputFileSize && $quality > 0);
|
---|
113 | break;
|
---|
114 | case 3:
|
---|
115 | case 'png':
|
---|
116 | imagepng($outputImage, $outputImagePathTemp);
|
---|
117 | break;
|
---|
118 | case 6:
|
---|
119 | case 'bmp':
|
---|
120 | imagewbmp($outputImage, $outputImagePathTemp);
|
---|
121 | break;
|
---|
122 | }
|
---|
123 |
|
---|
124 | // メモリ開放
|
---|
125 | imagedestroy($outputImage);
|
---|
126 |
|
---|
127 | $scale -= 0.1;
|
---|
128 | clearstatcache();
|
---|
129 | } while (filesize($outputImagePathTemp) > $this->outputFileSize && $scale >= 0.5);
|
---|
130 |
|
---|
131 | rename($outputImagePathTemp, $outputImagePath);
|
---|
132 |
|
---|
133 | // メモリ開放
|
---|
134 | imagedestroy($tempImage);
|
---|
135 |
|
---|
136 | $info['convert'] = TRUE;
|
---|
137 | }
|
---|
138 |
|
---|
139 | if ($height_was_null) {
|
---|
140 | $this->outputImageHeigh = NULL;
|
---|
141 | }
|
---|
142 |
|
---|
143 | $info['outputImagePath'] = $outputImagePath;
|
---|
144 | $info['outputImageName'] = $outputImageName;
|
---|
145 | return $info;
|
---|
146 |
|
---|
147 | }
|
---|
148 |
|
---|
149 | // Setter
|
---|
150 | function setOutputDir($outputDir) { $this->outputImageDir = $outputDir; }
|
---|
151 | function setImageType($imageType) { $this->outputImageType = $imageType; }
|
---|
152 | function setImageWidth($imageWidth) { $this->outputImageWidth = $imageWidth; }
|
---|
153 | function setImageHeight($imageHeight) { $this->outputImageHeight = $imageHeight; }
|
---|
154 | function setFileSize($fileSize) { $this->outputFileSize = $fileSize; }
|
---|
155 |
|
---|
156 | // Getter
|
---|
157 | function getOutputDir() { return $this->outputDir; }
|
---|
158 | function getImageType() { return $this->outputImageType; }
|
---|
159 | function getImageWidth() { return $this->outputImageWidth; }
|
---|
160 | function getImageHeight() { return $this->outputImageHeight; }
|
---|
161 |
|
---|
162 | /*
|
---|
163 | * PrivateMethod
|
---|
164 | */
|
---|
165 | function beforeExecute() {
|
---|
166 | }
|
---|
167 | }
|
---|
168 | ?>
|
---|