avatar.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Christopher Schäpers <kondou@ts.unde.re>
  5. * @author Joas Schilling <nickvergessen@owncloud.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <icewind@owncloud.com>
  8. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @copyright Copyright (c) 2015, ownCloud, Inc.
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC;
  29. use OC_Image;
  30. /**
  31. * This class gets and sets users avatars.
  32. */
  33. class Avatar implements \OCP\IAvatar {
  34. private $view;
  35. /**
  36. * constructor
  37. * @param string $user user to do avatar-management with
  38. */
  39. public function __construct ($user) {
  40. $this->view = new \OC\Files\View('/'.$user);
  41. }
  42. /**
  43. * get the users avatar
  44. * @param int $size size in px of the avatar, avatars are square, defaults to 64
  45. * @return boolean|\OCP\IImage containing the avatar or false if there's no image
  46. */
  47. public function get ($size = 64) {
  48. if ($this->view->file_exists('avatar.jpg')) {
  49. $ext = 'jpg';
  50. } elseif ($this->view->file_exists('avatar.png')) {
  51. $ext = 'png';
  52. } else {
  53. return false;
  54. }
  55. $avatar = new OC_Image();
  56. $avatar->loadFromData($this->view->file_get_contents('avatar.'.$ext));
  57. $avatar->resize($size);
  58. return $avatar;
  59. }
  60. /**
  61. * Check if an avatar exists for the user
  62. *
  63. * @return bool
  64. */
  65. public function exists() {
  66. return $this->view->file_exists('avatar.jpg') || $this->view->file_exists('avatar.png');
  67. }
  68. /**
  69. * sets the users avatar
  70. * @param \OCP\IImage|resource|string $data An image object, imagedata or path to set a new avatar
  71. * @throws \Exception if the provided file is not a jpg or png image
  72. * @throws \Exception if the provided image is not valid
  73. * @throws \OC\NotSquareException if the image is not square
  74. * @return void
  75. */
  76. public function set ($data) {
  77. if($data instanceOf \OCP\IImage) {
  78. $img = $data;
  79. $data = $img->data();
  80. } else {
  81. $img = new OC_Image($data);
  82. }
  83. $type = substr($img->mimeType(), -3);
  84. if ($type === 'peg') {
  85. $type = 'jpg';
  86. }
  87. if ($type !== 'jpg' && $type !== 'png') {
  88. $l = \OC::$server->getL10N('lib');
  89. throw new \Exception($l->t("Unknown filetype"));
  90. }
  91. if (!$img->valid()) {
  92. $l = \OC::$server->getL10N('lib');
  93. throw new \Exception($l->t("Invalid image"));
  94. }
  95. if (!($img->height() === $img->width())) {
  96. throw new \OC\NotSquareException();
  97. }
  98. $this->view->unlink('avatar.jpg');
  99. $this->view->unlink('avatar.png');
  100. $this->view->file_put_contents('avatar.'.$type, $data);
  101. }
  102. /**
  103. * remove the users avatar
  104. * @return void
  105. */
  106. public function remove () {
  107. $this->view->unlink('avatar.jpg');
  108. $this->view->unlink('avatar.png');
  109. }
  110. }