]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/imagefile.php
Two different functions for file size
[quix0rs-gnu-social.git] / lib / imagefile.php
1 <?php
2 /**
3  * Laconica, the distributed open-source microblogging tool
4  *
5  * Abstraction for an image file
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Image
23  * @package   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @author    Zach Copley <zach@controlyourself.ca>
26  * @copyright 2008-2009 Control Yourself, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://laconi.ca/
29  */
30
31 if (!defined('LACONICA')) {
32     exit(1);
33 }
34
35 /**
36  * A wrapper on uploaded files
37  *
38  * Makes it slightly easier to accept an image file from upload.
39  *
40  * @category Image
41  * @package  Laconica
42  * @author   Evan Prodromou <evan@controlyourself.ca>
43  * @author   Zach Copley <zach@controlyourself.ca>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://laconi.ca/
46  */
47
48 class ImageFile
49 {
50     var $id;
51     var $filepath;
52     var $barename;
53     var $type;
54     var $height;
55     var $width;
56
57     function __construct($id=null, $filepath=null, $type=null, $width=null, $height=null)
58     {
59         $this->id = $id;
60         $this->filepath = $filepath;
61
62         $info = @getimagesize($this->filepath);
63         $this->type = ($info) ? $info[2]:$type;
64         $this->width = ($info) ? $info[0]:$width;
65         $this->height = ($info) ? $info[1]:$height;
66     }
67
68     static function fromUpload($param='upload')
69     {
70         switch ($_FILES[$param]['error']) {
71         case UPLOAD_ERR_OK: // success, jump out
72             break;
73         case UPLOAD_ERR_INI_SIZE:
74         case UPLOAD_ERR_FORM_SIZE:
75             throw new Exception(sprintf(_('That file is too big. The maximum file size is %d.'), $this->maxFileSize()));
76             return;
77         case UPLOAD_ERR_PARTIAL:
78             @unlink($_FILES[$param]['tmp_name']);
79             throw new Exception(_('Partial upload.'));
80             return;
81         default:
82             throw new Exception(_('System error uploading file.'));
83             return;
84         }
85
86         $info = @getimagesize($_FILES[$param]['tmp_name']);
87
88         if (!$info) {
89             @unlink($_FILES[$param]['tmp_name']);
90             throw new Exception(_('Not an image or corrupt file.'));
91             return;
92         }
93
94         if ($info[2] !== IMAGETYPE_GIF &&
95             $info[2] !== IMAGETYPE_JPEG &&
96             $info[2] !== IMAGETYPE_PNG) {
97
98             @unlink($_FILES[$param]['tmp_name']);
99             throw new Exception(_('Unsupported image file format.'));
100             return;
101         }
102
103         return new ImageFile(null, $_FILES[$param]['tmp_name']);
104     }
105
106     function resize($size, $x = 0, $y = 0, $w = null, $h = null)
107     {
108         $w = ($w === null) ? $this->width:$w;
109         $h = ($h === null) ? $this->height:$h;
110
111         if (!file_exists($this->filepath)) {
112             throw new Exception(_('Lost our file.'));
113             return;
114         }
115
116         switch ($this->type) {
117          case IMAGETYPE_GIF:
118             $image_src = imagecreatefromgif($this->filepath);
119             break;
120          case IMAGETYPE_JPEG:
121             $image_src = imagecreatefromjpeg($this->filepath);
122             break;
123          case IMAGETYPE_PNG:
124             $image_src = imagecreatefrompng($this->filepath);
125             break;
126          default:
127             throw new Exception(_('Unknown file type'));
128             return;
129         }
130
131         $image_dest = imagecreatetruecolor($size, $size);
132
133         if ($this->type == IMAGETYPE_GIF || $this->type == IMAGETYPE_PNG) {
134
135             $transparent_idx = imagecolortransparent($image_src);
136
137             if ($transparent_idx >= 0) {
138
139                 $transparent_color = imagecolorsforindex($image_src, $transparent_idx);
140                 $transparent_idx = imagecolorallocate($image_dest, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
141                 imagefill($image_dest, 0, 0, $transparent_idx);
142                 imagecolortransparent($image_dest, $transparent_idx);
143
144             } elseif ($this->type == IMAGETYPE_PNG) {
145
146                 imagealphablending($image_dest, false);
147                 $transparent = imagecolorallocatealpha($image_dest, 0, 0, 0, 127);
148                 imagefill($image_dest, 0, 0, $transparent);
149                 imagesavealpha($image_dest, true);
150
151             }
152         }
153
154         imagecopyresampled($image_dest, $image_src, 0, 0, $x, $y, $size, $size, $w, $h);
155
156         $outname = common_avatar_filename($this->id,
157                                           image_type_to_extension($this->type),
158                                           $size,
159                                           common_timestamp());
160
161         $outpath = common_avatar_path($outname);
162
163         switch ($this->type) {
164          case IMAGETYPE_GIF:
165             imagegif($image_dest, $outpath);
166             break;
167          case IMAGETYPE_JPEG:
168             imagejpeg($image_dest, $outpath);
169             break;
170          case IMAGETYPE_PNG:
171             imagepng($image_dest, $outpath);
172             break;
173          default:
174             throw new Exception(_('Unknown file type'));
175             return;
176         }
177
178         return $outname;
179     }
180
181     function unlink()
182     {
183         @unlink($this->filename);
184     }
185
186     static function maxFileSize()
187     {
188         $value = ImageFile::maxFileSizeInt();
189
190         if ($value > 1024 * 1024) {
191             return ($value/(1024*1024)).'Mb';
192         } else if ($value > 1024) {
193             return ($value/(1024)).'kB';
194         } else {
195             return $value;
196         }
197     }
198
199     static function maxFileSizeInt()
200     {
201         return min(ImageFile::strToInt(ini_get('post_max_size')),
202                    ImageFile::strToInt(ini_get('upload_max_filesize')),
203                    ImageFile::strToInt(ini_get('memory_limit')));
204     }
205
206     static function strToInt($str)
207     {
208         $unit = substr($str, -1);
209         $num = substr($str, 0, -1);
210
211         switch(strtoupper($unit)){
212             case 'G':
213                 $num *= 1024;
214             case 'M':
215                 $num *= 1024;
216             case 'K':
217                 $num *= 1024;
218         }
219
220         return $num;
221     }
222 }