4 * Copyright (C) 2010, Free Software Foundation, Inc.
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.
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.
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/>.
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
29 if (!defined('STATUSNET')) {
33 class PhotosAction extends Action
37 function prepare($args)
39 parent::prepare($args);
41 $this->user = common_current_user();
42 common_log(LOG_INFO, "finishing prepare. user : ");
43 common_log(LOG_INFO, $this->user->nickname);
47 function handle($args)
49 parent::handle($args);
55 if (empty($this->user)) {
58 return sprintf(_m('Hello, %s'), $this->user->nickname);
63 function showContent()
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');
70 common_log(LOG_INFO, 'fileroot : ' . $_SERVER['DOCUMENT_ROOT'] . '/file/');
71 $dir = opendir($_SERVER['DOCUMENT_ROOT'] . '/file/');
73 $err = error_get_last();
74 common_log(LOG_INFO, 'Error opening dir : ' . $err['message']);
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]);
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);
102 $this->element('img', array('src' => 'http://' . common_config('site', 'server') . '/file/' . 'thumb.' . $file));
103 $this->elementEnd('a');
104 $this->elementEnd('li');
107 $this->elementEnd('ul');
111 function makeThumb($filename)
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;
129 $size_src = getimagesize(INSTALLDIR . '/file/' . $filename);
130 $width_src = $size_src[0];
131 $height_src = $size_src[1];
133 $ratio_src = (float) $width_src / (float) $height_src;
134 $ratio_dest = (float) $width_dest / (float) $height_dest;
136 if ($ratio_src > $ratio_dest) {
137 $height_crop = $height_src;
138 $width_crop = (int)($height_crop * $ratio_dest);
139 $x_crop = ($width_src - $width_crop) / 2;
141 $width_crop = $width_src;
142 $height_crop = (int)($width_crop / $ratio_dest);
146 $image_dest = imagecreatetruecolor($width_dest, $height_dest);
148 imagecopyresampled($image_dest, $image_src, 0, 0, $x_crop, 0, $width_dest, $height_dest, $width_crop, $height_crop);
149 switch ($image_type) {
151 imagejpeg($image_dest, INSTALLDIR . '/file/' . 'thumb.' . $filename, 100);
154 imagepng($image_dest, INSTALLDIR . '/file/thumb.' . $filename);
157 imagegif($image_dest, INSTALLDIR . '/file/thumb.' . $filename);
161 imagedestroy($image_src);
162 imagedestroy($image_dest);