]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GNUsocialPhotos/actions/photos.php
Thumbnails in the photo plugins.
[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 = 96;
114         $width_dest = 128;
115
116         if (substr($filename, -4) == '.jpg' || substr($filename, -5) == '.jpeg') {
117             $image_src = imagecreatefromjpeg(INSTALLDIR . '/file/' . $filename);
118             $image_type = IMAGETYPE_JPEG;
119         } else if(substr($filename, -4) == '.png') {
120             $image_src = imagecreatefrompng(INSTALLDIR . '/file/' . $filename);
121             $image_type = IMAGETYPE_PNG;
122         } else if(substr($filename, -4) == '.gif') {
123             $image_src = imagecreatefromgif(INSTALLDIR . '/file/' . $filename);
124             $image_type = IMAGETYPE_GIF;
125         } else {
126             return false;
127         }
128
129         $image_dest = imagecreatetruecolor($width_dest, $height_dest);
130         $size_src = getimagesize(INSTALLDIR . '/file/' . $filename);
131  
132         imagecopyresampled($image_dest, $image_src, 0, 0, 0, 0, $width_dest, $height_dest, $size_src[0], $size_src[1]);
133         switch ($image_type) {
134         case IMAGETYPE_JPEG:
135             imagejpeg($image_dest, INSTALLDIR . '/file/' . 'thumb.' . $filename, 100);
136             break;
137         case IMAGETYPE_PNG:
138             imagepng($image_dest, INSTALLDIR . '/file/thumb.' . $filename);
139             break;
140         case IMAGETYPE_GIF:
141             imagegif($image_dest, INSTALLDIR . '/file/thumb.' . $filename);
142             break;
143         }
144         
145         imagedestroy($image_src);
146         imagedestroy($image_dest);
147
148         return true;
149     }
150 }