avatar.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Christopher Schäpers <christopher@schaepers.it>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC;
  9. use OC_Image;
  10. /**
  11. * This class gets and sets users avatars.
  12. */
  13. class Avatar implements \OCP\IAvatar {
  14. private $view;
  15. /**
  16. * constructor
  17. * @param string $user user to do avatar-management with
  18. */
  19. public function __construct ($user) {
  20. $this->view = new \OC\Files\View('/'.$user);
  21. }
  22. /**
  23. * get the users avatar
  24. * @param int $size size in px of the avatar, avatars are square, defaults to 64
  25. * @return boolean|\OCP\IImage containing the avatar or false if there's no image
  26. */
  27. public function get ($size = 64) {
  28. if ($this->view->file_exists('avatar.jpg')) {
  29. $ext = 'jpg';
  30. } elseif ($this->view->file_exists('avatar.png')) {
  31. $ext = 'png';
  32. } else {
  33. return false;
  34. }
  35. $avatar = new OC_Image();
  36. $avatar->loadFromData($this->view->file_get_contents('avatar.'.$ext));
  37. $avatar->resize($size);
  38. return $avatar;
  39. }
  40. /**
  41. * Check if an avatar exists for the user
  42. *
  43. * @return bool
  44. */
  45. public function exists() {
  46. return $this->view->file_exists('avatar.jpg') || $this->view->file_exists('avatar.png');
  47. }
  48. /**
  49. * sets the users avatar
  50. * @param \OCP\IImage|resource|string $data An image object, imagedata or path to set a new avatar
  51. * @throws \Exception if the provided file is not a jpg or png image
  52. * @throws \Exception if the provided image is not valid
  53. * @throws \OC\NotSquareException if the image is not square
  54. * @return void
  55. */
  56. public function set ($data) {
  57. if($data instanceOf \OCP\IImage) {
  58. $img = $data;
  59. $data = $img->data();
  60. } else {
  61. $img = new OC_Image($data);
  62. }
  63. $type = substr($img->mimeType(), -3);
  64. if ($type === 'peg') {
  65. $type = 'jpg';
  66. }
  67. if ($type !== 'jpg' && $type !== 'png') {
  68. $l = \OC::$server->getL10N('lib');
  69. throw new \Exception($l->t("Unknown filetype"));
  70. }
  71. if (!$img->valid()) {
  72. $l = \OC::$server->getL10N('lib');
  73. throw new \Exception($l->t("Invalid image"));
  74. }
  75. if (!($img->height() === $img->width())) {
  76. throw new \OC\NotSquareException();
  77. }
  78. $this->view->unlink('avatar.jpg');
  79. $this->view->unlink('avatar.png');
  80. $this->view->file_put_contents('avatar.'.$type, $data);
  81. }
  82. /**
  83. * remove the users avatar
  84. * @return void
  85. */
  86. public function remove () {
  87. $this->view->unlink('avatar.jpg');
  88. $this->view->unlink('avatar.png');
  89. }
  90. }