]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/imagefile.php
Merge branch '0.9.x' into 1.0.x
[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 %s.'),
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          case UPLOAD_ERR_NO_FILE:
83             // No file; probably just a non-AJAX submission.
84             return;
85          default:
86             common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " .
87                 $_FILES[$param]['error']);
88             throw new Exception(_('System error uploading file.'));
89             return;
90         }
91
92         $info = @getimagesize($_FILES[$param]['tmp_name']);
93
94         if (!$info) {
95             @unlink($_FILES[$param]['tmp_name']);
96             throw new Exception(_('Not an image or corrupt file.'));
97             return;
98         }
99
100         if ($info[2] !== IMAGETYPE_GIF &&
101             $info[2] !== IMAGETYPE_JPEG &&
102             $info[2] !== IMAGETYPE_PNG) {
103
104             @unlink($_FILES[$param]['tmp_name']);
105             throw new Exception(_('Unsupported image file format.'));
106             return;
107         }
108
109         return new ImageFile(null, $_FILES[$param]['tmp_name']);
110     }
111
112     function resize($size, $x = 0, $y = 0, $w = null, $h = null)
113     {
114         $w = ($w === null) ? $this->width:$w;
115         $h = ($h === null) ? $this->height:$h;
116
117         if (!file_exists($this->filepath)) {
118             throw new Exception(_('Lost our file.'));
119             return;
120         }
121
122         // Don't crop/scale if it isn't necessary
123         if ($size === $this->width
124             && $size === $this->height
125             && $x === 0
126             && $y === 0
127             && $w === $this->width
128             && $h === $this->height) {
129
130             $outname = Avatar::filename($this->id,
131                                         image_type_to_extension($this->type),
132                                         $size,
133                                         common_timestamp());
134             $outpath = Avatar::path($outname);
135             @copy($this->filepath, $outpath);
136             return $outname;
137         }
138
139         switch ($this->type) {
140          case IMAGETYPE_GIF:
141             $image_src = imagecreatefromgif($this->filepath);
142             break;
143          case IMAGETYPE_JPEG:
144             $image_src = imagecreatefromjpeg($this->filepath);
145             break;
146          case IMAGETYPE_PNG:
147             $image_src = imagecreatefrompng($this->filepath);
148             break;
149          default:
150             throw new Exception(_('Unknown file type'));
151             return;
152         }
153
154         $image_dest = imagecreatetruecolor($size, $size);
155
156         if ($this->type == IMAGETYPE_GIF || $this->type == IMAGETYPE_PNG) {
157
158             $transparent_idx = imagecolortransparent($image_src);
159
160             if ($transparent_idx >= 0) {
161
162                 $transparent_color = imagecolorsforindex($image_src, $transparent_idx);
163                 $transparent_idx = imagecolorallocate($image_dest, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
164                 imagefill($image_dest, 0, 0, $transparent_idx);
165                 imagecolortransparent($image_dest, $transparent_idx);
166
167             } elseif ($this->type == IMAGETYPE_PNG) {
168
169                 imagealphablending($image_dest, false);
170                 $transparent = imagecolorallocatealpha($image_dest, 0, 0, 0, 127);
171                 imagefill($image_dest, 0, 0, $transparent);
172                 imagesavealpha($image_dest, true);
173
174             }
175         }
176
177         imagecopyresampled($image_dest, $image_src, 0, 0, $x, $y, $size, $size, $w, $h);
178
179         $outname = Avatar::filename($this->id,
180                                     image_type_to_extension($this->type),
181                                     $size,
182                                     common_timestamp());
183
184         $outpath = Avatar::path($outname);
185
186         switch ($this->type) {
187          case IMAGETYPE_GIF:
188             imagegif($image_dest, $outpath);
189             break;
190          case IMAGETYPE_JPEG:
191             imagejpeg($image_dest, $outpath, 100);
192             break;
193          case IMAGETYPE_PNG:
194             imagepng($image_dest, $outpath);
195             break;
196          default:
197             throw new Exception(_('Unknown file type'));
198             return;
199         }
200
201         imagedestroy($image_src);
202         imagedestroy($image_dest);
203
204         return $outname;
205     }
206
207     function unlink()
208     {
209         @unlink($this->filename);
210     }
211
212     static function maxFileSize()
213     {
214         $value = ImageFile::maxFileSizeInt();
215
216         if ($value > 1024 * 1024) {
217             return ($value/(1024*1024)) . _('MB');
218         } else if ($value > 1024) {
219             return ($value/(1024)) . _('kB');
220         } else {
221             return $value;
222         }
223     }
224
225     static function maxFileSizeInt()
226     {
227         return min(ImageFile::strToInt(ini_get('post_max_size')),
228                    ImageFile::strToInt(ini_get('upload_max_filesize')),
229                    ImageFile::strToInt(ini_get('memory_limit')));
230     }
231
232     static function strToInt($str)
233     {
234         $unit = substr($str, -1);
235         $num = substr($str, 0, -1);
236
237         switch(strtoupper($unit)){
238          case 'G':
239             $num *= 1024;
240          case 'M':
241             $num *= 1024;
242          case 'K':
243             $num *= 1024;
244         }
245
246         return $num;
247     }
248 }