apiaccountupdateprofilebanner.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3. · ·
  4. · Update the profile banner ·
  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. if (!defined('GNUSOCIAL')) {
  40. exit(1);
  41. }
  42. class ApiAccountUpdateProfileBannerAction extends ApiAuthAction
  43. {
  44. protected $needPost = true;
  45. /**
  46. * Take arguments for running
  47. *
  48. * @param array $args $_REQUEST args
  49. *
  50. * @return boolean success flag
  51. */
  52. protected function prepare(array $args=array())
  53. {
  54. parent::prepare($args);
  55. $this->format = 'json';
  56. $this->user = $this->auth_user;
  57. $this->cropW = $this->trimmed('width');
  58. $this->cropH = $this->trimmed('height');
  59. $this->cropX = $this->trimmed('offset_left');
  60. $this->cropY = $this->trimmed('offset_top');
  61. $this->img = $this->trimmed('banner');
  62. return true;
  63. }
  64. /**
  65. * Handle the request
  66. *
  67. * @return void
  68. */
  69. protected function handle()
  70. {
  71. parent::handle();
  72. // see if we have regular uploaded image data
  73. try {
  74. $mediafile = MediaFile::fromUpload('banner', $this->scoped);
  75. } catch (NoUploadedMediaException $e) {
  76. // if not we may have base64 data
  77. $this->img = str_replace('data:image/jpeg;base64,', '', $this->img);
  78. $this->img = str_replace('data:image/png;base64,', '', $this->img);
  79. $this->img = str_replace(' ', '+', $this->img);
  80. $this->img = base64_decode($this->img, true);
  81. $fh = tmpfile();
  82. fwrite($fh, $this->img);
  83. unset($this->img);
  84. fseek($fh, 0);
  85. $mediafile = MediaFile::fromFilehandle($fh, $this->scoped);
  86. }
  87. // maybe resize
  88. $width = $this->cropW;
  89. $height = $this->cropH;
  90. $scale = 1;
  91. if($width > 1200) {
  92. $scale = 1200/$width;
  93. } elseif($height > 600) {
  94. $scale = 600/$height;
  95. }
  96. $width = round($width*$scale);
  97. $height = round($height*$scale);
  98. // crop
  99. try {
  100. $imagefile = ImageFile::fromFileObject($mediafile->fileRecord);
  101. unset($mediafile);
  102. // We're just using the Avatar function to build a filename here
  103. // but we don't save it _as_ an avatar below... but in the same dir!
  104. $filename = Avatar::filename(
  105. $this->scoped->getID(),
  106. image_type_to_extension($imagefile->preferredType()),
  107. null,
  108. 'banner-'.common_timestamp()
  109. );
  110. $imagefile->resizeTo(Avatar::path($filename), array('width'=>$width, 'height'=>$height, 'x'=>$this->cropX, 'y'=>$this->cropY, 'w'=>$this->cropW, 'h'=>$this->cropH));
  111. $result['url'] = Avatar::url($filename);
  112. } catch (Exception $e) {
  113. $this->clientError(_('The image could not be resized and cropped. '.$e), 422);
  114. }
  115. // save in profile_prefs
  116. try {
  117. Profile_prefs::setData($this->scoped, 'qvitter', 'cover_photo', $result['url']);
  118. } catch (ServerException $e) {
  119. $this->clientError(_('The image could not be resized and cropped. '.$e), 422);
  120. }
  121. // return json
  122. $this->initDocument('json');
  123. $this->showJsonObjects($result);
  124. $this->endDocument('json');
  125. }
  126. }