1 | <?php |
---|
2 | /* |
---|
3 | * ²èÁü¥Õ¥¡¥¤¥ë¤ÎÊÑ´¹¤ò¹Ô¤¦ |
---|
4 | * |
---|
5 | * |
---|
6 | * |
---|
7 | */ |
---|
8 | class ImageConverter { |
---|
9 | var $outputImageDir; // ÊÑ´¹¸å¤Î²èÁü¤ÎÊݸÀè |
---|
10 | var $outputImageType; // ÊÑ´¹¸å¤Î²èÁü¤Î·Á¼° |
---|
11 | var $outputImageWidth; // ÊÑ´¹¸å¤Î²èÁü¤Î²£Éý |
---|
12 | var $outputImageHeight; // ÊÑ´¹¸å¤Î²èÁü¤Î¹â¤µ |
---|
13 | //var $outputImageFileSize; // ÊÑ´¹¸å¤Î²èÁü¤Î¥Õ¥¡¥¤¥ë¥µ¥¤¥º |
---|
14 | |
---|
15 | // ¥³¥ó¥¹¥È¥é¥¯¥¿ |
---|
16 | function ImageConverter(){ |
---|
17 | $this->outputImageDir = realpath(realpath(dirname( __FILE__))); |
---|
18 | $this->outputImageType = 'jpeg'; |
---|
19 | $this->outputImageWidth = 100; |
---|
20 | $this->outputImageHeight = NULL; |
---|
21 | //$this->outputImageFileSize = NULL; |
---|
22 | } |
---|
23 | |
---|
24 | // ÊÑ´¹¼Â¹Ô |
---|
25 | function execute($inputImagePath) { |
---|
26 | // Á°½èÍý |
---|
27 | $filestat = @stat($inputImagePath); |
---|
28 | $imagesize = getimagesize($inputImagePath); |
---|
29 | $inputImageWidth = $imagesize[0]; |
---|
30 | $inputImageHeight = $imagesize[1]; |
---|
31 | $inputImageType = $imagesize[2]; |
---|
32 | $outputImageName = base64_encode($inputImagePath . '_' . $this->outputImageWidth . '_' . $filestat['mtime']) . '.' . $this->outputImageType; |
---|
33 | $outputImagePath = $this->outputImageDir . '/' . $outputImageName; |
---|
34 | |
---|
35 | $fp = fopen('/tmp/rebelt', 'a'); fwrite($fp, "*".(is_array($imagesize) ? 'yes' : 'no')."\n"); fclose($fp); |
---|
36 | |
---|
37 | // ÊÑ´¹¸å¤Î²èÁü¤Î¹â¤µ¤¬»ØÄꤵ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¡¢ÊÑ´¹¸å¤Î²èÁü¤Î²£Éý¤«¤éµá¤á¤ë |
---|
38 | if (is_null($this->outputImageHeight)) { |
---|
39 | $this->outputImageHeight = $inputImageHeight * ($this->outputImageWidth / $inputImageWidth); |
---|
40 | } |
---|
41 | |
---|
42 | // ¥Õ¥¡¥¤¥ë¤¬Â¸ºß¤¹¤ë¤«³Îǧ¤·¡¢Â¸ºß¤·¤Ê¤¤¾ì¹ç¤Î¤ßºîÀ®¤¹¤ë |
---|
43 | if (file_exists($outputImagePath)) { |
---|
44 | $info['convert'] = FALSE; |
---|
45 | } else { |
---|
46 | // ¸µ¥Õ¥¡¥¤¥ëºîÀ® |
---|
47 | switch($inputImageType) |
---|
48 | { |
---|
49 | case 1: |
---|
50 | // gif |
---|
51 | $tempImage = imagecreatefromgif ($inputImagePath); |
---|
52 | break; |
---|
53 | case 2: |
---|
54 | // jpeg |
---|
55 | $tempImage = imagecreatefromjpeg($inputImagePath); |
---|
56 | break; |
---|
57 | case 3: |
---|
58 | // png |
---|
59 | $tempImage = imagecreatefrompng($inputImagePath); |
---|
60 | break; |
---|
61 | case 6: |
---|
62 | // bmp |
---|
63 | $tempImage = imagecreatefromwbmp($inputImagePath); |
---|
64 | break; |
---|
65 | } |
---|
66 | |
---|
67 | if (!$tempImage) { |
---|
68 | return false; |
---|
69 | } |
---|
70 | |
---|
71 | // ¶õ¥Õ¥¡¥¤¥ëºîÀ® |
---|
72 | $outputImage = ImageCreateTruecolor($this->outputImageWidth, $this->outputImageHeight); |
---|
73 | ImageCopyResampled($outputImage, $tempImage, 0, 0, 0, 0, $this->outputImageWidth, $this->outputImageHeight, $inputImageWidth, $inputImageHeight); |
---|
74 | |
---|
75 | // ¥Õ¥¡¥¤¥ë½ÐÎÏ |
---|
76 | switch($this->outputImageType) |
---|
77 | { |
---|
78 | case 1: |
---|
79 | case 'gif': |
---|
80 | imagegif($outputImage, $outputImagePath); |
---|
81 | break; |
---|
82 | default: |
---|
83 | case 2: |
---|
84 | case 'jpg': |
---|
85 | case 'jpeg': |
---|
86 | imagejpeg($outputImage, $outputImagePath); |
---|
87 | break; |
---|
88 | case 3: |
---|
89 | case 'png': |
---|
90 | imagepng($outputImage, $outputImagePath); |
---|
91 | break; |
---|
92 | case 6: |
---|
93 | case 'bmp': |
---|
94 | imagewbmp($outputImage, $outputImagePath); |
---|
95 | break; |
---|
96 | } |
---|
97 | |
---|
98 | // ¥á¥â¥ê³«Êü |
---|
99 | imagedestroy($tempImage); |
---|
100 | imagedestroy($outputImage); |
---|
101 | |
---|
102 | $info['convert'] = TRUE; |
---|
103 | } |
---|
104 | |
---|
105 | // return |
---|
106 | /* |
---|
107 | $info['inputImageWidth'] = $inputImageWidth; |
---|
108 | $info['inputImageHeight'] = $inputImageHeight; |
---|
109 | $info['inputImageType'] = $inputImageType; |
---|
110 | $info['inputImagePath'] = $inputImagePath; |
---|
111 | $info['outputImageWidth'] = $this->outputImageWidth; |
---|
112 | $info['outputImageHeight'] = $this->outputImageHeight; |
---|
113 | $info['outputImageType'] = $this->outputImageType; |
---|
114 | */ |
---|
115 | $info['outputImagePath'] = $outputImagePath; |
---|
116 | $info['outputImageName'] = $outputImageName; |
---|
117 | return $info; |
---|
118 | |
---|
119 | } |
---|
120 | |
---|
121 | // Setter |
---|
122 | function setOutputDir($outputDir) { $this->outputImageDir = $outputDir; } |
---|
123 | function setImageType($imageType) { $this->outputImageType = $imageType; } |
---|
124 | function setImageWidth($imageWidth) { $this->outputImageWidth = $imageWidth; } |
---|
125 | // function setImageHeight($imageHeight) { $this->outputImageHeight = $imageHeight; } |
---|
126 | |
---|
127 | // Getter |
---|
128 | function getOutputDir() { return $this->outputDir; } |
---|
129 | function getImageType() { return $this->outputImageType; } |
---|
130 | function getImageWidth() { return $this->outputImageWidth; } |
---|
131 | function getImageHeight() { return $this->outputImageHeight; } |
---|
132 | |
---|
133 | /* |
---|
134 | * PrivateMethod |
---|
135 | **/ |
---|
136 | function beforeExecute() { |
---|
137 | } |
---|
138 | } |
---|
139 | ?> |
---|