Profile banner and background uploads with MediaFile::fromFilehandle

This commit is contained in:
Mikael Nordfeldth 2016-03-05 02:24:54 +01:00
parent 3bbfabfd4b
commit 0e0a92638f
3 changed files with 63 additions and 58 deletions

View File

@ -78,44 +78,27 @@ class ApiAccountUpdateProfileBannerAction extends ApiAuthAction
protected function handle()
{
parent::handle();
$profile = $this->user->getProfile();
// see if we have regular uploaded image data
try {
$mediafile = MediaFile::fromUpload('banner', $profile);
$mediafile = MediaFile::fromUpload('banner', $this->scoped);
} catch (NoUploadedMediaException $e) {
// if not we may have base64 data
$img = $this->img;
if(stristr($img, 'image/jpeg')) {
$img_mime = 'image/jpeg';
}
elseif(stristr($img, 'image/png')) {
// should convert to jpg here!!
$img_mime = 'image/png';
}
// i don't remember why we had to do this
$img = str_replace('data:image/jpeg;base64,', '', $img);
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$img = base64_decode($img, true);
$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);
try {
$img_filename = File::filename($profile, 'cover', $img_mime);
$img_path = File::path($img_filename);
$img_success = file_put_contents($img_path, $img);
$img_mimetype = MediaFile::getUploadedMimeType($img_path, $img_filename);
$mediafile = new MediaFile($profile, $img_filename, $img_mimetype);
} catch (Exception $e) {
$this->clientError($e, 400);
}
}
$fh = tmpfile();
fwrite($fh, $this->img);
unset($this->img);
fseek($fh, 0);
if(!$mediafile instanceof MediaFile) {
$this->clientError(_('Could not process image data.'), 400);
$mediafile = MediaFile::fromFilehandle($fh, $this->scoped);
}
// maybe resize
@ -132,16 +115,27 @@ class ApiAccountUpdateProfileBannerAction extends ApiAuthAction
// crop
try {
$imagefile = new ImageFile($mediafile->fileRecord->id, File::path($mediafile->filename));
$imagefile->resizeTo(File::path($mediafile->filename), array('width'=>$width, 'height'=>$height, 'x'=>$this->cropX, 'y'=>$this->cropY, 'w'=>$this->cropW, 'h'=>$this->cropH));
$result['url'] = File::url($mediafile->filename);
$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);
} catch (Exception $e) {
$this->clientError(_('The image could not be resized and cropped. '.$e), 422);
}
// save in profile_prefs
try {
Profile_prefs::setData($profile, 'qvitter', 'cover_photo', $result['url']);
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);
}

View File

@ -105,12 +105,10 @@ class ApiUpdateAvatarAction extends ApiAuthAction
$imagefile = ImageFile::fromFileObject($mediafile->fileRecord);
unset($mediafile); // This isn't needed in memory.
$type = $imagefile->preferredType();
// Get an appropriate filename for the avatar
$filename = Avatar::filename(
$this->scoped->getID(),
image_type_to_extension($type),
image_type_to_extension($imagefile->preferredType()),
null,
common_timestamp()
);

View File

@ -67,6 +67,15 @@ class ApiUpdateBackgroundImageAction extends ApiAuthAction
$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;
}
@ -79,31 +88,35 @@ class ApiUpdateBackgroundImageAction extends ApiAuthAction
{
parent::handle();
$profile = $this->user->getProfile();
$base64img = $this->img;
if(stristr($base64img, 'image/jpeg')) {
$base64img_mime = 'image/jpeg';
}
elseif(stristr($base64img, 'image/png')) {
// should convert to jpg here!!
$base64img_mime = 'image/png';
}
$base64img = str_replace('data:image/jpeg;base64,', '', $base64img);
$base64img = str_replace('data:image/png;base64,', '', $base64img);
$base64img = str_replace(' ', '+', $base64img);
$base64img_hash = md5($base64img);
$base64img = base64_decode($base64img);
$base64img_basename = basename('bg');
$base64img_filename = File::filename($profile, $base64img_basename, $base64img_mime);
$base64img_path = File::path($base64img_filename);
$base64img_success = file_put_contents($base64img_path, $base64img);
$base64img_mimetype = MediaFile::getUploadedMimeType($base64img_path, $base64img_filename);
$mediafile = new MediaFile($profile, $base64img_filename, $base64img_mimetype);
$imagefile = new ImageFile($mediafile->fileRecord->id, File::path($mediafile->filename));
$imagefile->resizeTo(File::path($mediafile->filename), array('width'=>1280, 'height'=>1280, 'x'=>$this->cropX, 'y'=>$this->cropY, 'w'=>$this->cropW, 'h'=>$this->cropH));
$result['url'] = File::url($mediafile->filename);
$imagefile = null;
Profile_prefs::setData($profile, 'qvitter', 'background_image', $result['url']);
// put the image data in a temporary file
$fh = tmpfile();
fwrite($fh, $this->img);
unset($this->img);
fseek($fh, 0); // go to beginning just to be sure the content is read properly
// We get a MediaFile with a File object using the filehandle
$mediafile = MediaFile::fromFilehandle($fh, $this->scoped);
// and can dispose of the temporary filehandle since we're certain we have a File on disk now
fclose($fh);
$imagefile = ImageFile::fromFileObject($mediafile->fileRecord);
unset($mediafile); // No need to keep the MediaFile around anymore, everything we need is in ImageFile
// 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,
'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);