]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GNUsocialPhotos/actions/photos.php
slight cleanup, we now get the image type from getimagesize(), rather than finding...
[quix0rs-gnu-social.git] / plugins / GNUsocialPhotos / actions / photos.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 if (!defined('STATUSNET')) {
30     exit(1);
31 }
32
33 class PhotosAction extends Action
34 {
35     var $user = null;
36
37     function prepare($args)
38     {
39         parent::prepare($args);
40
41         $this->user = common_current_user();
42         common_log(LOG_INFO, "finishing prepare. user : ");
43         common_log(LOG_INFO, $this->user->nickname);
44         return true;
45     }
46
47     function handle($args)
48     {
49         parent::handle($args);
50         $this->showPage();
51     }
52
53     function title()
54     {
55         if (empty($this->user)) {
56             return _m('Hello');
57         } else {
58             return sprintf(_m('Hello, %s'), $this->user->nickname);
59         }
60     }
61
62
63     function showContent()
64     {
65         common_log(LOG_INFO, "getting to show content.\n");
66         if (empty($this->user)) {
67             // TODO: should just redirect to the login page.
68             $this->element('p', array(), 'You are not logged in');
69         } else {
70             common_log(LOG_INFO, 'fileroot : ' . $_SERVER['DOCUMENT_ROOT'] . '/file/');
71             $dir = opendir($_SERVER['DOCUMENT_ROOT'] . '/file/');
72             if ($dir === false) {
73                 $err = error_get_last();
74                 common_log(LOG_INFO, 'Error opening dir : ' . $err['message']);
75                 return;
76             }
77             $args = $this->returnToArgs();
78             foreach (array_keys($args) as $key) {
79                 common_log(LOG_INFO, $key . ' => ' . $args[$key]);
80                 if (is_array($args[$key])) {
81                     foreach (array_keys($args[$key]) as $skey) {
82                         common_log(LOG_INFO, '   ' . $skey . ' => ' . $args[$key][$skey]);
83                     }
84                 }
85             }
86             $pathparts = explode('/', $args[1]['nickname']);
87             $username = $pathparts[0];
88             $this->elementStart('ul', array('class' => 'photothumbs'));
89             while (false !== ($file = readdir($dir))) {
90                 $fparts = explode('-', $file);
91                 if ($fparts[0] == $username // uploaded by this user
92                     && ((substr($file, -4) == '.png') 
93                         || (substr($file, -4) == '.jpg') // XXX: is this needed? status.net seems to save jpgs as .jpeg
94                         || (substr($file, -5) == '.jpeg')
95                         || (substr($file, -4) == '.gif'))) { // and it's an image
96                         common_log(LOG_INFO, 'file : ' . $file);
97                         $this->elementStart('li');
98                         $this->elementStart('a', array('href' => 'http://' . common_config('site', 'server') . '/file/' . $file));
99                         if (!file_exists(INSTALLDIR . '/file/thumb.' . $file)) {
100                             $this->makeThumb($file);
101                         }
102                         $this->element('img', array('src' => 'http://' . common_config('site', 'server') . '/file/' . 'thumb.' .  $file));
103                         $this->elementEnd('a');
104                         $this->elementEnd('li');
105                 }
106             }
107             $this->elementEnd('ul');
108         }
109     }
110
111     function makeThumb($filename)
112     {
113         $height_dest = 192;
114         $width_dest = 256;
115
116         $size_src = getimagesize(INSTALLDIR . '/file/' . $filename);
117         $image_type = $size_src[2];
118         
119         switch($image_type) {
120         case IMAGETYPE_JPEG:
121             $image_src = imagecreatefromjpeg(INSTALLDIR . '/file/' . $filename);
122             break;
123         case IMAGETYPE_PNG:
124             $image_src = imagecreatefrompng(INSTALLDIR . '/file/' . $filename);
125             break;
126         case IMAGETYPE_GIF:
127             $image_src = imagecreatefromgif(INSTALLDIR . '/file/' . $filename);
128             break;
129         default:
130             return false;
131         } 
132
133         $width_src = $size_src[0];
134         $height_src = $size_src[1];
135
136         $ratio_src = (float) $width_src / (float) $height_src;
137         $ratio_dest = (float) $width_dest / (float) $height_dest;
138
139         if ($ratio_src > $ratio_dest) {
140             $height_crop = $height_src;
141             $width_crop = (int)($height_crop * $ratio_dest);
142             $x_crop = ($width_src - $width_crop) / 2;
143         } else {
144             $width_crop = $width_src;
145             $height_crop = (int)($width_crop / $ratio_dest);
146             $x_crop = 0;
147         }
148         
149         $image_dest = imagecreatetruecolor($width_dest, $height_dest);
150         
151         imagecopyresampled($image_dest, $image_src, 0, 0, $x_crop, 0, $width_dest, $height_dest, $width_crop, $height_crop);
152         switch ($image_type) {
153         case IMAGETYPE_JPEG:
154             imagejpeg($image_dest, INSTALLDIR . '/file/' . 'thumb.' . $filename, 100);
155             break;
156         case IMAGETYPE_PNG:
157             imagepng($image_dest, INSTALLDIR . '/file/thumb.' . $filename);
158             break;
159         case IMAGETYPE_GIF:
160             imagegif($image_dest, INSTALLDIR . '/file/thumb.' . $filename);
161             break;
162         }
163         
164         imagedestroy($image_src);
165         imagedestroy($image_dest);
166
167         return true;
168     }
169 }