apiupdateavatar.php 5.6 KB
Newer Older
1 2
<?php
 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
hannes's avatar
hannes committed
3
  ·                                                                             ·
4
  ·  Update the avatar
hannes's avatar
hannes committed
5 6
  ·                                                                             ·
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
7 8 9 10
  ·                                                                             ·
  ·                                                                             ·
  ·                             Q V I T T E R                                   ·
  ·                                                                             ·
hannes's avatar
hannes committed
11
  ·                      https://git.gnu.io/h2p/Qvitter                         ·
12 13 14 15 16 17 18 19
  ·                                                                             ·
  ·                                                                             ·
  ·                                                                             ·
  ·                                 <o)                                         ·
  ·                                  /_////                                     ·
  ·                                 (____/                                      ·
  ·                                          (o<                                ·
  ·                                   o> \\\\_\                                 ·
hannes's avatar
hannes committed
20 21
  ·                                 \\)   \____)                                ·
  ·                                                                             ·
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  ·                                                                             ·
  ·  Qvitter 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 three of the License or (at  ·
  ·  your option) any later version.                                            ·
  ·                                                                             ·
  ·  Qvitter is distributed  in hope that  it will be  useful but  WITHOUT ANY  ·
  ·  WARRANTY;  without even the implied warranty of MERCHANTABILTY 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 Qvitter. If not, see <http://www.gnu.org/licenses/>.            ·
  ·                                                                             ·
  ·  Contact h@nnesmannerhe.im if you have any questions.                       ·
hannes's avatar
hannes committed
37
  ·                                                                             ·
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
  · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */


if (!defined('GNUSOCIAL')) {
    exit(1);
}

class ApiUpdateAvatarAction extends ApiAuthAction
{
    protected $needPost = true;

    /**
     * Take arguments for running
     *
     * @param array $args $_REQUEST args
     *
     * @return boolean success flag
     */
    protected function prepare(array $args=array())
    {
        parent::prepare($args);

60 61
        $this->format = 'json';        

62 63 64 65 66 67
        $this->user = $this->auth_user;

        $this->cropW = $this->trimmed('cropW');
        $this->cropH = $this->trimmed('cropH');
        $this->cropX = $this->trimmed('cropX');
        $this->cropY = $this->trimmed('cropY');
hannes's avatar
hannes committed
68 69
        $this->img   = $this->trimmed('img');

70 71 72 73 74 75 76 77 78
        $this->img = str_replace('data:image/jpeg;base64,', '', $this->img);
        $this->img = str_replace('data:image/png;base64,', '', $this->img);
        $this->img = str_replace(' ', '+', $this->img);
        $this->img = base64_decode($this->img);

        if (empty($this->img)) {
            throw new ClientException(_('No uploaded image data.'));
        }

79 80 81 82 83 84 85 86 87 88 89 90
        return true;
    }

    /**
     * Handle the request
     *
     * @return void
     */
    protected function handle()
    {
        parent::handle();

91 92 93
        $imagefile = null;
        // write the image to a temporary file
        $fh = tmpfile();
94 95
        fwrite($fh, $this->img);
        unset($this->img);  // no need to keep it in memory
96 97 98 99 100 101 102 103 104 105
        // seek back to position 0, so we don't read EOF directly
        fseek($fh, 0);
        // read the temporary file as an uploaded image, will store File object
        $mediafile = MediaFile::fromFilehandle($fh, $this->scoped);
        // Deletes the temporary file, if it was needed we stored it in fromFilehandle
        fclose($fh);

        // Now try to get it as an ImageFile since it has some handy functions
        $imagefile = ImageFile::fromFileObject($mediafile->fileRecord);
        unset($mediafile);  // This isn't needed in memory.
hannes's avatar
hannes committed
106

107
        // Get an appropriate filename for the avatar
108
        $filename = Avatar::filename(
109
            $this->scoped->getID(),
110
            image_type_to_extension($imagefile->preferredType()),
111 112 113
            null,
            common_timestamp()
        );
114
        $imagefile->resizeTo(Avatar::path($filename), array('width'=>$this->cropW, 'height'=>$this->cropH, 'x'=>$this->cropX, 'y'=>$this->cropY, 'w'=>$this->cropW, 'h'=>$this->cropH));
115

116
        $this->scoped->setOriginal($filename);
117

118
        $twitter_user = $this->twitterUserArray($this->scoped, true);
119 120 121 122 123
        $this->initDocument('json');
        $this->showJsonObjects($twitter_user);
        $this->endDocument('json');
	}
}