]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/imagefile.php
Rationalize logic in showProfile()
[quix0rs-gnu-social.git] / lib / imagefile.php
1 <?php
2 /**
3  * StatusNet, 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   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Zach Copley <zach@status.net>
26  * @copyright 2008-2009 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET') && !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  StatusNet
42  * @author   Evan Prodromou <evan@status.net>
43  * @author   Zach Copley <zach@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://status.net/
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.'),
76                 ImageFile::maxFileSize()));
77             return;
78          case UPLOAD_ERR_PARTIAL:
79             @unlink($_FILES[$param]['tmp_name']);
80             throw new Exception(_('Partial upload.'));
81             return;
82          default:
83             throw new Exception(_('System error uploading file.'));
84             return;
85         }
86
87         $info = @getimagesize($_FILES[$param]['tmp_name']);
88
89         if (!$info) {
90             @unlink($_FILES[$param]['tmp_name']);
91             throw new Exception(_('Not an image or corrupt file.'));
92             return;
93         }
94
95         if ($info[2] !== IMAGETYPE_GIF &&
96             $info[2] !== IMAGETYPE_JPEG &&
97             $info[2] !== IMAGETYPE_PNG) {
98
99             @unlink($_FILES[$param]['tmp_name']);
100             throw new Exception(_('Unsupported image file format.'));
101             return;
102         }
103
104         return new ImageFile(null, $_FILES[$param]['tmp_name']);
105     }
106
107     function resize($size, $x = 0, $y = 0, $w = null, $h = null)
108     {
109         $w = ($w === null) ? $this->width:$w;
110         $h = ($h === null) ? $this->height:$h;
111
112         if (!file_exists($this->filepath)) {
113             throw new Exception(_('Lost our file.'));
114             return;
115         }
116
117         // Don't crop/scale if it isn't necessary
118         if ($size === $this->width
119             && $size === $this->height
120             && $x === 0
121             && $y === 0
122             && $w === $this->width
123             && $h === $this->height) {
124
125             $outname = Avatar::filename($this->id,
126                                         image_type_to_extension($this->type),
127                                         $size,
128                                         common_timestamp());
129             $outpath = Avatar::path($outname);
130             @copy($this->filepath, $outpath);
131             return $outname;
132         }
133
134         switch ($this->type) {
135          case IMAGETYPE_GIF:
136             $image_src = imagecreatefromgif($this->filepath);
137             break;
138          case IMAGETYPE_JPEG:
139             $image_src = imagecreatefromjpeg($this->filepath);
140             break;
141          case IMAGETYPE_PNG:
142             $image_src = imagecreatefrompng($this->filepath);
143             break;
144          default:
145             throw new Exception(_('Unknown file type'));
146             return;
147         }
148
149         $image_dest = imagecreatetruecolor($size, $size);
150
151         if ($this->type == IMAGETYPE_GIF || $this->type == IMAGETYPE_PNG) {
152
153             $transparent_idx = imagecolortransparent($image_src);
154
155             if ($transparent_idx >= 0) {
156
157                 $transparent_color = imagecolorsforindex($image_src, $transparent_idx);
158                 $transparent_idx = imagecolorallocate($image_dest, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
159                 imagefill($image_dest, 0, 0, $transparent_idx);
160                 imagecolortransparent($image_dest, $transparent_idx);
161
162             } elseif ($this->type == IMAGETYPE_PNG) {
163
164                 imagealphablending($image_dest, false);
165                 $transparent = imagecolorallocatealpha($image_dest, 0, 0, 0, 127);
166                 imagefill($image_dest, 0, 0, $transparent);
167                 imagesavealpha($image_dest, true);
168
169             }
170         }
171
172         imagecopyresampled($image_dest, $image_src, 0, 0, $x, $y, $size, $size, $w, $h);
173
174         $outname = Avatar::filename($this->id,
175                                     image_type_to_extension($this->type),
176                                     $size,
177                                     common_timestamp());
178
179         $outpath = Avatar::path($outname);
180
181         switch ($this->type) {
182          case IMAGETYPE_GIF:
183             imagegif($image_dest, $outpath);
184             break;
185          case IMAGETYPE_JPEG:
186             imagejpeg($image_dest, $outpath, 100);
187             break;
188          case IMAGETYPE_PNG:
189             imagepng($image_dest, $outpath);
190             break;
191          default:
192             throw new Exception(_('Unknown file type'));
193             return;
194         }
195
196         imagedestroy($image_src);
197         imagedestroy($image_dest);
198
199         return $outname;
200     }
201
202     function unlink()
203     {
204         @unlink($this->filename);
205     }
206
207     static function maxFileSize()
208     {
209         $value = ImageFile::maxFileSizeInt();
210
211         if ($value > 1024 * 1024) {
212             return ($value/(1024*1024)).'Mb';
213         } else if ($value > 1024) {
214             return ($value/(1024)).'kB';
215         } else {
216             return $value;
217         }
218     }
219
220     static function maxFileSizeInt()
221     {
222         return min(ImageFile::strToInt(ini_get('post_max_size')),
223                    ImageFile::strToInt(ini_get('upload_max_filesize')),
224                    ImageFile::strToInt(ini_get('memory_limit')));
225     }
226
227     static function strToInt($str)
228     {
229         $unit = substr($str, -1);
230         $num = substr($str, 0, -1);
231
232         switch(strtoupper($unit)){
233          case 'G':
234             $num *= 1024;
235          case 'M':
236             $num *= 1024;
237          case 'K':
238             $num *= 1024;
239         }
240
241         return $num;
242     }
243 }