Elxis CMS Forum
Support => General => Topic started by: balisto on February 19, 2009, 21:57:49
-
Hello,
is there any way to limit the file size and physical size of user's avatar-pictures? I just realized that registered users upload, 0,5MB large pictures with 3076x2308 pixel :-\
Don't know why I didn't check that before, but I was sure that files were somehow resized on upload, but probably mixed that up with some other cms I tried before!?!
Does anybody have an idea how to do that?
Cheers,
Chris
-
Elxis resizes user avatars automatically to 100X100 pixels. It is strange that your user avatars are too big.
Check if you have GD2 library available in your PHP.
You can resize existing user avatars from within Elxis administration -> media manager -> avatars -> right click on an image -> resize.
-
hi datahell,
ha, so I was right. I was pretty sure that they used to be resized with my test installation. Yes GD works fine. Any idea, what else it could be? I moved the whole installation from my test-server to the live server previously. Perhaps something I forgot to change?
Chris
-
Elxis first uploads image to the images/avatars/ folder and makes it globally writable (permissions: 666).
Then checks image's size. If it is different than 100X100 pixel it resizes it.
In order for the resize to work properly it is required:
- GD2 library to be available.
- PHP's temporary folder to be writable.
- Image to be writable by PHP.
If the original uploaded avatar is owned by the local user and Elxis could not change permissions to the image after upload then PHP won't be able to overwrite the original image with the generated thumbnail. So, it might be a permissions issue. If you have enabled FTP access in Elxis global configuration make sure the settings are correct. You can use the Change mode Elxis tool to see the image owner and permissions and try to change permissions. If you are able to change permissions your system works fine. Try to upload a new avatar and check afterwards apache's error log. Did you see any error? Elxis tmpr/ folder should always be writable by PHP (permissions: 777 or 755 if you use SUPHP).
-
Hi Datahell,
thanks for keeping on this issue with me.
I checked everything and FTP and chmod works fine. I tested a bit and I found out that it depends on the file size. If the file size is smaller than about 1,5MB everything works as it should. If the file size is larger than that the file is not resized (and when I check in MediaManager, there is an icon saying "file size exceed" instead of the thumb). Actually after uploading a picture larger than 1,5 MB for a cupple of miliseconds I see 2 red php-error bars, but it directly fowards to the confirmation page saying "You profile has been saved" so that I never get the chance to click on them to see what it says... Did this file size thing give you any idea?
P.S. just realized another interesting behaviour: if I rightclick on a "normal" image thumb in MediaManager and then click on "resize" I get the resize dialogue as expected (and resizing works fine). If I do the same on one of the "file size exceed" thumbs, it just fowards to the huge picture (whole browser window) without giving me any possibility to resize.
P.P.S. I know that it doesn't make sense that people upload avatars bigger than 1,5 MB (that's probably why I never realized this problem while testing), but unfortunately on a live site people do such straaaaaange things ::)
Chris
-
I just uploaded a 1.9MB image for avatar and was resized successfully to 100X100pixel (2.7kb).
I guess this is a limitation to your PHP. Do the following: Upload an image and be ready to click the stop ("X") button on your browser when you see the red error messages and before Elxis redirects you. You must be fast in this but you it can be done. When you succeed this click the error to see the detailed error report. Alternatively see the apache's error log, I am sure there is something interesting there about this error (maybe memory exceeded). Finally you can edit the corresponding file and comment or temporary delete the re-direct line in order Elxis not to redirect you after saving.
-
Yes tried this already a few times, but never got to stop it in time. Will give it a try with Firefox :P
-
Ha! It worked with FF ;D So here are the results of the belgian jury:
PHP Erreur [Warning]
URI: index.php
Path: /components/com_user/user.php
Line: 744
imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecoverable error:
and
PHP Erreur [Warning]
URI: index.php
Path: /components/com_user/user.php
Line: 744
imagecreatefromjpeg() [function.imagecreatefromjpeg]: '<correct absolute path to my homepage, blablabla... htdocs>/images/avatars/68_bodylotion.jpg' is not a valid JPEG file
-
Your server has available the GD2 library ?
-
that's what it says - and it des work perfectly for smaller pictures
-
It is a memory issue. When an jpeg image is loaded in the memory it needs more memory than the image's size. Tell your hosting provider to increase the allocated memory to PHP if you wish to handle large images.
Here are the benchmark results I found on the internet.
For an initial image of 262kb:
Load JPEG: allocated memory increases to 4.1 MB
Create Thumbnail: allocated memory increases to 4.4 MB
Destroy JPEG: allocated memory decreases to 1.5 MB
So, for a 2mb image the allocated memory could be huge...
More info here:
http://www.corephp.co.uk/archives/42-Interesting-memory-use-with-GD-images.html (http://www.corephp.co.uk/archives/42-Interesting-memory-use-with-GD-images.html)
-
Hi - OK. Thanks for giving a solution. I will check that, but I guess 1&1 is not the hoster to discuss such things with ::)
On the other hand: I don't necessarily want to handle large images (it's more the users that are obviously to lazy to bring their images to a smaller file size before uploading them). I would also be happy just to tell my users you cannot upload images larger than 1MB and if they do, they just get an error message (and the pic is not uploaded). Is there some possibilty to manually limit the allowed max. file size of avatar uploads or would that be a big hack?
-
It is easy to add a size limitation.
components/com_user/user.php
go to line 650:
$file = $_FILES['upavatar'];
add bellow:
if ($file['size'] < 102400) {
go to line 674:
}
Add bellow an other one }
Now only images smaller than 100kb will be uploaded.
All together:
if ( isset($_FILES['upavatar']) && is_array($_FILES['upavatar'])) {
$file = $_FILES['upavatar'];
if ($file['size'] < 102400) {
$avname = eUTF::utf8_strtolower($file['name']);
................
}
}
if ($newavatar) {
................
}
You can change 100kb to anything you like but you have to write the value in bytes (multiply X 1024, i.e.: 250 * 1024).
-
COOL! Thanks so much. Going to try that. Can I add an error message for the user the same way ("You file couldn't be uploaded because it's bigger than 1MB")?
-
The image upload is being executed after saving user's profile, so alerting him a message and redirecting him back would be confusing. The best solution would be to modify the message the user sees after saving his profile data (i.e. "Your profile was saved but the avatar upload failed, please use a smaller image."). You can set the message that will be displayed to the user by modifying the $mosmsg parameter in the mosRedirect function.
Example:
mosRedirect('page that will be redirected to', 'message that will be displayed');
-
OK. First step of your instructions accomplished successfully ;D Avatar image uploads are now limited to 100KB - that's fair enough and works great! Thanks again. I didn't get how to do the redirect in case an avatar was bigger than 100KB...
The saving process in user.php is:
if ($access->canViewProfOwn) {
if ($mosConfig_sef != 2) {
$query = "SELECT id FROM #__menu WHERE link='index.php?option=com_user&task=list'"
."\n AND published='1' AND ((language IS NULL) OR (language LIKE '%$lang%'))";
$database->setQuery($query, '#__', 1, 0);
$_Itemid = intval($database->loadResult());
if (!$_Itemid) { $_Itemid = $Itemid; }
$redLink = 'index.php?option=com_user&task=profile&uid='.$uid.'&Itemid='.$_Itemid;
} else {
$redLink = 'members/'.$row->username.'.html';
}
mosRedirect ($redLink, _E_USRDET_SAVED);
} else {
mosRedirect ('index.php', _E_USRDET_SAVED);
}
So I don't see a possibility to ask again for the file size (after it was already rejected) to redirect to another message!?! Or should I use another IF? You see I'm not a programmer, so obviously I lack some logic ???
-
From the language files:
define('_E_USRDET_SAVED','Your settings have been saved.');
Now, we want to modify the above message when the avatar upload fails.
Find (line 650 or near): $file = $_FILES['upavatar'];
Add bellow:
$avatar_ok = 0;
Find (line 665 or near): $newavatar = 1;
And next to it or bellow it: $avatar_ok= 1;
Now make lines 682 to 696 (or near) like this:
if ($access->canViewProfOwn) {
if ($mosConfig_sef != 2) {
$query = "SELECT id FROM #__menu WHERE link='index.php?option=com_user&task=list'"
."\n AND published='1' AND ((language IS NULL) OR (language LIKE '%$lang%'))";
$database->setQuery($query, '#__', 1, 0);
$_Itemid = intval($database->loadResult());
if (!$_Itemid) { $_Itemid = $Itemid; }
$redLink = 'index.php?option=com_user&task=profile&uid='.$uid.'&Itemid='.$_Itemid;
} else {
$redLink = 'members/'.$row->username.'.html';
}
$txt = (isset($avatar_ok) && !$avatar_ok) ? ' Your avatar could not be uploaded. Try with a smaller image.' : '';
mosRedirect ($redLink, _E_USRDET_SAVED.$txt);
} else {
$txt = (isset($avatar_ok) && !$avatar_ok) ? ' Your avatar could not be uploaded. Try with a smaller image.' : '';
mosRedirect ('index.php', _E_USRDET_SAVED.$txt);
}
-
Whow. Works great! Thank you. Just to be sure that I don't mess everything up: As I have a multilingual website I created a new language definition
define('_E_AVATAR_TOO_LARGE','Your image was too large. Please try again with a smaller image.');
Could I change your given code this way:
if ($access->canViewProfOwn) {
if ($mosConfig_sef != 2) {
$query = "SELECT id FROM #__menu WHERE link='index.php?option=com_user&task=list'"
."\n AND published='1' AND ((language IS NULL) OR (language LIKE '%$lang%'))";
$database->setQuery($query, '#__', 1, 0);
$_Itemid = intval($database->loadResult());
if (!$_Itemid) { $_Itemid = $Itemid; }
$redLink = 'index.php?option=com_user&task=profile&uid='.$uid.'&Itemid='.$_Itemid;
} else {
$redLink = 'members/'.$row->username.'.html';
}
$txt = (isset($avatar_ok) && !$avatar_ok) ? ' _E_AVATAR_TOO_LARGE' : '';
mosRedirect ($redLink, _E_USRDET_SAVED.$txt);
} else {
$txt = (isset($avatar_ok) && !$avatar_ok) ? '_E_AVATAR_TOO_LARGE' : '';
mosRedirect ('index.php', _E_USRDET_SAVED.$txt);
}
Or is this wrong syntax? Can I use HTML at this place to have the error message in red? For example:
$txt = (isset($avatar_ok) && !$avatar_ok) ? '<font color="red"> _E_AVATAR_TOO_LARGE</font>' : '';
Or would this be wrong? Alternatively I could use the red message-box instead of the blue one, but don't know how to manage that.
BTW: just in case, anybody else is experiencing the same problem with 1&1 (or any other) hosting, it seems that this is a known issue with gd resizing cause they usually have low memory limit for their shared hosting (some say it's only 20MB, phpinfo says it's 40 - the truth is probably somewhere in the middle). This is just one thread out of 1000s on the net: http://www.phpalbum.net/node/1197