Changeset 671

Show
Ignore:
Timestamp:
09/09/07 16:57:42 (1 year ago)
Author:
louisedade
Message:

avatar-upload: committing version 0.8.3 - added thumbnail feature (undocumented)

Files:

Legend:

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

    r531 r671  
    3434if (!empty($_FILES['p_browse'])) 
    3535{ 
    36         $current_avatar = avatarupload_get_avatar($user_id, 0, 1); // for comparison later 
     36        $arr_current_avatar = avatarupload_get_avatar($user_id, 0, 1); 
     37        $current_avatar = ereg_replace("\?[0-9]+$", "", $arr_current_avatar[0]); // for comparison later 
    3738 
    3839        // Grab the uploaded image 
     
    8283                        $img_size = @filesize($img_temp); 
    8384                        list($img_w, $img_h) = $resized; // overwrite image width / height 
     85 
     86 
     87                        // Do we want a thumbnail and was it successful? 
     88                        if  ($config->use_thumbnail == 1) 
     89                        { 
     90                                $thumb_file = BBPATH . $config->avatar_dir . "thumb." . $user_filename; 
     91 
     92                                if ( !$thumbresized = avatar_thumbnail($img_temp, $img_w, $img_h, $img_type, $thumb_file) ) 
     93                                { 
     94                                        $img_errs = 12; 
     95                                } 
     96                        } 
     97 
    8498                } 
    8599        } 
     
    101115        { 
    102116                // Compare 'new' and 'current' avatar filenames 
    103                 if (!empty($current_avatar[0]) && $user_filename != $current_avatar[0]
     117                if (!empty($current_avatar) && $user_filename != $current_avatar
    104118                { 
    105119                        // If different, delete 'current' - this will only occur when 
    106120                        // the new and current avatars have different file extensions. 
    107                         @unlink(BBPATH . $config->avatar_dir . $current_avatar[0]); 
     121                        @unlink(BBPATH . $config->avatar_dir . $current_avatar); 
     122                        @unlink(BBPATH . $config->avatar_dir . "thumb." . $current_avatar); 
    108123                } 
    109124 
     
    150165                        case 11: // custom error code 
    151166                                $error_message = __("The file could not be saved to the {$config->avatar_dir} folder."); 
     167                                break; 
     168                        case 12: // custom error code 
     169                                $error_message = __("The thumbnail file could not be saved to the {$config->avatar_dir} folder."); 
    152170                                break; 
    153171                        default: // unknown error (this probably won't ever happen) 
     
    261279} 
    262280 
     281 
     282/* Image thumbnail */ 
     283function avatar_thumbnail($img_temp, $img_w, $img_h, $img_type, $img_thumb_name) 
     284{ 
     285        global $config; 
     286 
     287        // if either the image width or height is greater than the maximums allowed 
     288        if ($img_w > $config->thumb_width || $img_h > $config->thumb_height) 
     289        { 
     290                // To maintain aspect ratio we need to resize proportionally 
     291 
     292                if ($img_w > $img_h) 
     293                { 
     294                        // width is greater - make width 'max_width' and proportion height 
     295                        $new_width = $config->thumb_width; 
     296                        $new_height = round($img_h * ($config->thumb_width/$img_w)); 
     297                } 
     298                else if ($img_w < $img_h) 
     299                { 
     300                        // height is greater - make height 'max_height' and proportion width 
     301                        $new_width = round($img_w * ($config->thumb_height/$img_h)); 
     302                        $new_height = $config->thumb_height; 
     303                } 
     304                else 
     305                { 
     306                        // equal (square) - make both 'max' values 
     307                        $new_width = $config->thumb_width; 
     308                        $new_height = $config->thumb_height; 
     309                } 
     310        } 
     311        else 
     312        { 
     313                // image already within maximum limits - do not resize 
     314                $new_width = $img_w; 
     315                $new_height = $img_h; 
     316        } 
     317 
     318        // Thumbnail coordinates if width/height smaller than max thumb size 
     319        // because we want a square thumb image. 
     320        $x_start = ($config->thumb_width - $new_width > 0) ? round(($config->thumb_width - $new_width)/2) : 0; 
     321        $y_start = ($config->thumb_height - $new_height > 0) ? round(($config->thumb_height - $new_height)/2) : 0; 
     322 
     323        // Can we use a truecolor image? 
     324        $truecolor = function_exists('imagecreatetruecolor'); 
     325 
     326        // Resize the image, preserving image type 
     327 
     328        switch ($img_type) 
     329        { 
     330                case 1: 
     331                        // GIF 
     332                        $im1 = @imagecreatefromgif($img_temp); 
     333                        $im2 = @imagecreate($config->thumb_width, $config->thumb_height) or $error = 1; 
     334                        $bg = @imagecolorallocate($im2, 192, 192, 192); // default background color 
     335                        @imagecolortransparent($im2, $bg); // make it transparent 
     336                        @imagecopyresampled ($im2, $im1, $x_start, $y_start, 0, 0, $new_width, $new_height, $img_w, $img_h); 
     337                        @imagegif($im2, $img_thumb_name); 
     338                        break; 
     339 
     340                case 2: 
     341                        // JPEG 
     342                        $im1 = @imagecreatefromjpeg($img_temp); 
     343                        $im2 = ($truecolor) ? @imagecreatetruecolor($config->thumb_width, $config->thumb_height) : @imagecreate($config->thumb_width, $config->thumb_height); 
     344                        @imagecopyresampled ($im2, $im1, $x_start, $y_start, 0, 0, $new_width, $new_height, $img_w, $img_h); 
     345                        $im2 = ($truecolor) ? do_unsharp_mask($im2, $config->use_unsharpmask) : $im2; 
     346                        @imagejpeg($im2, $img_thumb_name, 100); // quality integer might become config variable 
     347                        break; 
     348 
     349                case 3: 
     350                        // PNG 
     351                        $im1 = @imagecreatefrompng($img_temp); 
     352                        $im2 = ($truecolor) ? @imagecreatetruecolor($config->thumb_width, $config->thumb_height) : @imagecreate($config->thumb_width, $config->thumb_height); 
     353                        $bg = @imagecolorallocate($im2, 192, 192, 192); // default background colour 
     354                        @imagecolortransparent($im2, $bg); // make it transparent 
     355                        @imagealphablending($im2, false); // stop alpha blending 
     356                        @imagesavealpha($im2, true); // save original image alpha channel 
     357                        @imagecopyresampled ($im2, $im1, $x_start, $y_start, 0, 0, $new_width, $new_height, $img_w, $img_h); 
     358                        $im2 = ($truecolor) ? do_unsharp_mask($im2, $config->use_unsharpmask) : $im2; 
     359                        @imagepng($im2, $img_thumb_name); 
     360                        break; 
     361 
     362                default: 
     363                        $error = 1; 
     364                        break; 
     365        } 
     366 
     367        @imagedestroy($im2); 
     368        @imagedestroy($im1); 
     369 
     370        if ($error > 0) 
     371        { 
     372                // Something went wrong. 
     373                return false; 
     374        } else { 
     375                // return the new sizes 
     376                return array($new_width, $new_height); 
     377        } 
     378} 
     379 
    263380?> 
  • avatar-upload/trunk/additional-files/my-templates/avatar.php

    r532 r671  
    2929<h3><?php _e('Current Avatar'); ?></h3> 
    3030 
    31 <p><?php echo avatarupload_display($user->ID, $force_db); ?></p> 
     31<p><?php 
     32        echo avatarupload_display($user->ID, $force_db); 
     33 
     34        if ($config->use_thumbnail == 1) { 
     35                echo " &nbsp; " . avatarupload_displaythumb($user->ID, $force_db) . "</p>"; 
     36        } 
     37?></p> 
    3238 
    3339<?php if (!usingidenticon($user->ID)) { ?> 
  • avatar-upload/trunk/bb-avatar-upload.php

    r568 r671  
    33Plugin Name: Avatar Upload 
    44Plugin URI: http://bbpress.org/plugins/topic/46 
    5 Version: 0.8.2 
     5Version: 0.8.3 
    66Description: Allows users to upload an avatar (gif, jpeg/jpg or png) image to bbPress. 
    77Author: Louise Dade 
     
    5151                $this->use_unsharpmask = 1; 
    5252                 
     53                // Use a thumbnail image (hidden feature for now). 1 = yes / 0 = no (default) 
     54                $this->use_thumbnail = 0; 
     55                $this->thumb_width = 25; 
     56                $this->thumb_height = 25; 
     57 
    5358                // Default avatar - set 'use_default' to '0' to display Identicon instead of default 
    5459                // The default URI is in the '$this->avatar_dir' folder. 
     
    102107} 
    103108 
     109// Display the avatar image 
     110function avatarupload_displaythumb($id, $force_db=0, $class='avatar_thumb') 
     111{ 
     112        $config = new avatarupload_config(); 
     113 
     114        if ($a = avatarupload_get_avatar($id,1,$force_db, 1)) 
     115        { 
     116                echo '<img src="'.$a[0].'" width="'.$config->thumb_width.'" height="'.$config->thumb_height.'" alt="'.$a[4].'" class="'.$class.'" />'; 
     117        } 
     118} 
     119 
    104120// Get the avatar URI ($id = user->ID, $fulluri = full url to image, 
    105121// $force_db = get avatar from database where 'usermeta' not already available) 
    106 function avatarupload_get_avatar($id, $fulluri=1, $force_db=0
     122function avatarupload_get_avatar($id, $fulluri=1, $force_db=0, $is_thumb=0
    107123{ 
    108124        global $bbdb, $user; 
     
    132148                        return false; 
    133149                } 
     150        } 
     151 
     152        if ($is_thumb == 1) 
     153        { 
     154                $a[0] = "thumb.".$a[0]; 
    134155        } 
    135156 
  • avatar-upload/trunk/readme.txt

    r568 r671  
    44Requires at least: 0.8.2 
    55Tested up to: 0.8.2.1 
    6 Stable Tag: 0.8.2 
     6Stable Tag: 0.8.3 
    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.