2014-06-02 04:51:28 +09:00
|
|
|
<?php
|
|
|
|
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2016-01-18 09:24:37 +09:00
|
|
|
· ·
|
2016-01-29 05:12:34 +09:00
|
|
|
· Update the profile banner ·
|
2016-01-18 09:24:37 +09:00
|
|
|
· ·
|
|
|
|
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2014-06-02 04:51:28 +09:00
|
|
|
· ·
|
|
|
|
· ·
|
|
|
|
· Q V I T T E R ·
|
|
|
|
· ·
|
2016-01-18 09:24:37 +09:00
|
|
|
· https://git.gnu.io/h2p/Qvitter ·
|
2014-06-02 04:51:28 +09:00
|
|
|
· ·
|
|
|
|
· ·
|
|
|
|
· ·
|
|
|
|
· <o) ·
|
|
|
|
· /_//// ·
|
|
|
|
· (____/ ·
|
|
|
|
· (o< ·
|
|
|
|
· o> \\\\_\ ·
|
2016-01-18 09:24:37 +09:00
|
|
|
· \\) \____) ·
|
|
|
|
· ·
|
2014-06-02 04:51:28 +09:00
|
|
|
· ·
|
|
|
|
· 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. ·
|
2016-01-18 09:24:37 +09:00
|
|
|
· ·
|
2014-06-02 04:51:28 +09:00
|
|
|
· · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */
|
|
|
|
|
|
|
|
|
|
|
|
if (!defined('GNUSOCIAL')) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-01-29 05:12:34 +09:00
|
|
|
class ApiAccountUpdateProfileBannerAction extends ApiAuthAction
|
2014-06-02 04:51:28 +09:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2016-02-02 02:22:35 +09:00
|
|
|
$this->format = 'json';
|
|
|
|
|
2014-06-02 04:51:28 +09:00
|
|
|
$this->user = $this->auth_user;
|
|
|
|
|
2016-01-29 05:12:34 +09:00
|
|
|
$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');
|
2016-01-18 09:24:37 +09:00
|
|
|
|
2014-06-02 04:51:28 +09:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the request
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function handle()
|
|
|
|
{
|
|
|
|
parent::handle();
|
|
|
|
|
2016-01-29 05:12:34 +09:00
|
|
|
// see if we have regular uploaded image data
|
|
|
|
try {
|
2016-01-18 09:24:37 +09:00
|
|
|
|
2016-03-05 10:24:54 +09:00
|
|
|
$mediafile = MediaFile::fromUpload('banner', $this->scoped);
|
2016-01-29 05:12:34 +09:00
|
|
|
|
|
|
|
} catch (NoUploadedMediaException $e) {
|
|
|
|
|
|
|
|
// if not we may have base64 data
|
|
|
|
|
2016-03-05 10:24:54 +09:00
|
|
|
$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);
|
2016-01-29 05:12:34 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// maybe resize
|
|
|
|
$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);
|
|
|
|
|
|
|
|
// crop
|
|
|
|
try {
|
2016-03-05 10:24:54 +09:00
|
|
|
$imagefile = ImageFile::fromFileObject($mediafile->fileRecord);
|
|
|
|
unset($mediafile);
|
|
|
|
|
|
|
|
// We're just using the Avatar function to build a filename here
|
|
|
|
// but we don't save it _as_ an avatar below... but in the same dir!
|
|
|
|
$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);
|
2016-01-29 05:12:34 +09:00
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->clientError(_('The image could not be resized and cropped. '.$e), 422);
|
|
|
|
}
|
|
|
|
|
|
|
|
// save in profile_prefs
|
|
|
|
try {
|
2016-03-05 10:24:54 +09:00
|
|
|
Profile_prefs::setData($this->scoped, 'qvitter', 'cover_photo', $result['url']);
|
2016-01-29 05:12:34 +09:00
|
|
|
} catch (ServerException $e) {
|
|
|
|
$this->clientError(_('The image could not be resized and cropped. '.$e), 422);
|
|
|
|
}
|
|
|
|
|
|
|
|
// return json
|
2014-06-02 04:51:28 +09:00
|
|
|
$this->initDocument('json');
|
|
|
|
$this->showJsonObjects($result);
|
2016-01-18 09:24:37 +09:00
|
|
|
$this->endDocument('json');
|
2014-06-02 04:51:28 +09:00
|
|
|
}
|
|
|
|
}
|