123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
-
- if (!defined('GNUSOCIAL')) {
- exit(1);
- }
- class ApiUpdateBackgroundImageAction extends ApiAuthAction
- {
- protected $needPost = true;
-
- protected function prepare(array $args=array())
- {
- parent::prepare($args);
- $this->format = 'json';
- $this->user = $this->auth_user;
- $this->cropW = $this->trimmed('cropW');
- $this->cropH = $this->trimmed('cropW');
- $this->cropX = $this->trimmed('cropX');
- $this->cropY = $this->trimmed('cropY');
- $this->img = $this->trimmed('img');
- $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.'));
- }
- return true;
- }
-
- protected function handle()
- {
- parent::handle();
- $imagefile = null;
-
- $fh = tmpfile();
- fwrite($fh, $this->img);
- unset($this->img);
- fseek($fh, 0);
-
- $mediafile = MediaFile::fromFilehandle($fh, $this->scoped);
-
- fclose($fh);
- $imagefile = ImageFile::fromFileObject($mediafile->fileRecord);
- unset($mediafile);
-
-
- $filename = Avatar::filename(
- $this->scoped->getID(),
- image_type_to_extension($imagefile->preferredType()),
- null,
- 'bg-'.common_timestamp()
- );
- $imagefile->resizeTo(Avatar::path($filename), array('width'=>1280, 'height'=>1280, 'x'=>$this->cropX, 'y'=>$this->cropY, 'w'=>$this->cropW, 'h'=>$this->cropH));
- $result['url'] = Avatar::url($filename);
- Profile_prefs::setData($this->scoped, 'qvitter', 'background_image', $result['url']);
- $this->initDocument('json');
- $this->showJsonObjects($result);
- $this->endDocument('json');
- }
- }
|