]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GNUsocialPhotos/lib/photolib.php
Improved type-hint for following methods:
[quix0rs-gnu-social.git] / plugins / GNUsocialPhotos / lib / photolib.php
1 <?php
2 /**
3  * GNU Social
4  * Copyright (C) 2010, Free Software Foundation, Inc.
5  *
6  * PHP version 5
7  *
8  * LICENCE:
9  * 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  Widget
23  * @package   GNU Social
24  * @author    Ian Denhardt <ian@zenhack.net>
25  * @copyright 2010 Free Software Foundation, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
27  */
28
29
30 function photo_make_thumbnail($filename)
31 {
32     $height_dest = 192;
33     $width_dest = 256;
34
35     $size_src = getimagesize(INSTALLDIR . '/file/' . $filename);
36     $image_type = $size_src[2];
37     
38     switch($image_type) {
39     case IMAGETYPE_JPEG:
40         $image_src = imagecreatefromjpeg(INSTALLDIR . '/file/' . $filename);
41         break;
42     case IMAGETYPE_PNG:
43         $image_src = imagecreatefrompng(INSTALLDIR . '/file/' . $filename);
44         break;
45     case IMAGETYPE_GIF:
46         $image_src = imagecreatefromgif(INSTALLDIR . '/file/' . $filename);
47         break;
48     default:
49         return false;
50     } 
51
52     $width_src = $size_src[0];
53     $height_src = $size_src[1];
54
55     $ratio_src = (float) $width_src / (float) $height_src;
56     $ratio_dest = (float) $width_dest / (float) $height_dest;
57
58     if ($ratio_src > $ratio_dest) {
59         $height_crop = $height_src;
60         $width_crop = (int)($height_crop * $ratio_dest);
61         $x_crop = ($width_src - $width_crop) / 2;
62     } else {
63         $width_crop = $width_src;
64         $height_crop = (int)($width_crop / $ratio_dest);
65         $x_crop = 0;
66     }
67     
68     $image_dest = imagecreatetruecolor($width_dest, $height_dest);
69     
70     imagecopyresampled($image_dest, $image_src, 0, 0, $x_crop, 0, $width_dest, $height_dest, $width_crop, $height_crop);
71     switch ($image_type) {
72     case IMAGETYPE_JPEG:
73         imagejpeg($image_dest, INSTALLDIR . '/file/' . 'thumb.' . $filename, 100);
74         break;
75     case IMAGETYPE_PNG:
76         imagepng($image_dest, INSTALLDIR . '/file/thumb.' . $filename);
77         break;
78     case IMAGETYPE_GIF:
79         imagegif($image_dest, INSTALLDIR . '/file/thumb.' . $filename);
80         break;
81     }
82         
83     imagedestroy($image_src);
84     imagedestroy($image_dest);
85
86     return true;
87 }