From 746a5d75071a2a7c913c522ea78f2b88b87f4ce2 Mon Sep 17 00:00:00 2001 From: Sean Murphy Date: Thu, 5 Feb 2009 14:11:50 -0500 Subject: [PATCH 1/2] Fixed #905: Presenting image size limit to user. --- actions/avatarsettings.php | 2 +- actions/grouplogo.php | 2 +- lib/imagefile.php | 25 ++++++++++++++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/actions/avatarsettings.php b/actions/avatarsettings.php index 643c0e5675..79ca6b7898 100644 --- a/actions/avatarsettings.php +++ b/actions/avatarsettings.php @@ -75,7 +75,7 @@ class AvatarsettingsAction extends AccountSettingsAction function getInstructions() { - return _('You can upload your personal avatar.'); + return _('You can upload your personal avatar. The maximum file size is '.ImageFile::maxFileSize().'.'); } /** diff --git a/actions/grouplogo.php b/actions/grouplogo.php index 294005f1bb..4be7c4e12f 100644 --- a/actions/grouplogo.php +++ b/actions/grouplogo.php @@ -152,7 +152,7 @@ class GrouplogoAction extends Action function getInstructions() { - return _('You can upload a logo image for your group.'); + return _('You can upload a logo image for your group. The maximum file size is '.ImageFile::maxFileSize().'.'); } /** diff --git a/lib/imagefile.php b/lib/imagefile.php index 5e9913235c..f9f47a47ee 100644 --- a/lib/imagefile.php +++ b/lib/imagefile.php @@ -72,7 +72,7 @@ class ImageFile break; case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: - throw new Exception(_('That file is too big.')); + throw new Exception(_('That file is too big. The maximum file size is '.$this->maxFileSize().'.')); return; case UPLOAD_ERR_PARTIAL: @unlink($_FILES[$param]['tmp_name']); @@ -182,4 +182,27 @@ class ImageFile { @unlink($this->filename); } + + static function maxFileSize() + { + $limit = min(ImageFile::strToInt(ini_get('post_max_size')), ImageFile::strToInt(ini_get('upload_max_filesize'))); + return ($limit/(1024*1024)).'MB'; + } + + static function strToInt($str) + { + $unit = substr($str, -1); + $num = substr($str, 0, -1); + + switch(strtoupper($unit)){ + case 'G': + $num *= 1024; + case 'M': + $num *= 1024; + case 'K': + $num *= 1024; + } + + return $num; + } } \ No newline at end of file From d4bdb2dc1924e2753baa4cf1751acb08b6ed3cae Mon Sep 17 00:00:00 2001 From: Sean Murphy Date: Thu, 5 Feb 2009 15:01:44 -0500 Subject: [PATCH 2/2] Better fix for displaying max file size. --- actions/avatarsettings.php | 2 +- actions/grouplogo.php | 2 +- lib/common.php | 1 - lib/imagefile.php | 9 +++++++-- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/actions/avatarsettings.php b/actions/avatarsettings.php index 79ca6b7898..139d85b4c2 100644 --- a/actions/avatarsettings.php +++ b/actions/avatarsettings.php @@ -155,7 +155,7 @@ class AvatarsettingsAction extends AccountSettingsAction $this->element('input', array('name' => 'MAX_FILE_SIZE', 'type' => 'hidden', 'id' => 'MAX_FILE_SIZE', - 'value' => MAX_AVATAR_SIZE)); + 'value' => ImageFile::maxFileSize(true))); $this->elementEnd('li'); $this->elementEnd('ul'); diff --git a/actions/grouplogo.php b/actions/grouplogo.php index 4be7c4e12f..473303373f 100644 --- a/actions/grouplogo.php +++ b/actions/grouplogo.php @@ -229,7 +229,7 @@ class GrouplogoAction extends Action $this->element('input', array('name' => 'MAX_FILE_SIZE', 'type' => 'hidden', 'id' => 'MAX_FILE_SIZE', - 'value' => MAX_AVATAR_SIZE)); + 'value' => ImageFile::maxFileSize(true))); $this->elementEnd('li'); $this->elementEnd('ul'); diff --git a/lib/common.php b/lib/common.php index 825ba0ff75..482800876d 100644 --- a/lib/common.php +++ b/lib/common.php @@ -24,7 +24,6 @@ define('LACONICA_VERSION', '0.7.0'); define('AVATAR_PROFILE_SIZE', 96); define('AVATAR_STREAM_SIZE', 48); define('AVATAR_MINI_SIZE', 24); -define('MAX_AVATAR_SIZE', 256 * 1024); define('NOTICES_PER_PAGE', 20); define('PROFILES_PER_PAGE', 20); diff --git a/lib/imagefile.php b/lib/imagefile.php index f9f47a47ee..74c3d14f03 100644 --- a/lib/imagefile.php +++ b/lib/imagefile.php @@ -183,9 +183,14 @@ class ImageFile @unlink($this->filename); } - static function maxFileSize() + static function maxFileSize($return_bytes = false) { - $limit = min(ImageFile::strToInt(ini_get('post_max_size')), ImageFile::strToInt(ini_get('upload_max_filesize'))); + $limit = min(ImageFile::strToInt(ini_get('post_max_size')), ImageFile::strToInt(ini_get('upload_max_filesize')), ImageFile::strToInt(ini_get('memory_limit'))); + + if ($return_bytes) { + return $limit; + } + return ($limit/(1024*1024)).'MB'; }