Changeset 510

Show
Ignore:
Timestamp:
07/24/07 12:10:07 (1 year ago)
Author:
louisedade
Message:

avatar-upload: committing version 0.7 - added admin page, unsharp mask and changed file layout

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • avatar-upload/trunk/additional-files/avatar-upload.php

    r501 r510  
    11<?php 
    22/* 
    3 Plugin Name: Avatar Upload 
    4 Plugin URI: http://bbpress.org/plugins/topic/46 
    5 Version: 0.6.2 
    6 Description: Allows users to upload an avatar (gif, jpeg/jpg or png) image to bbPress. 
    7 Author: Louise Dade 
    8 Author URI: http://www.classical-webdesigns.co.uk/ 
     3This file is part of the Avatar Upload plugin 
    94*/ 
    105 
     
    145140                                break; 
    146141                        case 8: // UPLOAD_ERR_EXTENSION (since PHP 5.2.0) 
    147                                 $error_message = __("The file is not a valid GIF, JPG/JPEG or PNG image-type."); 
     142                                $error_message = __("You are not allowed to upload images of the type: ".strtoupper($img_ext)); 
    148143                                break; 
    149144                        case 9: // custom error code 
     
    154149                                break; 
    155150                        case 11: // custom error code 
    156                                 $error_message = __("The file could not be saved to the 'avatars' folder."); 
     151                                $error_message = __("The file could not be saved to the {$config->avatar_dir} folder."); 
    157152                                break; 
    158153                        default: // unknown error (this probably won't ever happen) 
     
    208203                return array($img_w, $img_h); 
    209204        } 
     205 
     206        // Can we use a truecolor image? 
     207        $truecolor = function_exists('imagecreatetruecolor'); 
    210208 
    211209        // Resize the image preserving image type 
     
    224222                        // JPEG 
    225223                        $im1 = @imagecreatefromjpeg($img_temp); 
    226                         $im2 = @imagecreatetruecolor($new_width, $new_height) or $error = 1
     224                        $im2 = ($truecolor) ? @imagecreatetruecolor($new_width, $new_height) : @imagecreate($new_width, $new_height)
    227225                        @imagecopyresampled ($im2, $im1, 0, 0, 0, 0, $new_width, $new_height, $img_w, $img_h); 
     226                        $im2 = ($truecolor) ? do_unsharp_mask($im2, $config->use_unsharpmask) : $im2; 
    228227                        @imagejpeg($im2, $img_temp, 100); // quality integer might become config variable 
    229228                        break; 
     
    232231                        // PNG 
    233232                        $im1 = @imagecreatefrompng($img_temp); 
    234                         $im2 = @imagecreatetruecolor($new_width, $new_height) or $error = 1
     233                        $im2 = ($truecolor) ? @imagecreatetruecolor($new_width, $new_height) : @imagecreate($new_width, $new_height)
    235234                        @imagecopyresampled ($im2, $im1, 0, 0, 0, 0, $new_width, $new_height, $img_w, $img_h); 
     235                        $im2 = ($truecolor) ? do_unsharp_mask($im2, $config->use_unsharpmask) : $im2; 
    236236                        @imagepng($im2, $img_temp); 
    237237                        break; 
  • avatar-upload/trunk/bb-avatar-upload.php

    r501 r510  
    33Plugin Name: Avatar Upload 
    44Plugin URI: http://bbpress.org/plugins/topic/46 
    5 Version: 0.6.2 
     5Version: 0.7 
    66Description: Allows users to upload an avatar (gif, jpeg/jpg or png) image to bbPress. 
    77Author: Louise Dade 
     
    1414        function avatarupload_config() 
    1515        { 
     16                // Set default options 
     17                $options = array( 
     18                        'avatar_dir'     => 'avatars/', 
     19                        'max_width'      => 150, 
     20                        'max_height'     => 150, 
     21                        'max_bytes'      => 1048576, 
     22                        'use_default'    => 1, 
     23                        'identicon_size' => 100, 
     24                        'file_extns' => array("gif", "jpg", "jpeg", "png") 
     25                ); 
     26                 
     27                // Get database options 
     28                $db_options = bb_get_option('avatar_upload_options'); 
     29                 
     30                // If there are options in the database 
     31                if ($db_options) { 
     32                        // The database options override the defaults 
     33                        $options = array_merge($options, $db_options); 
     34                } 
     35                 
     36                // If there is no trailing slash on the directory then add it 
     37                if (substr($options['avatar_dir'], -1) != '/') { 
     38                        $options['avatar_dir'] .= '/'; 
     39                } 
     40                 
    1641                // Avatar folder location (default is 'avatars' in the bbPress root folder) 
    1742                // You must create the folder before you install this plugin. 
    18                 $this->avatar_dir = "avatars/"; // remember to include trailing slash 
    19  
     43                $this->avatar_dir = $options['avatar_dir']; 
     44                 
    2045                // Define maximum values allowed 
    21                 $this->max_width = 150; // pixels 
    22                 $this->max_height = 150; // pixels 
    23                 $this->max_bytes = 1048576; // filesize (1024 bytes = 1 KB / 1048576 bytes = MB) 
    24  
     46                $this->max_width = $options['max_width']; // pixels 
     47                $this->max_height = $options['max_height']; // pixels 
     48                $this->max_bytes = $options['max_bytes']; // filesize (1024 bytes = 1 KB / 1048576 bytes = MB) 
     49 
     50                // Use Unsharp Mask on resized truecolor images 1=yes (hidden option for now) 
     51                $this->use_unsharpmask = 1; 
     52                 
    2553                // Default avatar - set 'use_default' to '0' to display Identicon instead of default 
    2654                // The default URI is in the '$this->avatar_dir' folder. 
    2755                $this->default_avatar = array(   
    28                         'use_default' => 1
     56                        'use_default' => $options['use_default']
    2957                        'uri' =>  bb_get_option('uri') . $this->avatar_dir . 'default.png', 
    3058                        'width' => 80, 
     
    3260                        'alt' => "User has not uploaded an avatar" 
    3361                ); 
    34  
     62                 
    3563                // Identicon dimensions (width/height are equal): 
    36                 $config->identicon_size = 100; // pixels 
    37  
     64                $this->identicon_size = $options['identicon_size']; // pixels 
     65                 
    3866                // Allowed file extensions 
    39                 $this->file_extns = array("gif", "jpg", "jpeg", "png")
    40  
     67                $this->file_extns = $options['file_extns']
     68                 
    4169                // Just pretty values (Kilobytes/megabytes) for output use 
    4270                $this->max_kbytes = round($this->max_bytes / 1024, 2); 
     
    162190} 
    163191 
     192 
     193// Unsharp Mask on image resize (truecolor images only) 
     194function do_unsharp_mask($img, $use_unsharpmask=0) 
     195{ 
     196        if ($use_unsharpmask == 1) 
     197        { 
     198                require_once("unsharpmask.php"); 
     199                return UnsharpMask($img, 80, 0.5, 3); 
     200        } 
     201        else 
     202        { 
     203                return $img; 
     204        } 
     205} 
     206 
     207/** 
     208 * The admin pages below are handled outside of the class due to constraints 
     209 * in the architecture of the admin menu generation routine in bbPress 
     210 */ 
     211 
     212 
     213// Add filters for the admin area 
     214add_action('bb_admin_menu_generator', 'avatar_upload_admin_page_add'); 
     215add_action('bb_admin-header.php', 'avatar_upload_admin_page_process'); 
     216 
     217 
     218/** 
     219 * Adds in an item to the $bb_admin_submenu array 
     220 * 
     221 * @return void 
     222 * @author Sam Bauers 
     223 **/ 
     224function avatar_upload_admin_page_add() { 
     225        if (function_exists('bb_admin_add_submenu')) { // Build 794+ 
     226                bb_admin_add_submenu(__('Avatar Upload'), 'use_keys', 'avatar_upload_admin_page'); 
     227        } else { 
     228                global $bb_submenu; 
     229                $submenu = array(__('Avatar Upload'), 'use_keys', 'avatar_upload_admin_page'); 
     230                if (isset($bb_submenu['plugins.php'])) { // Build 740-793 
     231                        $bb_submenu['plugins.php'][] = $submenu; 
     232                } else { // Build 277-739 
     233                        $bb_submenu['site.php'][] = $submenu; 
     234                } 
     235        } 
     236} 
     237 
     238 
     239/** 
     240 * Writes an admin page for the plugin 
     241 * 
     242 * @return string 
     243 * @author Sam Bauers 
     244 * @modified Louise Dade 
     245 **/ 
     246function avatar_upload_admin_page() { 
     247         
     248        $config = new avatarupload_config(); 
     249         
     250        $gif_checked = (in_array('gif', $config->file_extns)) ? "checked=\"checked\" " : ""; 
     251        $jpg_checked = (in_array('jpg', $config->file_extns)) ? "checked=\"checked\" " : ""; 
     252        $png_checked = (in_array('png', $config->file_extns)) ? "checked=\"checked\" " : ""; 
     253 
     254        $use_default_checked = ($config->default_avatar['use_default']) ? "checked=\"checked\" " : ""; 
     255        $identicons_checked  = ($config->default_avatar['use_default']) ? "" : "checked=\"checked\" "; 
    164256?> 
     257        <h2>Avatar Upload Settings</h2> 
     258        <form method="post"> 
     259 
     260        <table cellpadding="5"> 
     261        <tr> 
     262                <th scope="row" width="360"><label for="avatar_dir">Directory to where avatars are uploaded:</label></th> 
     263                <td><code>bbPressRootDirectory/</code><input type="text" name="avatar_dir" id="avatar_dir" size="20" value="<?php echo($config->avatar_dir); ?>" /> <em>(location must be writable by the web server)</em></td> 
     264        </tr> 
     265        <tr> 
     266                <th scope="row"><label for="max_width">Maximum allowed width of avatars in pixels:</label></th> 
     267                <td><input type="text" name="max_width" id="max_width" size="4" value="<?php echo($config->max_width); ?>" /> pixels</td> 
     268        </tr> 
     269        <tr> 
     270                <th scope="row"><label for="max_height">Maximum allowed height of avatars in pixels:</label></th> 
     271                <td><input type="text" name="max_height" id="max_height" size="4" value="<?php echo($config->max_height); ?>" /> pixels</td> 
     272        </tr> 
     273        <tr> 
     274                <th scope="row"><label for="max_bytes">Maximum allowed filesize of avatars in bytes:</label></th> 
     275                <td><input type="text" name="max_bytes" id="max_bytes" size="10" value="<?php echo($config->max_bytes); ?>" /> bytes (n.b. 1 KB = 1024 bytes)</td> 
     276        </tr> 
     277        <tr> 
     278                <th scope="row">Allow the following image types to be uploaded:</th> 
     279                <td> 
     280                <label><input type="checkbox" name="file_extns[]" value="gif" <?php echo($gif_checked); ?>/> GIF (*.gif)</label><br /> 
     281                <label><input type="checkbox" name="file_extns[]" value="jpg"  <?php echo($jpg_checked); ?>/> JPEG (*.jpeg; *.jpg)</label><br /> 
     282                <label><input type="checkbox" name="file_extns[]" value="png"  <?php echo($png_checked); ?>/> PNG (*.png)</label> 
     283                </td> 
     284        </tr> 
     285        <tr> 
     286                <th scope="row">Action to take when a user has no avatar present:</th> 
     287                <td> 
     288                <label><input type="radio" name="use_default" value="1" <?php echo($use_default_checked); ?>/> Use the default avatar image</label>  
     289                (<a href="<?php echo $config->default_avatar['uri']; ?>" target="_blank" title="View default avatar in a new window">view</a>).<br /> 
     290                <label><input type="radio" name="use_default" value="0" <?php echo($identicons_checked); ?>/> Use an auto generated identicon</label> 
     291                </td> 
     292        </tr> 
     293        <tr> 
     294                <th scope="row"><label for="identicon_size">Height &amp; width of identicons in pixels (if used):</label></th> 
     295                <td><input type="text" name="identicon_size" id="identicon_size" size="4" value="<?php echo($config->identicon_size); ?>" /> pixels</td> 
     296        </tr> 
     297        </table> 
     298                <input name="action" type="hidden" value="avatar_upload_settings_post"/> 
     299                <p class="submit"><input type="submit" name="submit" value="Save Avatar Upload Settings" /></p> 
     300        </form> 
     301 
     302        <h3>Don't Forget!</h3> 
     303 
     304        <p>If you haven't already done so: upload the files in the "additional-files" directory to the following locations.</p> 
     305        <ul> 
     306                <li><code>avatars/</code> - directory to the location specified on the admin page, rename if neccesary.</li> 
     307                <li><code>avatars/default.png</code> - default avatar image into the directory created above.</li> 
     308                <li><code>avatar-upload.php</code> - bbPress root directory.</li> 
     309                <li><code>my-templates/avatar.php</code> - your <code>my-templates/my-template-name/</code> (or <code>bb-templates/kakumei/</code>) directory.</li> 
     310        </ul> 
     311 
     312<p><strong>Read the <code>readme.txt</code> file that came with this plugin for more detailed instructions.</strong></p> 
     313 
     314<?php 
     315} 
     316 
     317 
     318/** 
     319 * Processes the admin page form 
     320 * 
     321 * @return void 
     322 * @author Sam Bauers 
     323 * @modified Louise Dade 
     324 **/ 
     325function avatar_upload_admin_page_process() 
     326{ 
     327        if (isset($_POST['submit'])) { 
     328                if ($_POST['action'] == 'avatar_upload_settings_post') { 
     329                         
     330                        $options = array(); 
     331                         
     332                        if ($_POST['avatar_dir']) { 
     333                                $options['avatar_dir'] = $_POST['avatar_dir']; 
     334                        } 
     335                         
     336                        if (ereg("^[0-9]{1,}$", $_POST['max_width'])) { 
     337                                $options['max_width'] = $_POST['max_width']; 
     338                        } 
     339                         
     340                        if (ereg("^[0-9]{1,}$", $_POST['max_height'])) { 
     341                                $options['max_height'] = $_POST['max_height']; 
     342                        } 
     343                         
     344                        if (ereg("^[0-9]{1,}$", $_POST['max_bytes'])) { 
     345                                $options['max_bytes'] = $_POST['max_bytes']; 
     346                        } 
     347                         
     348                        if ($_POST['file_extns']) { 
     349                                $valid = array("gif", "jpg", "png"); // not user configurable! 
     350 
     351                                foreach ($_POST['file_extns'] as $extn) 
     352                                { 
     353                                        if (in_array($extn, $valid)) { 
     354                                                $options['file_extns'][] = $extn; 
     355                                                if ($extn == "jpg") { 
     356                                                        $options['file_extns'][] = "jpeg"; 
     357                                                } 
     358                                        } 
     359                                } 
     360                        } 
     361                         
     362                        if (!$_POST['use_default']) { 
     363                                $options['use_default'] = 0; 
     364                        } 
     365                         
     366                        if (ereg("^[0-9]{1,}$", $_POST['identicon_size'])) { 
     367                                $options['identicon_size'] = $_POST['identicon_size']; 
     368                        } 
     369                         
     370                        if (count($options)) { 
     371                                bb_update_option('avatar_upload_options', $options); 
     372                        } else { 
     373                                bb_delete_option('avatar_upload_options'); 
     374                        } 
     375                         
     376                        bb_admin_notice(__('Settings Saved')); 
     377                } 
     378        } 
     379} 
     380?> 
  • avatar-upload/trunk/readme.txt

    r501 r510  
    44Requires at least: 0.8.2 
    55Tested up to: 0.8.2.1 
    6 Stable Tag: 0.6.2 
     6Stable Tag: 0.7 
    77 
    88Allows users to upload an avatar (gif, jpeg/jpg or png) image to bbPress. Admins can configure maximum allowed file size and image dimensions. Includes fel64's code enabling 'Identicons' - default avatars made of abstract patterns unique to each user. 
     
    2020* Bozos can not upload avatars. 
    2121 
    22 * Admins can configure maximum allowed file size (bytes) and dimensions (pixels) of images
     22* Admins can configure maximum allowed file size (bytes) and dimensions (pixels) of images from within the admin page (credit: Sam Bauers)
    2323 
    24   - Currently done from within the script (no Admin page interface at this time). 
    25  
    26   - Images that exceed maximum dimensions are automatically resized. 
     24  - Images that exceed maximum dimensions are automatically resized (and sharpened if truecolor images). 
    2725 
    2826* Anybody with the 'moderate' capability can upload another user's avatar 
     
    3028  - this to ensures that inappropriate images can be removed. 
    3129 
    32   - there is no "delete avatar" function at this time, but an inappropriate image can be removed by uploading a 'safe' image (e.g. a blank 1x1 pixel image) to replace it (you could them manually set that user as a bozo to stop them re-uploading inappropriate images). 
     30  - there is no "delete avatar" function at this time, but an inappropriate image can be removed by uploading a 'safe' image (e.g. a blank 1x1 pixel image) to replace it (you could them manually set that user as a bozo to stop them re-uploading inappropriate images). Alternatively, you could set their avatar to an identicon (see below). 
    3331 
    3432* Option to display a default avatar for users who do not upload their own. 
     
    4038== Installation == 
    4139 
    42 UPGRADING?  If you are using an older version of this plugin, you need to follow these installation instructions because the template functions are incompatible with older versions
     40UPGRADING?  If you are using a version older than 0.3 of this plugin you NEED to follow these instructions fully because the template functions are incompatible with older versions.  If you are upgrading from version 0.3 or later then of this plugin you can ignore the template instructions (steps 2 to 4), but do check the rest
    4341 
    44 1. Open up the `my-plugins/bb-avatar-upload.php` file and configure the "Configuration Settings". At least make sure the `$avatar_dir` variable is correct. 
    45  
    46     IMPORTANT: to use Identicons, you must set the 'use_default' (avatar) option to '0' so that 
    47     the user's automatically created identicon is displayed and not the generic default image.  
    48     Obviously, to go back to using a generic default set the option to '1' again. 
     421. After activating the plugin go to the "Avatar upload" admin page and configure the options displayed there. At least make sure you have set the avatar upload directory to the location of your choice. 
    4943 
    50442. The avatar upload page should appear as a tab ("Avatar") on the user's Profile menu.  If you'd prefer the link to be elsewhere, insert the following "Upload Avatar" link wherever you wish: 
     
    76704. OPTIONAL: open up `my-templates/avatar.php` and edit the template if you wish, but be sure not to mess with the upload form. 
    7771 
    78 5. Upload the plugin files to the following locations. 
     725. Upload the files in the "additional-files" directory to the following locations. 
    7973 
    80    `avatars/default.png`             - both `avatars/` dir and image into the bbPress root dir. 
     74   `avatars/`                    - directory to the location specified on the admin page, rename if neccesary 
    8175 
    82    `avatar-upload.php`               - bbPress root dir
     76   `avatars/default.png`         - default avatar image into the directory created above
    8377 
    84    `my-templates/avatar.php`         - your `my-templates/my-template-name/` (or kakumei) dir
     78   `avatar-upload.php`           - bbPress root directory
    8579 
    86    `my-plugins/bb-avatar-upload.php` - your `my-plugins/` dir (and activated)
     80   `my-templates/avatar.php`     - your `my-templates/my-template-name/` (or bb-templates/kakumei/) directory
    8781 
    88    `my-plugins/identicon.php`        - your `my-plugins/` dir (it is automatically activated). 
     826. Upload the plugin files. 
     83 
     84   `bb-avatar-upload.php`        - your `my-plugins/` directory (and activate it). 
     85 
     86   `identicon.php`               - your `my-plugins/` directory (it is automatically included). 
     87 
     88   `unsharpmask.php`             - your `my-plugins/` directory (it is automatically included). 
    8989 
    9090That's it, the 'Avatar Upload' plugin should now be working. 
     
    9292== Configuration == 
    9393 
    94 Some variables can be configured.  See 'Installation' instructions. 
     94The following options are configured on the 'Avatar Upload Settings' admin page. 
     95 
     96* Avatar upload directory 
     97 
     98* Max. allowed width 
     99 
     100* Max. allowed height 
     101 
     102* Max. allowed filesize 
     103 
     104* Allow upload of GIFs, JPGs and/or PNGs 
     105 
     106* Use default avatar or auto generated identicon 
     107 
     108* Height/width of identicons (if used) 
     109 
     110See 'Installation' instructions and admin page for more details. 
    95111 
    96112== Frequently Asked Questions == 
     
    106122You need to set the file permissions (chmod) of the `avatars` folder to `666` to allow the plugin to write to the folder.  You can do this using SHH or alternatively (and more easily) many FTP applications allow permissions setting.  Please refer to your web host for their advice if you do not know how to do this. 
    107123 
     124= When I upload some JPEGs they either look rubbish or are blocks of plain colour = 
     125 
     126[N.b. this can also apply to some PNG images, but I'm only going to talk about JPEGS.] 
     127 
     128This is most likely a result of your web host providing a version of PHP that can not create 'truecolor' images. JPEGS are usually photographic images and therefore contain millions of colours, hence the need for truecolor. The plugin detects if truecolor is not an option and instead uses the same function as GIF (palette based) images, which can make some JPEGS look awful. 
     129 
     130To get the best out of a JPEG image you really need <strong>PHP version >= 4.0.6 or PHP 5 and GD library GD 2.0.1 or later</strong>. 
     131 
     132Alternatively, because this only happens when an image needs to be resized, you could inform your uses that they <strong>must</strong> make sure they avatar conforms to the maximum allowed dimensions <em>before</em> they upload it. 
     133 
    108134== Change Log == 
     135 
     1362007-07-23 Ver. 0.7   Added admin page for configuration, added an unsharp mask function, changed  
     137                      additional file layout to be more friendly to the Plugin Browser plugin. 
    109138 
    1101392007-07-19 Ver. 0.6.2 Bug-fix. 'Upload Avatar' page wasn't displaying new avatar after upload.