From 3bbfabfd4bc81ef1b049e949edb6f6b2c3173a23 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Sat, 5 Mar 2016 01:42:13 +0100 Subject: [PATCH] Don't keep $this->img in memory the whole time --- actions/apiupdateavatar.php | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/actions/apiupdateavatar.php b/actions/apiupdateavatar.php index bfe5792..f34f449 100644 --- a/actions/apiupdateavatar.php +++ b/actions/apiupdateavatar.php @@ -67,6 +67,15 @@ class ApiUpdateAvatarAction 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,21 +88,11 @@ class ApiUpdateAvatarAction extends ApiAuthAction { parent::handle(); - $base64img = $this->img; - $base64img = str_replace('data:image/jpeg;base64,', '', $base64img); - $base64img = str_replace('data:image/png;base64,', '', $base64img); - $base64img = str_replace(' ', '+', $base64img); - $base64img = base64_decode($base64img); - - if (empty($base64img)) { - throw new ClientException(_('No uploaded image data.')); - } - $imagefile = null; // write the image to a temporary file $fh = tmpfile(); - fwrite($fh, $base64img); - unset($base64img); // no need to keep it in memory + fwrite($fh, $this->img); + unset($this->img); // no need to keep it in memory // 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