PDA

View Full Version : Auto resizing of avatars


Dmitriy
10-16-2002, 10:00 PM
Those of you with busy boards know that a lot of times users don't have a clue of how to re-size the
pictures they want to use as avatars.

Someone may want to use a cool pic he has on the hard drive, but the picture size is too big and
user gets error message when trying to upload it as avatar. Many users don't know how to re-size the
picture and they will just give up and nevere upload the avatar.

I was ignoring this for too long, but now I came up with the solution:

This hack will re-size the uploaded avatar on the fly, meaning there are no error messages
when the picture is too large, it will be re-sized automatically before it's inserted into the
database



open the member.php file
find thess lines:

if ($imginfo[0]>$avatarmaxdimension or $imginfo[1]>$avatarmaxdimension) {
@unlink($filename);
eval("standarderror(\"".gettemplate("error_avatarbaddimensions")."\");");
}


replace with these lines:

if ($imginfo=@getimagesize($filename)) {
if ($imginfo[0]>$avatarmaxdimension or $imginfo[1]>$avatarmaxdimension) {

$image_width = $imginfo[0];
$image_height= $imginfo[1];
if ($image_height > $image_width)
{
$sizefactor = (double) ($avatarmaxdimension / $image_height);
}
else
{
$sizefactor = (double) ($avatarmaxdimension / $image_width) ;
}


$newwidth = (int) ($image_width * $sizefactor);
$newheight = (int) ($image_height * $sizefactor);

$newsize = $newwidth . "x" . $newheight;

$cmd = "/usr/local/bin/mogrify -resize $newsize "."$filename 2>&1";

exec($cmd, $exec_output, $exec_retval);
if($exec_retval > 0)
{
print "ERROR: exec() error: $exec_output[0]";
}
else
{
print "<P align=\"center\"><b>Image was resized from " . $image_width . "x" .
$image_height . " to $newsize</p>";
}

}

That's all.

Now make sure in your control panel to set a really large max file size in the avatar options because
people may now use any size pictures, so the file size may possibly be over 500K
Don't worry, after the avatar is re-sized, the new file size will be really small.

This hack uses the Mogrify utility from ImageMagick, which is free from imagemagick.org

I have this hack installed on www.examnotes.net/forums

You need at least 1 post in order to upload avatar.

NuclioN
10-16-2002, 11:54 PM
Only imagemagic or does it also work with others? It's a good hack but i think our apache is using MD or something like that.

Dmitriy
10-16-2002, 11:59 PM
As far as I know GD does not support GIF format, but GD can be used for similar function to re-size the PNG and JPG files (if GD was installed with JPG support)

For me it was more important to support GIF and JPG than PNG and JPG, also ImageMagic can support PNG, I just did not compile it with PNG support, but it's easy to add.

This hack will only work with ImageMagick.

I think GIMP will also have re-size functions but it's more complicated to use GIMP.

Dmitriy
10-17-2002, 12:06 AM
By the way, I just added an auto-resize for attached images to keep images under the max size. Basically the same think, edit the functions.php in the same manner (almost):

if ($extension=="gif" or $extension=="jpg" or $extension=="jpeg" or $extension=="jpe" or $extension=="png" or $extension=="swf") { // Picture file
if ($imginfo=@getimagesize($attachment)) {
if (($maxattachwidth>0 and $imginfo[0]>$maxattachwidth) or ($maxattachheight>0 and $imginfo[1]>$maxattachheight)) {

$image_width = $imginfo[0];
$image_height= $imginfo[1];

if ($image_width > $maxattachwidth)
{
$sizefactor = (double) ($maxattachwidth / $image_width);
}
elseif ($image_height > $maxattachheight)
{
$sizefactor = (double) ($maxattachheight / $image_height) ;
}


$newwidth = (int) ($image_width * $sizefactor);
$newheight = (int) ($image_height * $sizefactor);

$newsize = $newwidth . "x" . $newheight;

$cmd = "/usr/local/bin/mogrify -resize $newsize "."$attachment 2>&1";

exec($cmd, $exec_output, $exec_retval);
if($exec_retval > 0)
{
print "ERROR: exec() error: $exec_output[0]";
}
else
{
print "<P align=\"center\"><b>Image was resized from " . $image_width . "x" .
$image_height . " to $newsize</p>";
}

}

NuclioN
10-17-2002, 12:24 AM
I've requested the imagemagic by our sysop, i hope he will use his common sense. :D

LangTuDaTinh
10-17-2002, 04:05 AM
this hack doesn't work /w 2.2.7 right? because i got this error

Parse error: parse error, unexpected $ in /home/tvseries/public_html/forums/member.php on line 1621

can u modify it so it would work /w 2.2.7 too. thanks

Erwin
10-17-2002, 04:09 AM
Good idea. :) Good job.

Talisman
10-17-2002, 08:45 AM
Won't work with 2.2.6 either, so will have to come back for this one sometime later. Bummer.

Dmitriy
10-17-2002, 10:26 AM
It should work with any version of 2.2.X but you MUST have the ImageMagick installed on your server with support for GIF and JPG format.

If you don't have ImageMagick installed you will get parse errors.

FleaBag
10-17-2002, 02:33 PM
Nice hack. :D

Talisman
10-17-2002, 05:14 PM
It should work with any version of 2.2.X but you MUST have the ImageMagick installed on your server with support for GIF and JPG format.

Ok, that's it then. Thanks.

Dean C
10-17-2002, 05:22 PM
awesome idea mate ! :D

- miSt

Crinos
10-17-2002, 07:12 PM
hmmm ... it's a bummer that I need to specify a bigger file size beforehand for this hack to work ... I keep max avatar file sizes at 7Kb, to discourage people from using those animated GIFs that may fulfill the dimension requirements, but are almost 50Kb+ in size...

So, great hack, but I'll pass :(

alkatraz
10-23-2002, 05:37 AM
Thx!
where do I add the attachment hack tho?

Chris M
10-23-2002, 10:38 AM
admin/functions.php:)

Satan

Okiewan
10-29-2002, 12:58 AM
I'd REALLY like to implement the attachment code mentioned above... not sure what code to replace in admin/functions.php ...

This?


if ($extension=="gif" or $extension=="jpg" or $extension=="jpeg" or $extension=="jpe" or $extension=="png" or $extension=="swf") { // Picture file
if ($imginfo=@getimagesize($attachment)) {
if (($maxattachwidth>0 and $imginfo[0]>$maxattachwidth) or ($maxattachheight>0 and $imginfo[1]>$maxattachheight)) {
eval("standarderror(\"".gettemplate("error_attachbaddimensions")."\");");
}
if (!$imginfo[2]) {
eval("standarderror(\"".gettemplate("error_avatarnotimage")."\");");
}
} elseif (!$allowimgsizefailure) {
eval("standarderror(\"".gettemplate("error_avatarnotimage")."\");");
}
}

Jesus Chio
12-02-2002, 01:09 PM
me too, anyone?

Sebastian
12-02-2002, 07:29 PM
Dmitriy: I like the idea for resizing attachment images.. is it possible to make the image click-able, lets say I upload an 1280* picture, have it resize to 640* but when the user clicks it they can see the orginal size.. this would be useful.

msimplay
02-13-2003, 01:26 AM
Originally posted by Okiewan
I'd REALLY like to implement the attachment code mentioned above... not sure what code to replace in admin/functions.php ...

This?


if ($extension=="gif" or $extension=="jpg" or $extension=="jpeg" or $extension=="jpe" or $extension=="png" or $extension=="swf") { // Picture file
if ($imginfo=@getimagesize($attachment)) {
if (($maxattachwidth>0 and $imginfo[0]>$maxattachwidth) or ($maxattachheight>0 and $imginfo[1]>$maxattachheight)) {
eval("standarderror(\"".gettemplate("error_attachbaddimensions")."\");");
}
if (!$imginfo[2]) {
eval("standarderror(\"".gettemplate("error_avatarnotimage")."\");");
}
} elseif (!$allowimgsizefailure) {
eval("standarderror(\"".gettemplate("error_avatarnotimage")."\");");
}
}

i want to know where to put the code aswell :rolleyes:

LangTuDaTinh
03-08-2003, 02:46 AM
i have ImageMagick installed

no parse error but each time i upload in image

it will display "ERROR: exec() error:" but do not display which error after that line when it forwarding to my edit option page.

please help
thanks

msimplay
03-08-2003, 03:06 AM
have the boards gone dead no hacker wants to help anymore
even where it says the hacker agreed to give help

Lol infact the ones that say cannot guarantee help seem to helping more these days :P

LangTuDaTinh
03-09-2003, 07:21 PM
PHP SAFE MODE MUST BE OFF TO USE THIS HACK. ELSE IT' WON'T WORK

warpeditor
03-10-2003, 12:52 PM
It doesn't work with 2.3.0, does it? I got parse error.

Parse error: parse error, unexpected '}' in /home/somewhere/public_html/vb/admin/functions.php on line 1829

Imagemagick installed, safe upload mode off.

LangTuDaTinh
03-21-2003, 06:16 PM
check your code....remove 1 extra "}"

it should work

i found another bug....if a gif dimension is smaller than avatarmaxdimension in setting then this hack will not run. and the size will not change

forexample a user have a gif avatar with size 1mb and dimension is smaller than the one i specified in setting than he will be able to upload his avatar.... so u need to change the code in admin/functions.php

Pikok
08-02-2003, 04:23 AM
I'm running linux on my server, and they're saying they support everything except some, but as you'd suppose. Linux isnt there!

corsacrazy
10-24-2003, 09:17 PM
any updates on this would be great

Keith
02-09-2004, 01:47 AM
I don't have ImageMagick, but we do have GD v2 installed. PhotoPost uses GD2 to resize jpeg images. This appears to be the line you're using to resize the image.
$cmd = "/usr/local/bin/mogrify -resize $newsize "."$attachment 2>&1";
Is it as simple as using a GD equivalent command to replace this? If so, can you list it out here?

sgtmaj
02-09-2004, 03:33 PM
By the way, I just added an auto-resize for attached images to keep images under the max size. Basically the same think, edit the functions.php in the same manner (almost):

if ($extension=="gif" or $extension=="jpg" or $extension=="jpeg" or $extension=="jpe" or $extension=="png" or $extension=="swf") { // Picture file
if ($imginfo=@getimagesize($attachment)) {
if (($maxattachwidth>0 and $imginfo[0]>$maxattachwidth) or ($maxattachheight>0 and $imginfo[1]>$maxattachheight)) {

$image_width = $imginfo[0];
$image_height= $imginfo[1];

if ($image_width > $maxattachwidth)
{
$sizefactor = (double) ($maxattachwidth / $image_width);
}
elseif ($image_height > $maxattachheight)
{
$sizefactor = (double) ($maxattachheight / $image_height) ;
}


$newwidth = (int) ($image_width * $sizefactor);
$newheight = (int) ($image_height * $sizefactor);

$newsize = $newwidth . "x" . $newheight;

$cmd = "/usr/local/bin/mogrify -resize $newsize "."$attachment 2>&1";

exec($cmd, $exec_output, $exec_retval);
if($exec_retval > 0)
{
print "ERROR: exec() error: $exec_output[0]";
}
else
{
print "<P align=\"center\"><b>Image was resized from " . $image_width . "x" .
$image_height . " to $newsize</p>";
}

}

what code in my current functions.php do I replace your code with???

Willard Reece
02-09-2004, 04:05 PM
Looks like this seems to work , but on either part you use it says it makes the image smaller but when you look at the image , it is the same size as before ,
Post a attachment on forums and it says image has been resized, but it don't work.

could this be in imagemajicK not set right???

even the avatar part works the same, so unlees its something to do with the vb admin settings then it has to be in the imagemajicK setting I would think.....