Backwards compatible functions are still in Profile class.
try {
$avatar = Avatar::getUploaded($profile);
$url = $avatar->displayUrl();
- } catch (Exception $e) {
+ } catch (NoAvatarException $e) {
$url = Avatar::defaultImage(AVATAR_PROFILE_SIZE);
}
} else {
'alt' => $user->nickname));
$this->elementEnd('div');
$this->elementEnd('li');
- } catch (NoResultException $e) {
+ } catch (NoAvatarException $e) {
// No original avatar found!
}
$this->submit('delete', _m('BUTTON','Delete'));
}
$this->elementEnd('li');
- } catch (Exception $e) {
+ } catch (NoAvatarException $e) {
// No previously uploaded avatar to preview.
}
}
$this->elementEnd('Image');
$this->elementEnd('img');
- } catch (Exception $e) {
+ } catch (NoAvatarException $e) {
// No avatar for this user!
}
}
$avatar->delete();
}
- } catch (NoResultException $e) {
+ } catch (NoAvatarException $e) {
// There are no avatars to delete, a sort of success.
}
return true;
}
+ static protected $_avatars = array();
+
+ /*
+ * Get an avatar by profile. Currently can't call newSize with $height
+ */
+ public static function byProfile(Profile $target, $width=null, $height=null)
+ {
+ $width = (int) floor($width);
+ $height = !is_null($height) ? (int) floor($height) : null;
+ if (is_null($height)) {
+ $height = $width;
+ }
+
+ $size = "{$width}x{$height}";
+ if (!isset(self::$_avatars[$target->id])) {
+ self::$_avatars[$target->id] = array();
+ } elseif (isset(self::$_avatars[$target->id][$size])){
+ return self::$_avatars[$target->id][$size];
+ }
+
+ $avatar = null;
+ if (Event::handle('StartProfileGetAvatar', array($target, $width, &$avatar))) {
+ $avatar = self::pkeyGet(
+ array(
+ 'profile_id' => $target->id,
+ 'width' => $width,
+ 'height' => $height,
+ )
+ );
+ Event::handle('EndProfileGetAvatar', array($target, $width, &$avatar));
+ }
+
+ if (is_null($avatar)) {
+ // Obviously we can't find an avatar, so let's resize the original!
+ $avatar = Avatar::newSize($target, $width);
+ } elseif (!($avatar instanceof Avatar)) {
+ throw new NoAvatarException($target, $avatar);
+ }
+
+ self::$_avatars[$target->id]["{$avatar->width}x{$avatar->height}"] = $avatar;
+ return $avatar;
+ }
+
public static function getUploaded(Profile $target)
{
$avatar = new Avatar();
$avatar->profile_id = $target->id;
$avatar->original = true;
if (!$avatar->find(true)) {
- throw new NoResultException($avatar);
+ throw new NoAvatarException($target, $avatar);
}
if (!file_exists(Avatar::path($avatar->filename))) {
// The delete call may be odd for, say, unmounted filesystems
// that cause a file to currently not exist, but actually it does...
$avatar->delete();
- throw new FileNotFoundException(Avatar::path($avatar->filename));
+ throw new NoAvatarException($target, $avatar);
}
return $avatar;
}
$avatar = new Avatar();
$avatar->profile_id = $target->id;
if (!$avatar->find()) {
- throw new NoResultException($avatar);
+ throw new NoAvatarException($target, $avatar);
}
return $avatar->fetchAll();
}
}
}
+ static function urlByProfile(Profile $target, $width=null, $height=null) {
+ try {
+ return self::byProfile($target, $width, $height)->displayUrl();
+ } catch (Exception $e) {
+ return self::defaultImage($width);
+ }
+ }
+
static function defaultImage($size)
{
static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
return Theme::path('default-avatar-'.$sizenames[$size].'.png');
}
- static function newSize(Profile $target, $size) {
- $size = floor($size);
- if ($size < 1 || $size > common_config('avatar', 'maxsize')) {
+ static function newSize(Profile $target, $width) {
+ $width = (int) floor($width);
+ if ($width < 1 || $width > common_config('avatar', 'maxsize')) {
// TRANS: An error message when avatar size is unreasonable
throw new Exception(_m('Avatar size too large'));
}
$original = Avatar::getUploaded($target);
$imagefile = new ImageFile($target->id, Avatar::path($original->filename));
- $filename = $imagefile->resize($size);
+ $filename = $imagefile->resize($width);
$scaled = clone($original);
$scaled->original = false;
- $scaled->width = $size;
- $scaled->height = $size;
+ $scaled->width = $width;
+ $scaled->height = $width;
$scaled->url = Avatar::url($filename);
$scaled->filename = $filename;
$scaled->created = common_sql_now();
public function getAvatar($width, $height=null)
{
- $width = (int) floor($width);
-
- if (is_null($height)) {
- $height = $width;
- }
-
- if (isset($this->_avatars[$width])) {
- return $this->_avatars[$width];
- }
-
- if (Event::handle('StartProfileGetAvatar', array($this, $width, &$avatar))) {
- $avatar = Avatar::pkeyGet(
- array(
- 'profile_id' => $this->id,
- 'width' => $width,
- 'height' => $height
- )
- );
- Event::handle('EndProfileGetAvatar', array($this, $width, &$avatar));
- }
-
- if (is_null($avatar)) {
- // Obviously we can't find an avatar, so let's resize the original!
- $avatar = Avatar::newSize($this, $width);
- } elseif (!($avatar instanceof Avatar)) {
- throw new Exception('Bad Avatar retrieved');
- }
-
- // cache the avatar for future use
- $this->_avatars[$width] = $avatar;
-
- return $avatar;
+ return Avatar::byProfile($this, $width, $height);
}
public function setOriginal($filename)
function avatarUrl($size=AVATAR_PROFILE_SIZE)
{
- $size = floor($size);
- try {
- $avatar = $this->getAvatar($size);
- return $avatar->displayUrl();
- } catch (Exception $e) {
- return Avatar::defaultImage($size);
- }
+ return Avatar::urlByProfile($this, $size);
}
function getSubscribed($offset=0, $limit=null)
$object->link = $profile->profileurl;
try {
- $orig = Avatar::getUploaded($profile);
- $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
- } catch (Exception $e) {
+ $avatar = Avatar::getUploaded($profile);
+ $object->avatarLinks[] = AvatarLink::fromAvatar($avatar);
+ } catch (NoAvatarException $e) {
// Could not find an original avatar to link
}
foreach ($sizes as $size) {
$alink = null;
try {
- $avatar = $profile->getAvatar($size);
+ $avatar = Avatar::byProfile($profile, $size);
$alink = AvatarLink::fromAvatar($avatar);
- } catch (Exception $e) {
+ } catch (NoAvatarException $e) {
$alink = new AvatarLink();
$alink->type = 'image/png';
$alink->height = $size;
}
}
- static function fromAvatar($avatar)
+ static function fromAvatar(Avatar $avatar)
{
- if (empty($avatar)) {
- return null;
- }
$alink = new AvatarLink();
$alink->type = $avatar->mediatype;
$alink->height = $avatar->height;
--- /dev/null
+<?php
+/**
+ * StatusNet, the distributed open-source microblogging tool
+ *
+ * Class for an exception when unable to retrieve or resize a profile's avatar.
+ *
+ * PHP version 5
+ *
+ * LICENCE: This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @category Exception
+ * @package GNUSocial
+ * @author Mikael Nordfeldth <mmn@hethane.se>
+ * @copyright 2013 Free Software Foundation, Inc.
+ * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
+ * @link http://www.gnu.org/software/social/
+ */
+
+if (!defined('GNUSOCIAL')) { exit(1); }
+
+class NoAvatarException extends NoResultException
+{
+ public function __construct(Profile $target, Avatar $avatar)
+ {
+ parent::__construct($avatar);
+ }
+}
public function __construct(DB_DataObject $obj)
{
// We could log an entry here with the search parameters
- parent::__construct(_('No result found on lookup.'));
+ parent::__construct(sprintf(_('No result found on %s lookup.'), get_class($obj)));
}
}
common_debug(__METHOD__ . " - Updating profile avatar (profile_id={$profile->id}) " .
"from {$avatar->filename} to {$filename}");
// else we continue with creating a new avatar
- } catch (Exception $e) {
- // Avatar was not found. We can catch NoResultException or FileNotFoundException
+ } catch (NoAvatarException $e) {
+ // Avatar was not found. We can catch NoAvatarException or FileNotFoundException
// but generally we just want to continue creating a new avatar.
common_debug(__METHOD__ . " - No avatar found for (profile_id={$profile->id})");
}