1 (edited by Source Code 2009-12-11 18:17)

Topic: Automatic Avatar Resizing

I did not create this mod, I simply reworked some coding from a punbb 1.2 mod, all credit goes to the original author "thame^" who's mod for 1.2 can be found here

##
##        Mod title:  Automatically resize uploaded avatars
##
##      Mod version:  1.0
##   Works on PunBB:  1.3.*
##     Release date:  2009-12-11
##         Original Author:  thame^
##         Update Author:  Source Code
##
##      Description:  PunBB's default behaviour is to reject any uploaded avatar
##                    image whose dimensions exceed those set in the options.
##                    Using this mod, PunBB will attempt to automatically resize
##                    those images to the maximum allowed dimensions, maintaining
##                    their aspect ratio. If the file size of the _resized image_
##                    is larger than allowed by the options, the upload will still
##                    be rejected.
##
##   Affected files:  profile.
##
##       Affects DB:  No
##
##            Notes:  This mod requires the GD image library, including support
##                    for at least JPEG (GIF and PNG are also recommended). If
##                    your copy of PHP is compiled without GD support (or lacks
##                    support for the uploaded image type), the mod will silently
##                    revert to the default PunBB behaviour. Visit
##                    http://php.net/gd for more information about the GD image
##                    library.
##
##       DISCLAIMER:  Please note that "mods" are not officially supported by
##                    PunBB. Installation of this modification is done at your
##                    own risk. Backup your forum database and any and all
##                    applicable files before proceeding.
##


#
#---------[ 1. OPEN ]---------------------------------------------------------
#

profile.php


#
#---------[ 2. FIND ]-----------------------------------------------
#

            // Now check the width/height
            list($width, $height, $type,) = getimagesize($forum_config['o_avatars_dir'].'/'.$id.'.tmp');
            if (empty($width) || empty($height) || $width > $forum_config['o_avatars_width'] || $height > $forum_config['o_avatars_height'])
            {
                @unlink($forum_config['o_avatars_dir'].'/'.$id.'.tmp');
                message($lang_profile['Too wide or high'].' '.$forum_config['o_avatars_width'].'x'.$forum_config['o_avatars_height'].' '.$lang_profile['pixels'].'.');
            }
            else if ($type == 1 && $uploaded_file['type'] != 'image/gif')    // Prevent dodgy uploads
            {
                @unlink($forum_config['o_avatars_dir'].'/'.$id.'.tmp');
                message($lang_profile['Bad type']);
            }            


#
#---------[ 3. REPLACE WITH ]--------------------------------------------------
#

            // Now check the width/height
            list($width, $height, $type,) = getimagesize($forum_config['o_avatars_dir'].'/'.$id.'.tmp');
            if (empty($width) || empty($height) || $width > $forum_config['o_avatars_width'] || $height > $forum_config['o_avatars_height'])
            {

                // Attempt to resize if GD is installed with support for the uploaded image type, as well as JPG for the output
                $check_type = str_replace(array(1, 2, 3), array('IMG_GIF', 'IMG_JPG', 'IMG_PNG'), $type);
                if (extension_loaded('gd') && imagetypes() & constant($check_type) && imagetypes() & IMG_JPG)
                {

                    // Load the image for processing
                    if ($type == 1) $src_img = @imagecreatefromgif($forum_config['o_avatars_dir'].'/'.$id.'.tmp');
                    elseif ($type == 2) $src_img = @imagecreatefromjpeg($forum_config['o_avatars_dir'].'/'.$id.'.tmp');
                    elseif ($type == 3) $src_img = @imagecreatefrompng($forum_config['o_avatars_dir'].'/'.$id.'.tmp');

                    if ($src_img)
                    {

                        // Figure out new image dimensions based on the maximum width
                        $new_w = $forum_config['o_avatars_width'];
                        $ratio = $height * $new_w;
                        $new_h = $ratio / $width;

                        // Do the new dimensions, based on the maximum width, fit the maximum height? If not, recalculate
                        if ($new_h > $forum_config['o_avatars_height'])
                        {
                            $new_h = $forum_config['o_avatars_height'];
                            $ratio = $width * $new_h;
                            $new_w = $ratio / $height;
                        }

                        // Resize the image
                        $new_img = imagecreatetruecolor($new_w, $new_h);
                        imagecopyresampled($new_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, $width, $height);

                        // Delete the old image and write the newly resized one
                        @unlink($forum_config['o_avatars_dir'].'/'.$id.'.tmp');
                        imagejpeg($new_img,$forum_config['o_avatars_dir'].'/'.$id.'.tmp',85);

                        // Set the extension to JPG, since that's what the resized image is now
                        $extensions[0] = '.jpg';
                    }

                    // Something went wrong while attempting to load the image for processing
                    else
                    {
                        @unlink($forum_config['o_avatars_dir'].'/'.$id.'.tmp');
                        message('An unexpected error occured while attempting to resize the image.');
                    }
                }

                // No GD installed or image type not supported; can't resize
                else
                {
                    @unlink($forum_config['o_avatars_dir'].'/'.$id.'.tmp');
                    message($lang_profile['Too wide or high'].' '.$forum_config['o_avatars_width'].'x'.$forum_config['o_avatars_height'].' '.$lang_profile['pixels'].'.');
                }
            }
            else if ($type == 1 && $uploaded_file['type'] != 'image/gif')    // Prevent dodgy uploads
            {
                @unlink($forum_config['o_avatars_dir'].'/'.$id.'.tmp');
                message($lang_profile['Bad type']);
            }

            // Make sure the file isn't too big
            if (filesize($forum_config['o_avatars_dir'].'/'.$id.'.tmp') > $forum_config['o_avatars_size'])
                message($lang_profile['Too large'].' '.$forum_config['o_avatars_size'].' '.$lang_profile['bytes'].'.');


#
#---------[ 4. FIND & DELETE ORIGINAL LINES ( AROUND 1175 ) ]-----------------------------------------------
#

            // Make sure the file isn't too big
            if ($uploaded_file['size'] > $forum_config['o_avatars_size'])
                message($lang_profile['Too large'].' '.$forum_config['o_avatars_size'].' '.$lang_profile['bytes'].'.');


#
#---------[ 5. SAVE/UPLOAD ]-------------------------------------------------
#

Again I did not create this mod and full credit goes to the original author, I have simply updated it to work with punbb 1.3.

Re: Automatic Avatar Resizing

Could you create an extension based on this mod, please?

Re: Automatic Avatar Resizing

If I'm honest I'm unsure how to the extension system works, if you can point me in the direction of some info on creating these I will be happy to work on the release

4 (edited by Gene53 2010-11-28 17:52)

Re: Automatic Avatar Resizing

Thanks for the mod, it works quite well.

Cheers!