apiupdateavatar.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3. · ·
  4. · Update the avatar
  5. · ·
  6. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  7. · ·
  8. · ·
  9. · Q V I T T E R ·
  10. · ·
  11. · https://git.gnu.io/h2p/Qvitter ·
  12. · ·
  13. · ·
  14. · ·
  15. · <o) ·
  16. · /_//// ·
  17. · (____/ ·
  18. · (o< ·
  19. · o> \\\\_\ ·
  20. · \\) \____) ·
  21. · ·
  22. · ·
  23. · Qvitter is free software: you can redistribute it and / or modify it ·
  24. · under the terms of the GNU Affero General Public License as published by ·
  25. · the Free Software Foundation, either version three of the License or (at ·
  26. · your option) any later version. ·
  27. · ·
  28. · Qvitter is distributed in hope that it will be useful but WITHOUT ANY ·
  29. · WARRANTY; without even the implied warranty of MERCHANTABILTY or FITNESS ·
  30. · FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for ·
  31. · more details. ·
  32. · ·
  33. · You should have received a copy of the GNU Affero General Public License ·
  34. · along with Qvitter. If not, see <http://www.gnu.org/licenses/>. ·
  35. · ·
  36. · Contact h@nnesmannerhe.im if you have any questions. ·
  37. · ·
  38. · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */
  39. require_once INSTALLDIR . '/lib/util/tempfile.php';
  40. if (!defined('GNUSOCIAL')) {
  41. exit(1);
  42. }
  43. class ApiUpdateAvatarAction extends ApiAuthAction
  44. {
  45. protected $needPost = true;
  46. /**
  47. * Take arguments for running
  48. *
  49. * @param array $args $_REQUEST args
  50. *
  51. * @return boolean success flag
  52. */
  53. protected function prepare(array $args=array())
  54. {
  55. parent::prepare($args);
  56. $this->format = 'json';
  57. $this->user = $this->auth_user;
  58. $this->cropW = $this->trimmed('cropW');
  59. $this->cropH = $this->trimmed('cropH');
  60. $this->cropX = $this->trimmed('cropX');
  61. $this->cropY = $this->trimmed('cropY');
  62. $this->img = $this->trimmed('img');
  63. $this->img = str_replace('data:image/jpeg;base64,', '', $this->img);
  64. $this->img = str_replace('data:image/png;base64,', '', $this->img);
  65. $this->img = str_replace(' ', '+', $this->img);
  66. $this->img = base64_decode($this->img);
  67. if (empty($this->img)) {
  68. throw new ClientException(_('No uploaded image data.'));
  69. }
  70. return true;
  71. }
  72. /**
  73. * Handle the request
  74. *
  75. * @return void
  76. */
  77. protected function handle()
  78. {
  79. parent::handle();
  80. $imagefile = null;
  81. // write the image to a temporary file
  82. $fh = new TemporaryFile('gs-mediaupload');
  83. fwrite($fh->getResource(), $this->img);
  84. unset($this->img); // no need to keep it in memory
  85. // read the temporary file as an uploaded image, will store File object
  86. fflush($fh->getResource());
  87. $mediafile = MediaFile::fromFileInfo($fh, $this->scoped);
  88. // Deletes the temporary file, if it was needed we stored it in fromFileInfo
  89. unset($fh);
  90. // Now try to get it as an ImageFile since it has some handy functions
  91. $imagefile = ImageFile::fromFileObject($mediafile->fileRecord);
  92. unset($mediafile); // This isn't needed in memory.
  93. // Get an appropriate filename for the avatar
  94. $filename = Avatar::filename(
  95. $this->scoped->getID(),
  96. image_type_to_extension($imagefile->preferredType()),
  97. null,
  98. common_timestamp()
  99. );
  100. $imagefile->resizeTo(Avatar::path($filename), array('width'=>$this->cropW, 'height'=>$this->cropH, 'x'=>$this->cropX, 'y'=>$this->cropY, 'w'=>$this->cropW, 'h'=>$this->cropH));
  101. $this->scoped->setOriginal($filename);
  102. $twitter_user = $this->twitterUserArray($this->scoped, true);
  103. $this->initDocument('json');
  104. $this->showJsonObjects($twitter_user);
  105. $this->endDocument('json');
  106. }
  107. }