123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
-
- if (!defined('GNUSOCIAL')) {
- exit(1);
- }
- class ApiAccountUpdateProfileBannerAction 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('width');
- $this->cropH = $this->trimmed('height');
- $this->cropX = $this->trimmed('offset_left');
- $this->cropY = $this->trimmed('offset_top');
- $this->img = $this->trimmed('banner');
- return true;
- }
-
- protected function handle()
- {
- parent::handle();
-
- try {
- $mediafile = MediaFile::fromUpload('banner', $this->scoped);
- } catch (NoUploadedMediaException $e) {
-
- $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, true);
- $fh = tmpfile();
- fwrite($fh, $this->img);
- unset($this->img);
- fseek($fh, 0);
- $mediafile = MediaFile::fromFilehandle($fh, $this->scoped);
- }
-
- $width = $this->cropW;
- $height = $this->cropH;
- $scale = 1;
- if($width > 1200) {
- $scale = 1200/$width;
- } elseif($height > 600) {
- $scale = 600/$height;
- }
- $width = round($width*$scale);
- $height = round($height*$scale);
-
- try {
- $imagefile = ImageFile::fromFileObject($mediafile->fileRecord);
- unset($mediafile);
-
-
- $filename = Avatar::filename(
- $this->scoped->getID(),
- image_type_to_extension($imagefile->preferredType()),
- null,
- 'banner-'.common_timestamp()
- );
- $imagefile->resizeTo(Avatar::path($filename), array('width'=>$width, 'height'=>$height, 'x'=>$this->cropX, 'y'=>$this->cropY, 'w'=>$this->cropW, 'h'=>$this->cropH));
- $result['url'] = Avatar::url($filename);
- } catch (Exception $e) {
- $this->clientError(_('The image could not be resized and cropped. '.$e), 422);
- }
-
- try {
- Profile_prefs::setData($this->scoped, 'qvitter', 'cover_photo', $result['url']);
- } catch (ServerException $e) {
- $this->clientError(_('The image could not be resized and cropped. '.$e), 422);
- }
-
- $this->initDocument('json');
- $this->showJsonObjects($result);
- $this->endDocument('json');
- }
- }
|