Changeset 1155 for trunk/common


Ignore:
Timestamp:
11/12/11 17:18:55 (6 months ago)
Author:
adrian.budau
Message:

Avatar Optimization

This is an optimization for managing avatars on the server. Because
there are only 5 sizes used on all the site it's more useful to resize
all of the images to those 5(thus increasing the space usage by less
than 100%) and when a client asks for a size give it directly rather
then resizing all the time. The 5 sizes are tiny (old L16x16),
small(old L32x32), normal(old L50x50), forum(old L75xL75, used only on

the forum at the moment), big(old 100x100) and full(the original picture).


To get the avatar on a size from the server simply use the page
avatar/%some_size%/%some_user where %some_user% is a username and
%some_size$ is a one the 5 sizes. In case the user doesn't exists or he
doesn't have an avatar we use a placeholder image. If the size given is
not from the list or is omitted the server return a classic
404 Error(File not found)

The script which resizes the image for the current users is called
make-avatar-folder found in the scripts folder. It requires the attach
folder to have permission for read and write.

The setup has also been modified so fresh installes will work without
calling the script.

Reviewboard Link: http://reviewboard.infoarena.ro/r/176/

Location:
trunk/common
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/common/attachment.php

    r1115 r1155  
    146146} 
    147147 
     148/** 
     149 * Resizes an image whose filepath is given by the parameters into a new 
     150 * location specified by the other parameters 
     151 * 
     152 * Returns whether or not the image has been successfully resized 
     153 * Note that it returns false if the image doesn't have a known file-type 
     154 * 
     155 * The $new_filepath parameter if ommited or given as null will result in the 
     156 * image being outputted directly to the client 
     157 * 
     158 * @param  array   $image_info         An array containing information about the 
     159 *                                     original image: width, height, mime-type 
     160 * @param  string  $filepath 
     161 * @param  array   $new_image_info     An array containing the new width and 
     162 *                                     height 
     163 * @param  string  $new_filepath 
     164 * @return bool 
     165 */ 
     166function image_resize($image_info, $filepath, $new_image_info, 
     167        $new_filepath = null) { 
     168    list($image_width, $image_height, $image_type, $image_attribute) = $image_info; 
     169    list($new_image_width, $new_image_height) = $new_image_info; 
     170 
     171    switch ($image_type) { 
     172        case IMAGETYPE_GIF: 
     173            // NOTE: animated GIFs become static. Only the first frame is saved 
     174            // Seems like a good thing anyway 
     175            $image = imagecreatefromgif($filepath); 
     176            $image_resized = imagecreate($new_image_width, $new_image_height); 
     177            // reset palette and transparent color to that of the original file 
     178            $trans_col = imagecolortransparent($image); 
     179            imagepalettecopy($image_resized, $image); 
     180            imagefill($image_resized, 0, 0, $trans_col); 
     181            imagecolortransparent($image_resized, $trans_col); 
     182            imagecopyresampled($image_resized, $image, 0, 0, 0, 0, 
     183                    $new_image_width, $new_image_height, $image_width, 
     184                    $image_height); 
     185 
     186            if ($new_filepath != null) { 
     187                return imagegif($image_resized, $new_filepath); 
     188            } 
     189            return imagegif($image_resized); 
     190 
     191        case IMAGETYPE_JPEG: 
     192            $image = imagecreatefromjpeg($filepath); 
     193            $image_resized = imagecreatetruecolor($new_image_width, 
     194                    $new_image_height); 
     195            imagecopyresampled($image_resized, $image, 0, 0, 0, 0, 
     196                    $new_image_width, $new_image_height, $image_width, 
     197                    $image_height); 
     198 
     199            if ($new_filepath != null) { 
     200                return imagejpeg($image_resized, $new_filepath); 
     201            } 
     202            return imagejpeg($image_resized); 
     203 
     204        case IMAGETYPE_PNG: 
     205            $image = imagecreatefrompng($filepath); 
     206            $image_resized = imagecreatetruecolor($new_image_width, 
     207                    $new_image_height); 
     208            // turn off the alpha blending to keep the alpha channel 
     209            imagealphablending($image_resized, false); 
     210            // allocate transparent color 
     211            $col = imagecolorallocatealpha($image_resized, 0, 0, 0, 127); 
     212            // fill the image with the new color 
     213            imagefilledrectangle($image_resized, 0, 0, $new_image_width, 
     214                    $new_image_height, $col); 
     215            imagecopyresampled($image_resized, $image, 0, 0, 0, 0, 
     216                    $new_image_width, $new_image_height, $image_width, 
     217                    $image_height); 
     218            imagesavealpha($image_resized, true); 
     219            if ($new_filepath != null) { 
     220                return imagepng($image_resized, $new_filepath); 
     221            } 
     222            return imagepng($image_resized); 
     223 
     224        default: 
     225            // unsupported image type 
     226            return false; 
     227    } 
     228} 
    148229?> 
  • trunk/common/common.php

    r1083 r1155  
    260260function is_taggable($obj) { 
    261261    return $obj == 'user' || $obj == 'task' || $obj == 'round' || $obj == 'textblock'; 
     262} 
     263 
     264/** 
     265 * Returns whether or not the size type given is an existent one on the site 
     266 * 
     267 * @param  string  $size_type 
     268 * @return bool 
     269 */ 
     270function is_valid_size_type($size_type) { 
     271    $size_types = array("full", "tiny", "small", "normal", "forum", "big"); 
     272    return in_array($size_type, $size_types); 
    262273} 
    263274 
  • trunk/common/db/user.php

    r1110 r1155  
    168168{ 
    169169    log_assert_valid(user_validate($user)); 
    170      
     170 
    171171    // Update DB 
    172172    db_update("ia_user", $user, "id = ".db_quote($user['id'])); 
Note: See TracChangeset for help on using the changeset viewer.